命令行未正确扩展

命令行未正确扩展

本文介绍了-D选项从g ++命令行未正确扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ CodeBlocks项目中,我将以下定义添加到项目设置,编译器设置,#define:

 
_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(复制)/包含-O3 -Wall -c -fmessage-length = 0 -fPIC -ggdb-MMD -MP -MFPathManager.d-MTPathManager.d-oPathManager.o../PathManager.cpp

因此,相同的命令行参数由g ++处理器以不同的方式处理。如何解决这个问题?

解决方案

将引号引入宏是很棘手的,不是一个好主意。

尝试使用预处理器添加所需的引号。

  #define DO_QUOTE(X)#X 
#定义QUOTE(X)DO_QUOTE(X)

#ifndef DATA_DIR
#define DATA_DIR / tmp
#endif

char commonDataDir [] = QUOTE DATA_DIR);


In C++ CodeBlocks project I added the following definitions to the project settings, compiler settings, #define:

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

This produces the following g++ command line:

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

This code does not compile:

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

Looking at preprocessor output file, I see that source code line is expanded in this way:

commonDataDir = /media/Shared/SiX/Data;

I expect:

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

The same code is compiled correctly from 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"

So, the same command line parameter is handled differently by g++ proprocessor. How can I fix this?

解决方案

Putting quotes on macros is tricky and not a good idea.
Try and use the pre-processor to add the required quotes.

#define DO_QUOTE(X)       #X
#define QUOTE(X)          DO_QUOTE(X)

#ifndef DATA_DIR
#define DATA_DIR       /tmp
#endif

char commonDataDir[] = QUOTE(DATA_DIR);

这篇关于-D选项从g ++命令行未正确扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:51