The*_*ist 5 c linux bash makefile gnu-make
对于 C 库,由于内存问题,我需要检查当前编译器是否适用于 x86_64。我发现的命令完全符合我的要求:
CXXARCH:=$(${CXX} -dumpmachine | grep -i 'x86_64')
Run Code Online (Sandbox Code Playgroud)
哪里${CXX}是gcc或clang。对于x86_64机器,这将返回一个非空字符串。对于 32 位机器,比如 Raspberry Pi,这将是空的。
我如何区分这两种情况?
我这样做了:
ifneq (${CXXARCH},)
MAGICVAR:=-DMY_DEFINE
endif
Run Code Online (Sandbox Code Playgroud)
通过$(info)打印,我确保在 Raspberry Pi 中没有满足这个条件,它应该满足,因为命令clang-6.0 -dumpmachine返回:armv7l-unknown-linux-gnueabihf。那么为什么这个条件没有被执行呢?我究竟做错了什么?
语法
$(${CXX} -dumpmachine | grep -i 'x86_64')
Run Code Online (Sandbox Code Playgroud)
是shell语法。它不会在Makefile. 要在Makefile 中扩展 CXX 变量,首选语法是使用$(CXX)(虽然${CXX}也可以,但$CXX不能)。要捕获 shell 输出,您需要使用$(shell command). 因此
CXXARCH:=$(shell $(CXX) -dumpmachine | grep -i 'x86_64')
ifneq ($(CXXARCH),)
MAGICVAR:=-DMY_DEFINE
endif
Run Code Online (Sandbox Code Playgroud)
请注意,编译器目标与结果程序中的“内存问题”无关。您可以使用编译 32 位程序 (-m32) 并获得与 32 位编译器相同的“相同内存问题”。x86_64-linux-gnu-gcc
最后确保你没有混淆ifneq和ifeq。
ifneq ($(CXXARCH),)
Run Code Online (Sandbox Code Playgroud)
意思是“如果$(CXXARCH)没有不扩大为空字符串,然后......”