简体   繁体   English

在宏中连接字符串 - C ++

[英]Concatenating strings in macros - C++

What's the easiest way to concatenate strings defined in macros. 连接宏中定义的字符串的最简单方法是什么。 ie The pseudo code I'm looking for would be like: 即我正在寻找的伪代码将是:

#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH+"data/inputA.bin"
#define INPUT_FILE_B ROOT_PATH+"data/inputB.bin"
...
#define INPUT_FILE_Z ROOT_PATH+"data/inputZ.bin"

The only way I know of is to use strcat in the code, or using the string class and then the c_str method, but it can get messy when I have lots of input files. 我知道的唯一方法是在代码中使用strcat,或者使用字符串类然后使用c_str方法,但是当我有大量输入文件时它会变得混乱。 I'd like to just use INPUT_FILE_A, etc. directly and not have lots of local variables. 我想直接使用INPUT_FILE_A等,而不是有很多局部变量。 Is there a good way to do this? 有没有办法做到这一点?

Thanks. 谢谢。

The compiler will automatically concatenate adjacent strings: 编译器将自动连接相邻的字符串:

#define ROOT_PATH "/home/david/"
#define INPUT_FILE_A ROOT_PATH "data/inputA.bin"

Or more generic: 或者更通用:

#define INPUT_FILE_DETAIL(root,x) root #x
#define INPUT_FILE(x) INPUT_FILE_DETAIL(ROOT_PATH "data/", x)

Shell was "eating" the quotes. 壳牌正在“吃掉”报价。 So, the following line had to use: 所以,以下行必须使用:

-DROOT_PATH=\"some-string"\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM