如何在c文件的#ifdef中使用makefile中的变量

如何在c文件的#ifdef中使用makefile中的变量,第1张

概述Makefile ifeq ($(wifiSim),1)WIFISIM :=1endifall: test.cpptest.cpp : test.o ./a.outtest.o : c++ test.cpp test.cpp #include <iostream>using namespace std;int main(){

Makefile

ifeq ($(wifiSim),1)WIFISIM :=1endifall: test.cpptest.cpp : test.o        ./a.outtest.o :        c++ test.cpp

test.cpp

#include <iostream>using namespace std;int main(){            #ifdef WIFISIM                cout << "InsIDe wifisim = 1" << endl;        #else              cout << "OutsIDe wifisim = 1" << endl;        #endif        return 0;}

我想在test.cpp中使用“WIFISIM”.
我正在运行“make wifiSim = 1 all”
但是else正在test.cpp中执行

有没有办法在不对“test.cpp”的编译方式做任何改变的情况下做到这一点,因为我需要在许多文件中使用这个标志WIFISIM,我不想改变它们的编译方式是正在完成.

解决方法 你可能会做这样的事情

ifeq ($(wifiSim),1)    WIFISIM := -DWIFISIMendifall: test.cpptest.cpp : test.o        ./a.outtest.o :        c++ $(WIFISIM) test.cpp

“Is there any way I can do it without doing any changes in the way the compilation for “test.cpp” is done,because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done.”

不,如果不更改规则中的编译器调用 *** 作,就没有办法.

您应该更改编写makefile的策略. make实际上支持implicit rules如何从.cpp创建.o文件并使用看起来像的动作

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

因此,您可以有条件地将-DWIFISIM添加到$(CPPFLAGS)或$(CXXFLAGS)变量,它将应用于编译的所有.cpp文件.

使用隐式规则的示例:

ifeq ($(wifiSim),1)    CXXFLAGS += -DWIFISIMendifSRC_fileS := test.cpp abc.cpp yxz.cppOBJ_fileS := $(patsubst %.cpp,%.o,$(SRC_fileS))all: testtest: $(OBJ_fileS)
总结

以上是内存溢出为你收集整理的如何在c文件的#ifdef中使用makefile中的变量全部内容,希望文章能够帮你解决如何在c文件的#ifdef中使用makefile中的变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1216765.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存