在C ++预处理器中为参数添加引号 [英] Adding quotes to argument in C++ preprocessor

查看:156
本文介绍了在C ++预处理器中为参数添加引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个包含文件的名称作为编译器参数,以便我可以修改大量的配置参数。但是,我的C ++构建是通过一个makefile类似的过程,从传递给编译器和预处理器的参数中删除引号。我希望做相当于

I'd like to pass the name of an include file as a compiler argument so that I can modify a large number of configuration parameters. However, my C++ build is via a makefile like process that removes quotes from arguments passed to the compiler and pre-processor. I was hoping to do something equivalent to

#ifndef FILE_ARG
// defaults
#else
#include "FILE_ARG"
#endif

我的命令行包括-DFILE_ARG = foo.h.这当然不工作,因为预处理器不翻译FILE_ARG。

with my command line including -DFILE_ARG=foo.h. This of course doesn't work since the preprocessor doesn't translate FILE_ARG.

我试过了

#define QUOTE(x) #x
#include QUOTE(FILE_ARG)


b $ b

因为同样的原因不起作用。

which doesn't work for the same reason.

由于脚本的原因,我宁愿在命令行上做这个,在适当的例程中包含一行。有什么办法吗?

For scripting reasons, I'd rather do this on the command line than go in and edit an include line in the appropriate routine. Is there any way?

推荐答案

添加引号需要这个技巧:

For adding quotes you need this trick:

#define Q(x) #x
#define QUOTE(x) Q(x)

#ifdef FILE_ARG
#include QUOTE(FILE_ARG)
#endif

这篇关于在C ++预处理器中为参数添加引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆