连接并字符串化#include的宏值 [英] Concatenate and stringize macro values for #include

查看:60
本文介绍了连接并字符串化#include的宏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从多个宏/值创建一个字符串,以供在 #include 中使用.我这样做是为了在简单状态系统中为初始状态清除一些代码.

I'm trying to create a string from multiple macros/values for use in a #include. I'm doing this to clean up some code for an initial state in a simple state system.

我有2个默认的可重定义宏(如果未定义,则有一个默认值)

I have 2 default, redefinable macros (if not defined there's a default value)

   #define DEFAULT_STATE          StateName    // name of class
   #define DEFAULT_STATE_LOCATION states/      // location of header file from root

正在从 root 的4个文件夹中使用文件的include指令,因此include应该看起来像这样

The include directive is being used from a file 4 folders in from the root, so the include should look like this

#include "../../../../states/StateName.h"

基于上面的示例.

所以我想从4个值中定义一个宏.

So I want to define a macro from the 4 values.

../../../../
DEFAULT_STATE_LOCATION
DEFAULT_STATE
.h

进入某个宏,例如 DEFAULT_STATE_INCLUDE

所以我可以说

#include #DEFAULT_STATE_INCLUDE
(to stringize the value for quotes)

这样,我可以更改默认状态以及从前缀文件的根文件的根开始到前缀头中的默认状态的路径,并且无需更新使用 #include 的源,并且每次都可以省略重新定义的常量.

That way I can change the default state and the path from the root of the header file for the default state in a prefix header, and the source using the #include will not have to be updated, and I can omit the constants from redefinition every time.

我没有在 DEFAULT_STATE 宏中包含 .h ,因为我使用相同的宏来创建默认状态的实例.

I'm not including the .h in the DEFAULT_STATE macro because I use the same macro to create and instance of the default state.

我尝试使用串联 ## 和stringize运算符,以及一些我在网上找到的技巧,但没有用.

I've tried using the concatenation ##, and the stringize operator, and some tricks I've found online, but nothing worked.

如果需要,我可以在自己的宏中定义 ../../../../ .h .

I can define ../../../../ and .h in their own macros if needed.

但是简单

    #define DEFAULT_STATE_INCLUDE ../../../../ ## DEFAULT_STATE_LOCATION ## DEFAULT_STATE ## .h
    #include #DEFAULT_STATE_INCLUDE

产生大量错误.

感谢您的帮助.谢谢

推荐答案

请注意,C99标准的第6.10.2节包含源文件"中的脚注143表示:

Note that footnote 143 in section §6.10.2 "Source file inclusion" of the C99 standard says:

143)请注意,相邻的字符串文字不被连接成单个字符串文字(请参阅翻译5.1.1.2中的阶段);因此,导致两个字符串文字扩展的扩展是无效指令.

143) Note that adjacent string literals are not concatenated into a single string literal (see the translation phases in 5.1.1.2); thus, an expansion that results in two string literals is an invalid directive.

因此,任何字符串连接都必须在源代码之外完成.不能使用 ## 进行令牌串联;用来构建标识符,而您要加入的标头名称的位不是标识符.

Thus, any string concatenation must be done outside the source code. Token concatenation with ## is not an option; that is used to build identifiers, and the bits of the header name you are joining are not identifiers.

我认为您应该只使用以下内容:

I think you should simply use something like:

#ifndef STATE_HEADER
#define STATE_HEADER "states/StateName.h"
#endif
#include STATE_HEADER

并将其留给构建系统(而不是源代码),以确定是否需要 -I ../../../..作为编译器的选项.

and leave it to the build system, not the source code, to determine that -I../../../.. is required as an option to the compiler.

这篇关于连接并字符串化#include的宏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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