问题描述
我通常使用以下选项将宏定义从make command line"传递到makefile":-Dname=值.该定义可在 makefile 中访问.
I usually pass macro definitions from "make command line" to a "makefile" using the option :-Dname=value. The definition is accessible inside the makefile.
我还使用类似的编译器选项将宏定义从makefile"传递到源代码":-Dname=value(许多编译器都支持).此定义可在源代码中访问.
I also pass macro definitions from the "makefile" to the "source code" using the similar compiler option :-Dname=value (supported in many compilers). This definition is accessible in the source code.
我现在需要的是让我的 makefile 的用户能够立即将任意宏定义从make.exe 命令行"传递到源代码",而无需更改 makefile 中的任何内容.
What I need now, is to allow the user of my makefile to be able to pass arbitrary macro definitions from the "make.exe commandline" to "source code" right away, without having to change anything in the makefile.
所以用户可以输入:make -f mymakefile.mk -SOMEOPTION var=5
so the user can type :make -f mymakefile.mk -SOMEOPTION var=5
那么直接main.c代码就可以看到var:
then directly the code main.c can see var :
int main()
{
int i = var;
}
推荐答案
这样调用make
命令:
make CFLAGS=-Dvar=42
并确保在 Makefile 的编译命令中使用 $(CFLAGS)
.正如@jørgensen 提到的,将变量赋值放在 make
命令之后将覆盖 CFLAGS
值已经定义了 Makefile.
And be sure to use $(CFLAGS)
in your compile command in the Makefile. As @jørgensen mentioned , putting the variable assignment after the make
command will override the CFLAGS
value already defined the Makefile.
或者,您可以在 CFLAGS
之外的另一个变量中设置 -Dvar=42
,然后在 CFLAGS
中重用该变量以避免完全覆盖 CFLAGS
.
Alternatively you could set -Dvar=42
in another variable than CFLAGS
and then reuse this variable in CFLAGS
to avoid completely overriding CFLAGS
.
这篇关于如何从“make"传递宏定义?C源代码的命令行参数(-D)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!