c - 在构建时生成非字符串化文本文本宏

标签 c gcc macros c-preprocessor

背景

In a separate question of mine ,我创建了一个类似函数的宏,它允许我连接用户提供的文本文字来创建宏名称,即:

/******************************************************************************
 * coconut.h
 ******************************************************************************/
#define COCONUT_FX_REGISTER (100)
#define COCONUT_BASE_REGISTER   (101)

/*******************************************************************************
 * pineapple.h
 ******************************************************************************/
#define PINEAPPLE_FX_REGISTER   (200)
#define PINEAPPLE_BASE_REGISTER (201)

/*******************************************************************************
 * test.c.
 ******************************************************************************/
#include <stdio.h>
#include "translation.h"
#include "coconut.h"
#include "pineapple.h"

int main(void) {

    int i = getTranslation(FX_REGISTER, COCONUT);
    printf("Translation:%d.\n", i);

    return 0;
}

/*******************************************************************************
 * translation.h
 ******************************************************************************/
#define getTranslation(x, y)  y ## _ ## x

目标

我想扩展这个逻辑,以便我可以使用宏作为默认值传递给 getTranslation,即:

#define XFRM(x)         #x
#define XFRM2(x)        XFRM(x)
#define DEFAULT_PRODUCT     XFRM2(COCONUT)

int main(void) {

    int i = getTranslation(FX_REGISTER, DEFAULT_PRODUCT);
    printf("Translation:%d.\n", i);

    return 0;
}

问题

但是,我似乎无法将 DEFAULT_PRODUCT 转换为非字符串文本文字。


构建错误

main.c: In function ‘main’:
main.c:14:35: error: ‘DEFAULT_PRODUCT_FX_REGISTER’ undeclared (first use in this function)
  printf("%d\n", getTranslation(FX_REGISTER, DEFAULT_PRODUCT));
                                   ^
translation.h:33:25: note: in definition of macro ‘getTranslation’
 #define getTranslation(x, y) y ## _ ## x
                         ^
main.c:14:35: note: each undeclared identifier is reported only once for each function it appears in
  printf("%d\n", getTranslation(FX_REGISTER, DEFAULT_PRODUCT));
                                   ^
translation.h:33:25: note: in definition of macro ‘getTranslation’
 #define getTranslation(x, y) y ## _ ## x

问题

如何创建解析为非字符串文本文字的 DEFAULT_PRODUCT 宏,以便我可以创建与 getTranslation 一起使用的“默认”值?这是使用设置为 C99 迂腐的 GCC。

最佳答案

听起来像是 XY 问题。

宏串联似乎是与宏文字扩展同时处理的,因此恐怕无法创建在 getTranslation 之前扩展的 DEFAULT_PRODUCT 宏。

我的建议:创建另一个宏函数getDefaultTranslation(x),您将轻松实现您想要的。

// You may want to add appropriate comments
// so code reviewers know what this is doing.
#define getDefaultTranslation(x) COCONUT ## x

关于this question ,宏扩展是逐层进行的,并且在同一层串联具有更高的优先级,因此添加另一层应该可以。请参阅ringø's answer下面。

关于c - 在构建时生成非字符串化文本文本宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47445887/

相关文章:

c++ - 如何在启动阶段捕获屏幕(当 Autochk 运行时)?

ios - Clang GCC 扩展 - 括号中的 block 返回一个值

c - 我如何不在终端中输入 gcc 和 MacOSX sdk 的完整路径?

C++ 或宏魔法来生成方法和转发参数

c - 区分#define宏中的变量和常量

c - Mac OS X 上的线程,强制使用多个 CPU

c - 读取文本文件的函数

c - 来自 C 程序的汇编代码

c - 即使重命名符号后全局变量的地址也不会改变

c++ - 我可以将符号数组传递给宏吗?