qinxg 发表于 2021-11-30 10:16:53

cl.exe行编译如何定义string, 如 /Dmode="123"

代码如下:
#include <stdio.h>
int main()
{
    char* str = mode;
    printf( "%s \r\n", str );
    return 1;
}

使用MSVC++19的cl.exe编译:cl /Dmode="123" main.c
结果mode被定义为int. (使用gcc可以编译)
main.c(6): warning C4047: “初始化”:“char *”与“int”的间接级别不同

在MS网站上:/D has the same effect as a #define directive at the beginning of a source code file.
The difference is that /D strips quotation marks on the command line, and a #define directive keeps them.

折腾了几个小时, 都没有搞定, 晕了呀.

wye11083 发表于 2021-11-30 10:22:27

用\"123\"试试。估计是命令行被自动注释掉了。

qinxg 发表于 2021-11-30 11:00:11

本帖最后由 qinxg 于 2021-11-30 11:09 编辑

实验了各种:
/Dmode=\"123\"
/Dmode="\"123\""
/D"mode=\"123\""
/D"mode=123"
/Dmode#"123"
/D mode="123"   
/D mode=\"123\"
/D "\"mode=\"\"123\"\"\""
/D "mode=\"123\""
要么不能编译, 要么编译为整数, 只有在MS自己的集成编译器的属性菜单对话框里才能设置成功.MS不会这么难搞吧.

qinxg 发表于 2021-11-30 12:04:42

算了. 另外编了一个def.h,#define mode "123".使用/FI def.h 行编译实现了这个功能

wudicgi 发表于 2021-11-30 14:16:32

/Dmode=\"\\\"123\\\"\"

想了想,可能只剩下这个能再试试了,程序看到的参数值是 /Dmode="\"123\""

wudicgi 发表于 2021-11-30 14:25:11

https://docs.microsoft.com/en-us/cpp/build/reference/d-preprocessor-definitions?view=msvc-170
https://stackoverflow.com/questions/1305422/how-to-make-a-string-preprocessor-definition-from-command-line-in-vc-2005-c/1305471#1305471

/DDEBUG#\"abc\"

还有这种可以试
页: [1]
查看完整版本: cl.exe行编译如何定义string, 如 /Dmode="123"