[英]Change variable's value in Makefile does not have effect
I am new to Makefile and I have written a simple Makefile to compile my C++ program. 我是Makefile的新手,并且编写了一个简单的Makefile来编译我的C ++程序。
TSP_OBU target checks if two libraries (lib_wave.a and libsaej2735.a) exists in the parent folder. TSP_OBU目标检查父文件夹中是否存在两个库(lib_wave.a和libsaej2735.a)。 If yes, the default value of 'LIBWAVE_PATH' and 'LIBSAEJ2735_PATH' variables are changed (in target LIBWAVE_check and LIBSAEJ2735_check).
如果是,则更改“ LIBWAVE_PATH”和“ LIBSAEJ2735_PATH”变量的默认值(在目标LIBWAVE_check和LIBSAEJ2735_check中)。 But apparently changing the 'LIBWAVE_PATH' and 'LIBSAEJ2735_PATH' variables do not have any effect and the default value (defined at the top of Makefile) is always used.
但是显然更改'LIBWAVE_PATH'和'LIBSAEJ2735_PATH'变量没有任何效果,并且始终使用默认值(在Makefile的顶部定义)。
LIBWAVE_PATH = /home/ubuntu/VENTOS/source/libs/lib_wave.a
LIBSAEJ2735_PATH = /home/ubuntu/VENTOS/source/libs/libsaej2735.a
all: TSP_OBU
# link
TSP_OBU: TSP_OBU.o gps.o wave.o LIBWAVE_check LIBSAEJ2735_check
g++ -std=c++11 -g -o TSP_OBU out/TSP_OBU.o out/gps.o out/wave.o -lboost_filesystem -lboost_system -lgps -lm -pthread $(LIBWAVE_PATH) $(LIBSAEJ2735_PATH)
# compile TSP_OBU
TSP_OBU.o: src/TSP_OBU.cc
g++ -std=c++11 -Wall -g -O2 -c -o out/TSP_OBU.o src/TSP_OBU.cc
# compile gps
gps.o: src/gps.cc
g++ -std=c++11 -Wall -g -O2 -c -o out/gps.o src/gps.cc
# compile wave
wave.o: src/wave.cc
g++ -std=c++11 -Wall -g -O2 -c -o out/wave.o src/wave.cc
# check if 'lib_wave.a' exists
LIBWAVE_check:
ifeq (,$(wildcard ../lib_wave.a))
ifeq (,$(wildcard $(LIBWAVE_PATH)))
$(error 'lib_wave.a' does not exist in $(LIBWAVE_PATH))
endif
else
LIBWAVE_PATH=../lib_wave.a
endif
# check if 'libsaej2735.a' exists
LIBSAEJ2735_check:
ifeq (,$(wildcard ../libsaej2735.a))
ifeq (,$(wildcard $(LIBSAEJ2735_PATH)))
$(error 'libsaej2735.a' does not exist in $(LIBSAEJ2735_PATH))
endif
else
LIBSAEJ2735_PATH=../libsaej2735.a
endif
# clean
clean:
rm -rf TSP_OBU
rm -rf out/TSP_OBU.o
rm -rf out/gps.o
rm -rf out/wave.o
Consider a simpler version of this rule: 考虑此规则的一个更简单的版本:
LIBWAVE_check:
LIBWAVE_PATH=../lib_wave.a
Each command in a rule executes in its own sub-shell. 规则中的每个命令都在其自己的子外壳中执行。 The command
LIBWAVE_PATH=...
modifies the shell variable, which then expires when the command terminates, so the change has no effect. 命令
LIBWAVE_PATH=...
修改shell变量,该变量将在命令终止时到期,因此更改无效。
To modify the Make variables in a way that the TSP_OBU
rule can use, you have two options. 要以
TSP_OBU
规则可以使用的方式修改Make变量,您有两个选择。 You could modify them within the command that uses them: 您可以在使用它们的命令中对其进行修改:
TSP_OBU: ...
LIBWAVE_PATH=../lib_wave.a; LIBSAEJ2735_PATH=../libsaej2735.a; g++ ...
Then for the conditionals you could either use shell conditionals within the command, or Make conditionals surrounding the command, a pain either way. 那么对于条件语句,你既可以使用Shell条件的命令中,或使周围的命令,疼痛无论哪种方式,条件语句。
The other option (which I recommend) is to remove the "check" targets and put all of this logic outside the rules: 另一个选择(我建议)是删除“检查”目标并将所有这些逻辑置于规则之外:
LIBWAVE_PATH = /home/ubuntu/VENTOS/source/libs/lib_wave.a
ifeq (,$(wildcard ../lib_wave.a))
ifeq (,$(wildcard $(LIBWAVE_PATH)))
$(error 'lib_wave.a' does not exist in $(LIBWAVE_PATH))
endif
else
LIBWAVE_PATH=../lib_wave.a
endif
...and likewise for LIBSAEJ2735_PATH
. ...同样适用于
LIBSAEJ2735_PATH
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.