在C源代码中接收makefile参数
我想这样调用一个makefile:make uj LOCATION=my_room,并在打印输出中显示my_room 例如:在C源代码中接收makefile参数,c,makefile,C,Makefile,我想这样调用一个makefile:make uj LOCATION=my_room,并在打印输出中显示my_room 例如: print(" LOCATION is %s \n", LOCATION); 应该打印的位置是我的房间。如果未给出位置参数,则应打印未定义 我试过以下几点。 在我的makefile中: ifndef LOCATION LOCATION = "undefined" endif CFLAGS += -DLOCATION=$(LOCATION) 然后调用
print(" LOCATION is %s \n", LOCATION);
应该打印的位置是我的房间。如果未给出位置参数,则应打印未定义
我试过以下几点。
在我的makefile中:
ifndef LOCATION
LOCATION = "undefined"
endif
CFLAGS += -DLOCATION=$(LOCATION)
然后调用上面的print函数,但这会引发编译错误。我错过了一些东西。你能帮忙吗
编辑:
在编译期间添加了错误
In file included from ../../uJ/cerberOS_BSP.h:4:0,
from ../../uJ/main_upnp.c:3:
../../uJ/main_upnp.c: In function ‘printDiagnostics’:
../../uJ/cerberOS_debug.h:15:59: error: expected expression before ‘)’ token
#define print(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
^
../../uJ/main_upnp.c:35:2: note: in expansion of macro ‘print’
print(" LOCATION (as s) is %s \n", LOCATION);
字符串的引号丢失,因此它将扩展为
打印(“位置为%s\n”,未定义)代码>
尝试:
CFLAGS+=-DLOCATION=\“$(位置)\”
这将扩展到
打印(“位置为%s\n”,“未定义”)代码>
这就是你想要的。你说的“错误”是什么意思?如果您有生成错误,请在问题正文中包含生成日志(完整、完整且未经编辑)。最简单的调试方法是将源代码通过预处理器,然后查看在该位置生成的内容。抱歉,在编译过程中添加了错误。与您的问题无关,但您使用宏和自定义函数进行打印的原因是什么?这会降低代码的可维护性,使其更难阅读和理解。我认为,问题在于如何使用。