Running make from Outside the Tree
Let's say I have a makefile like the following:
CXXFLAGS := -I./Include
Foo:
$(CXX) $(CXXFLAGS) -o Foo
If the user cd
s into the source tree and runs make
, everything is fine and dandy. However, if make
is invoked from somewhere outside the source tree, the include directory will be incorrect.
Using full paths instead of relative paths works, but that de开发者_运维知识库stroys the portability of the makefile.
Should I just rely on users invoking make
"properly?" Or is there an easy way to get around this?
If you want to have CXXFLAGS
be -Isome_dir/Include
when the make
is invoked asmake -f some_dir/Makefile
,
MAKEFILE_LIST
might meet the purpose.
For example:
MAKEFILE_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
CXXFLAGS := -I$(MAKEFILE_DIR)Include
If your make
's version is 3.80 or lower, lastword
might not work.
In that case,
$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
will work instead.
EDIT: This answer is for GNU-make.
Use the -C
flag.
make -C my_dir
You could present your users a compile command that looks like (cd /to/proper/directory && make -k)
精彩评论