C语言 如何在我的 Make 文件中定义 #define
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2136904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I define #define in my Make files
提问by n179911
In my c/c++ files, there are multiple #define. As an example:
在我的 c/c++ 文件中,有多个 #define。举个例子:
#ifdef LIBVNCSERVER_HAVE_LIBZ
/* some code */
#ifdef LIBVNCSERVER_HAVE_LIBJPEG
/* some more code */
Can you please tell me how can I modify my Makefile.in so that I have those #define in ALLfiles during compilation?
你能告诉我如何修改我的 Makefile.in 以便我在编译期间在所有文件中都有那些 #define吗?
Thank you.
谢谢你。
回答by YOU
-DLIBVNCSERVER_HAVE_LIBZ -DLIBVNCSERVER_HAVE_LIBJPEG
You could pass those in CPPFLAGS,
你可以通过那些CPPFLAGS,
CPPFLAGS = -DLIBVNCSERVER_HAVE_LIBZ -DLIBVNCSERVER_HAVE_LIBJPEG
or make new variable
或创建新变量
CUSTOMDEFINES = -DLIBVNCSERVER_HAVE_LIBZ -DLIBVNCSERVER_HAVE_LIBJPEG
and pass it to CPPFLAGS = -DEXISTINGFLAGS $(CUSTOMDEFINES)
并将其传递给 CPPFLAGS = -DEXISTINGFLAGS $(CUSTOMDEFINES)
Those are finally will pass to gcc/g++ -D...
那些最终会传递给 gcc/g++ -D...
$(CC) $(CPPFLAGS)
回答by t0mm13b
Add the line below, to your makefile:
将下面的行添加到您的 makefile 中:
DEFINES=LIBVNCSERVER_HAVE_LIBZ LIBVNCSERVER_HAVE_LIBJPEG
...
... further on in your makefile on the line where it says ....
...
$(cc) ($(addprefix -D, $(DEFINES))) .....
...
...
This is to serve as an example, you only add another define to the DEFINESvariable which gets referenced on the line as shown $(cc) -D$(DEFINES)in which the make will automatically expand the variable and compile those that are #ifdefd.
这是作为示例,您只需DEFINES向在行上引用的变量添加另一个定义,如图所示$(cc) -D$(DEFINES),make 将自动扩展变量并编译那些是#ifdefd的变量。
Thanks to R Samuel Klatchkofor pointing out a small amiss...this is specifically for GNU's make, you can use addprefix do properly do that ($(addprefix -D, $(DEFINES))).
感谢R Samuel Klatchko指出了一个小错误......这是专门针对 GNU 的 make,您可以使用 addprefix 正确地做到这一点 ($(addprefix -D, $(DEFINES)))。
回答by William Pursell
Don't modify your Makefile.in. (and consider using Automake and converting your Makefile.in to a much simpler Makefile.am). The whole point of those #defines is to let the configure script define them in config.h, and your source files should #include <config.h>. If you are maintaining the package, you will need to write tests in configure.ac to determine whether or not the system being used has libvncserver installed with jpeg and zlib support. If you modify Makefile.in to always define them, then you are making the assumption that your code is only being built on machines where those features are available. If you make that assumption, you should still add checks to configure.ac to confirm it, and have the configure script fail if the dependencies are not met.
不要修改你的 Makefile.in。(并考虑使用 Automake 并将您的 Makefile.in 转换为更简单的 Makefile.am)。这些#defines 的全部意义在于让配置脚本在 config.h 中定义它们,并且您的源文件应该 #include <config.h>。如果您正在维护包,您将需要在 configure.ac 中编写测试以确定正在使用的系统是否安装了支持 jpeg 和 zlib 的 libvncserver。如果您修改 Makefile.in 以始终定义它们,那么您就假设您的代码仅在具有这些功能的机器上构建。如果你做出这样的假设,你仍然应该向 configure.ac 添加检查以确认它,如果不满足依赖关系,则配置脚本失败。

