c - Openwrt包的Makefile中错误定义的宏
问题描述
在我正在处理的 openwrt 包中,我通过将以下 bloc 添加到 Config.in 文件来定义一个新的配置标志:
config VENDOR_PREFIX
string "Vendor Prefix"
default "X_Custom_SE_"
该标志已很好地添加到 menuconfig 中:
我希望这个配置标志的值在我的 C 代码中被视为一个宏。所以我在包的 Makefile 中定义了一个宏 CUSTOM_PREFIX 并用这种方式为其分配了定义标志的值:
TARGET_CFLAGS += -DCUSTOM_PREFIX=\"$(CONFIG_VENDOR_PREFIX)\"
然后我尝试在我的 C 代码中使用我的宏,方法是在结构变量初始化中调用它,如下所示:
struct parameter_struct param1= {CUSTOM_PREFIX"param1", 4};
之后我尝试编译它。但是我得到了这个编译错误:
/home/user/openwrt//staging_dir/toolchain-mips_mips32_gcc-5.5.0_musl/usr/include -I/home/user/openwrt/staging_dir/toolchain-mips_mips32_gcc-5.5.0_musl/include/fortify -I/home/user/openwrt/staging_dir/toolchain-mips_mips32_gcc-5.5.0_musl/include -I/home/user/openwrt/staging_dir/target-mips_mips32_musl/usr/include -I/home/user/openwrt/staging_dir/target-mips_mips32_musl/usr/include -I/home/user/openwrt/staging_dir/target-mips_mips32_musl/usr/include -DCWMP_VERSION=\"3.0.0\" -I../inc/ -I../dm/ -I../dm/dmtree/ -I../dm/dmtree/common -I../dm/dmtree/tr098 -I../dm/dmtree/tr181 -I../dm/dmtree/upnp -Os -pipe -mips32 -mtune=mips32 -fno-caller-saves -DCONFIG_TARGET_iopsys_brcm63xx_mips -g3 -fno-caller-saves -fno-plt -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result -msoft-float -iremap/home/user/openwrt/build_dir/target-mips_mips32_musl/icwmp-curl/icwmp-4.0-2018-03-21:icwmp-4.0-2018-03-21 -Wformat -Werror=format-security -fstack-protector -D_FORTIFY_SOURCE=1 -Wl,-z,now -Wl,-z,relro -DCUSTOM_PREFIX=X_CUSTOM1_SE_ -D_GNU_SOURCE -D_AADJ -MT ../dm/dmtree/common/libdatamodel_la-deviceinfo.lo -MD -MP -MF ../dm/dmtree/common/.deps/libdatamodel_la-deviceinfo.Tpo -c ../dm/dmtree/common/deviceinfo.c -fPIC -DPIC -o ../dm/dmtree/common/.libs/libdatamodel_la-deviceinfo.o
^
../dm/dmtree/common/deviceinfo.c: At top level:
<command-line>:0:15: error: 'X_CUSTOM1_SE_' undeclared here (not in a function)
../dm/dmtree/common/deviceinfo.c:28:2: note: in expansion of macro 'CUSTOM_PREFIX'
{CUSTOM_PREFIX"param1", 4}
struct parameter_struct param1= {CUSTOM_PREFIX"param1", 4}; 似乎 c 程序不接受它作为字符串。我的宏定义有问题吗?
解决方案
正如我所料,我在帖子标题中指出,错误出在 Makefile 中宏的定义中。在 Openwrt Makefile 中,宏的定义应该是这样的:
TARGET_CFLAGS += -DCUSTOM_PREFIX=\\\"$(CONFIG_VENDOR_PREFIX)\\\"
推荐阅读
- node.js - raise ValueError("Unsupported Lambda runtime {}".format(runtime))
- xml - DOMDocument - 创建书籍、加载章节和追加
- javascript - 从另一个文件绑定函数以响应类组件
- swift - 警卫错误让不快速停止 nil 值
- google-compute-engine - 使用 gcloud 命令行获取我的项目的配额使用/限制列表
- rest - 处理通过 Kafka 推送的消息
- sql - 查询以仅返回具有有效电子邮件的行并仅输出电子邮件
- django - Django - NoReverseMatch 在初始化表单时在创建视图中找不到参数
- php - 找不到类控制器
- c# - 在 MVC 中单击操作链接时发布到索引页面