简体   繁体   English

C ++预处理器变量

[英]C++ preprocessor variable

I'm using the SKELETON_JAR variable on my C++ code in one header. 我在一个标题中使用我的C ++代码上的SKELETON_JAR变量。 However, I want to allow the user to define the place of the jar in the compile time easily. 但是,我希望允许用户轻松地在编译时定义jar的位置。 I think the easiest way to do that is to put this define in makefile is that so? 我认为最简单的方法就是把这个定义放在makefile中就是这样吗?

#define SKELETON_JAR "./Util.jar"

In your code: 在你的代码中:

#ifndef SKELETON_JAR
  #define SKELETON_JAR "./Util.jar" // default path
#endif

and then in the makefile use CPPFLAGS:=-DSKELETON_JAR="./Util.jar" . 然后在makefile中使用CPPFLAGS:=-DSKELETON_JAR="./Util.jar"

Of course you have to make sure CPPFLAGS are passed to the compiler as part of the compile rule which is the case if you're using the default implicit rules. 当然,您必须确保将CPPFLAGS作为编译规则的一部分传递给编译器,如果您使用的是默认隐式规则。

From GNU Make documentation : 来自GNU Make文档

Compiling C programs 编译C程序

no is made automatically from nc with a command of the form `$(CC) -c $(CPPFLAGS) $(CFLAGS)' 使用“$(CC)-c $(CPPFLAGS)$(CFLAGS)”形式的命令从nc自动生成

Depending on your compiler, the normal way to do this is to use the compiler's -D flag in the makefile. 根据您的编译器,执行此操作的常规方法是在makefile中使用编译器的-D标志。 For example: 例如:

MYFLAGS = -DSKELETON_JAR="foo" MYFLAGS = -DSKELETON_JAR =“foo”

then later on: 然后是:

gcc $(MYFLAGS) $(OTHER_STUFF) gcc $(MYFLAGS)$(OTHER_STUFF)

在Makefile中使用相同的编译标志和define标志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM