我正在尝试创建一个宏,该宏将makefile中的define create转换为自动生成的defines列表,defines是外围组件的起始地址。以下是测试代码:

#include <stdio.h>
#include <stdint.h>

/* Created by makefile to specify the core/CPU */
#define  CPU_ID       5

/* Autogenerated hardware register descriptors */
#define CPU0_PERIPHERAL_I2C_BASE__ADDR         (0x40000)
#define CPU1_PERIPHERAL_I2C_BASE__ADDR         (0x56000)
#define CPU2_PERIPHERAL_I2C_BASE__ADDR         (0x57000)
#define CPU3_PERIPHERAL_I2C_BASE__ADDR         (0x61000)
#define SUBCORE0_PERIPHERAL_I2C_BASE__ADDR     (0x67000)
#define SUBCORE1_PERIPHERAL_I2C_BASE__ADDR     (0x90000)
#define SUBCORE2_PERIPHERAL_I2C_BASE__ADDR     (0xA3000)
#define SUBCORE3_PERIPHERAL_I2C_BASE__ADDR     (0xE3000)

#define BASE_ADDR_DEF     _PERIPHERAL_I2C_BASE__ADDR

/* defines for translating CPU_ID into autogenerated hardware regs */
#define CPU_ID_0       "CPU0"
#define CPU_ID_1       "CPU1"
#define CPU_ID_2       "CPU2"
#define CPU_ID_3       "CPU3"
#define CPU_ID_4       "SUBCORE0"
#define CPU_ID_5       "SUBCORE1"
#define CPU_ID_6       "SUBCORE2"
#define CPU_ID_7       "SUBCORE3"

#define _JOIN(x,y)       x ## y
#define _DEF1(cpu_id)    _JOIN(CPU_ID_,cpu_id)
#define _DEF2(cpu_id)    _JOIN(_DEF1(cpu_id),BASE_ADDR_DEF)

int main(void)
{
    printf("%s\n", _DEF1(CPU_ID));  // <- this prints out "SUBCORE0" which is part of the way there
    printf("%X\n", _DEF2(CPU_ID));  // <- This will not compile
    return 0;
}

在本例中,输出端的DEF1宏类型将如我所料是“SUBCORE1”。我似乎无法让宏向上转换一个额外的级别,以将“SUBCORE1”与“SUBCORE1”与“SUBCORE1”结合起来,创建“SUBCORE1”和“SUBCORE1”然后打印出十六进制值,该值应为0x90000。在扩展过程中,我是否丢失了另一个级别的宏,而这些宏我看不到或无法得到正确的结果?
编辑:我知道人们在评论文字字符串,我知道这并不是最终目标,但这只是实验中许多迭代步骤中的一个。我只是需要一些例子来说明我想做什么。谢谢。

最佳答案

声明宏不是字符串文本。就像弦一样。将ex."SUBCORE3"更改为SUBCORE3。预处理器无法删除"
需要使用嵌套宏触发展开。例如#define _JOIN2(x, y) x ## y然后#define _JOIN(x, y) _JOIN2(x, y)
所有以下划线和大字母开头的标识符都由标准保留。声明它们是未定义的行为。

#include <stdio.h>
#include <stdint.h>

/* Created by makefile to specify the core/CPU */
#define  CPU_ID       5

/* Autogenerated hardware register descriptors */
#define CPU0_PERIPHERAL_I2C_BASE__ADDR         (0x40000)
#define CPU1_PERIPHERAL_I2C_BASE__ADDR         (0x56000)
#define CPU2_PERIPHERAL_I2C_BASE__ADDR         (0x57000)
#define CPU3_PERIPHERAL_I2C_BASE__ADDR         (0x61000)
#define SUBCORE0_PERIPHERAL_I2C_BASE__ADDR     (0x67000)
#define SUBCORE1_PERIPHERAL_I2C_BASE__ADDR     (0x90000)
#define SUBCORE2_PERIPHERAL_I2C_BASE__ADDR     (0xA3000)
#define SUBCORE3_PERIPHERAL_I2C_BASE__ADDR     (0xE3000)

#define BASE_ADDR_DEF     _PERIPHERAL_I2C_BASE__ADDR

/* defines for translating CPU_ID into autogenerated hardware regs */
#define CPU_ID_0       CPU0
#define CPU_ID_1       CPU1
#define CPU_ID_2       CPU2
#define CPU_ID_3       CPU3
#define CPU_ID_4       SUBCORE0
#define CPU_ID_5       SUBCORE1
#define CPU_ID_6       SUBCORE2
#define CPU_ID_7       SUBCORE3

// usually those are called CONCAT or CONCATX or CONCAT2 or XCONCAT etc.
// so I name them the same here
// XCONCAT is a mnemonic from "eXpand then CONCATenate"
#define CONCAT(x,y)     x ## y
#define XCONCAT(x,y)    CONCAT(x, y)
#define STRING(x)        #x
#define XSTRING(x)       STRING(x)
// note that glibc defines __CONCAT __XCONCAT __STRING __XSTRING

// trigger the expansions
#define DEF1(cpu_id)    XCONCAT(CPU_ID_, cpu_id)
#define DEF2(cpu_id)    XCONCAT(DEF1(cpu_id),BASE_ADDR_DEF)

int main(void)
{
    printf("%s\n", XSTRING(DEF1(CPU_ID)));  // <- this prints out "SUBCORE0", which is at all not part the way here
    printf("%X\n", DEF2(CPU_ID));  // <- This will expand to 0x90000
    return 0;
}

关于c - 嵌套预处理器等同于#define,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57589160/

10-13 00:41