【问题标题】:Macro wrongly defined in Makefile of Openwrt packageOpenwrt包的Makefile中错误定义的宏
【发布时间】:2018-06-20 16:48:09
【问题描述】:

在我正在处理的 openwrt 包中,我通过将以下块添加到 Config.in 文件来定义一个新的配置标志:

config VENDOR_PREFIX
    string "Vendor Prefix"
    default "X_Custom_SE_"

flag 已经很好地添加到 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 程序不接受它作为字符串。 我的宏定义有问题吗?

【问题讨论】:

  • 不使用\" 试试?也许CONFIG_VENDOR_PREFIX 已经是一个字符串了。
  • @EugeneSh。不,它不起作用。同样的问题
  • 好吧,您可以通过#define STR(x) #x 然后STR(CUSTOM_PREFIX) 来解决它。但它不会解释为什么这不起作用。
  • @EugeneSh。如果我使用您的建议,很好,我不会收到编译错误。但我在执行中没有得到好的结果。因为在 c 代码中,结构初始化将是 struct parameter_struct param1= {STR(CUSTOM_PREFIX)"param1", 4};所以我得到字符串“CUSTOM_PREFIXparam1”,但我想要它 X_CUSTOM1_SE_param1

标签: c makefile c-preprocessor openwrt


【解决方案1】:

正如我所料,我在帖子标题中指出,错误出在 Makefile 中宏的定义中。在 Openwrt Makefile 中宏的定义应该是这样的:

TARGET_CFLAGS += -DCUSTOM_PREFIX=\\\"$(CONFIG_VENDOR_PREFIX)\\\"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多