代码之家  ›  专栏  ›  技术社区  ›  Alex F

-D选项从g++命令行扩展不正确

  •  3
  • Alex F  · 技术社区  · 14 年前

    在C++ CODBROCK项目中,我将以下定义添加到项目设置、编译器设置、γ定义:

    _DEBUG
    DATA_DIR=\"/media/Shared/SiX/Data\"
    

    这将生成以下g++命令行:

    g++ -Wall  -g -fPIC -save-temps -D_DEBUG -DDATA_DIR=\"/media/Shared/SiX/Data\"    -I../Includes  -c /media/Shared/SiX/SiXConfiguration/PathManager.cpp -o obj/Debug/PathManager.o
    

    char* commonDataDir;
    #ifdef DATA_DIR
    commonDataDir = DATA_DIR;
    #endif
    

    查看预处理器输出文件,我发现源代码行是这样展开的:

    commonDataDir = /media/Shared/SiX/Data;
    

    我希望:

    commonDataDir = "/media/Shared/SiX/Data";
    

    从Eclipse CDT正确编译了相同的代码:

    g++ -D_DEBUG -DDATA_DIR=\"/media/Shared/SiX/Data\" -I"/media/Shared/SiX (copy)/Includes" -O3 -Wall -c -fmessage-length=0 -fPIC -ggdb -MMD -MP -MF"PathManager.d" -MT"PathManager.d" -o"PathManager.o" "../PathManager.cpp"
    

    因此,相同的命令行参数由g++预处理器以不同的方式处理。我该怎么解决?

    5 回复  |  直到 8 年前
        1
  •  4
  •   Loki Astari    14 年前

    在宏上加引号很棘手,不是个好主意。

    #define DO_QUOTE(X)       #X
    #define QUOTE(X)          DO_QUOTE(X)
    
    #ifndef DATA_DIR
    #define DATA_DIR       /tmp
    #endif
    
    char commonDataDir[] = QUOTE(DATA_DIR);
    
        2
  •  2
  •   Meinersbur    14 年前

    在代码::块中

    DATA_DIR=\\"/media/Shared/SiX/Data\\"
    

    (这不只是猜测,而是我经常做的事情)

        3
  •  0
  •   codaddict    14 年前

    你需要把整根线都包起来 "

    -DDATA_DIR="\"/media/Shared/SiX/Data\""
               ^                          ^
    
        4
  •  0
  •   Bill Lynch    14 年前

    这似乎能解决问题。

    g++ -DDATA_DIR='"/media/Shared/SiX/Data"' ...
    
        5
  •  0
  •   diskerror    6 年前

    我刚用了一句类似于 -DDATA_DIR=\"/media/Shared/SiX/Data\" 在当前项目的make文件中,它运行得很好。(单反斜杠和双引号。)

    > uname -a
    Linux ... 3.16.0-5-amd64 #1 SMP Debian 3.16.51-3+deb8u1 (2018-01-08) x86_64 GNU/Linux
    > make -v
    GNU Make 4.0
    Built for x86_64-pc-linux-gnu
    > g++ -v
    Using built-in specs.
    COLLECT_GCC=g++
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.9/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: ...
    Thread model: posix
    gcc version 4.9.2 (Debian 4.9.2-10+deb8u1)