假设我的 makefile 中有以下条目:
ifeq ($(MY_DEBUG),yes)
EXTRACXXFLAGS += -DMY_DEBUG=2
endif
我用 make foo MY_DEBUG=yes
调用它,现在我基本上已经
#define MY_DEBUG 2
适用于所有可用的项目文件。
问题:我可以通过这种方式在整个项目中定义宏函数,而不用 -include
或其他方式?
我的 makefile 中需要的是这样的内容:
/* not working code */
ifeq ($(MY_DEBUG),yes)
EXTRACXXFLAGS += -Ddbg_print(...)=printf(##__VA_ARGS__)
endif
通常我会在某个 header 中定义此宏函数,包括整个项目,但这个特定项目太大并且没有这样的 header 。
如果到处都包含头文件,那就太好了:
Makefile
# this is in makefile
ifeq ($(MY_DEBUG),yes)
EXTRACXXFLAGS += -DMY_DEBUG
endif
globally_included_header.h
// header, included all over the project
#ifdef MY_DEBUG
# define dbg_print(...) printf(##__VA_ARGS__)
#else
# define dbg_print(...)
#endif
最佳答案
您可以通过将其添加到 CXX 标志来将 header 从命令行插入到所有源:
-include "path/to/header.h"
编辑:
在您的特定情况下,您还可以将其传递给 CXX
标志以创建宏函数:
-D'dbg_print(...)=printf(__VA_ARGS__)'
在 Windows 上,它对我有用 "
而不是 '
关于c++ - 从 makefile 定义宏函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30651374/