替代标题(以帮助搜索)
- 将预处理器标记转换为字符串
- 我怎样才能从一个字符串C宏的价值?
原始问题
我想用C #define
在编译时构建文字字符串。
该字符串是因调试、发布等而更改的域。
我想做这样的事情:
#ifdef __TESTING
#define IV_DOMAIN example.org // In house testing
#elif __LIVE_TESTING
#define IV_DOMAIN test.example.com // Live testing servers
#else
#define IV_DOMAIN example.com // Production
#endif
// Subdomain
#define IV_SECURE "secure.IV_DOMAIN" // secure.example.org, etc.
#define IV_MOBILE "m.IV_DOMAIN"
但预处理器不会评估“”内的任何内容
- 有没有解决的办法?
- 这是个好主意吗?
在 C 中,字符串文字会自动连接。例如,
const char * s1 = "foo" "bar";
const char * s2 = "foobar";
s1
and s2
是同一个字符串。
所以,对于你的问题,答案(没有标记粘贴)是
#ifdef __TESTING
#define IV_DOMAIN "example.org"
#elif __LIVE_TESTING
#define IV_DOMAIN "test.example.com"
#else
#define IV_DOMAIN "example.com"
#endif
#define IV_SECURE "secure." IV_DOMAIN
#define IV_MOBILE "m." IV_DOMAIN
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)