C 宏 - 动态 #include [英] C Macro - Dynamic #include

查看:20
本文介绍了C 宏 - 动态 #include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用 GCC 为 #include 语句构建变量字符串.

I'm trying to figure out how to build a variable string for the #include statement using GCC.

我的想法是,对于我编写的每个源模块,我都希望将在构建过程早期创建的动态生成的 C 源作为标头包含在内.

The idea is that for each source module I have written, I want to include as a header, a dynamically generated C source, that was created earlier in the build process.

生成此文件不是问题.不幸的是,包括它.

Generating this file is not an issue. Including it, unfortunately, is.

到目前为止我所拥有的是(identities.h):

What I have so far is (identities.h):

// identities.h

# define PASTER2(str)  #str
# define PASTER(str)   PASTER2(str ## .iden)
# define EVALUATOR(x)  PASTER(x)

# define IDENTITIES_FILE EVALUATOR(__FILE__)
# include IDENTITIES_FILE

理想情况下,这将像这样使用(main.c):

Ideally, this would be used like so (main.c):

//main.c

# include "identities.h"

int main() {return 0;}

在编译到 yield 之前,预处理器将在一次传递中对其进行扩展:

Which would be expanded in a single pass by the preprocessor before compilation to yield:

//main.c (preprocessed)

# include "main.c.iden"

int main() {return 0;}

我使用的两个间接级别(PASTER 和 EVALUATOR)是 这个帖子.

The two levels of indirection I'm using (PASTER and EVALUATOR) are a result of this post.

不幸的是,这不起作用,我留下了错误:

Unfortunately, this is not working and I am left with the error:

obj/win32/dbg/main.o
In file included from main.c:1:0:
identities.h:42:1: error: #include expects "FILENAME" or <FILENAME>

我认为问题在于 include 语句缺少引号.有什么想法吗?

I think the problem is that the include statement is missing quotes.. Any ideas?

推荐答案

Boost 预处理器库 .它专门用于在名称周围添加引号.

What about BOOST_PP_STRINGIZE from the Boost Preprocessor library . It is specifically made to add quotes around a name.

这篇关于C 宏 - 动态 #include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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