gpt4 book ai didi

c - #define ISR(x) #pragma isr=x

转载 作者:太空宇宙 更新时间:2023-11-04 00:33:16 24 4
gpt4 key购买 nike

我对这种宏尝试有疑问:

#define ISR(x) #pragma isr=x

无法编译,因为它试图用不存在的参数替换 #pragma。有什么办法可以实现我想要做的事情吗?我想将 ISR(VEC1) 扩展为 #pragma isr=VEC1

最佳答案

是的。可以使用 _Pragma 一元运算符定义扩展为 pragma 指令的宏。

来自 C99 标准的 6.10.9:

A unary operator of the form: _Pragma ( string-literal ) is processed as follows: The string literal is destringized by deleting the L prefix, if present, deleting the leading and trailing double-quotes, replacing each escape sequence \" by a double-quote, and replacing each escape sequence \\ by a single backslash. The resulting sequence of characters is processed through translation phase 3 to produce preprocessing tokens that are executed as if they were the pp-tokens in a pragma directive. The original four preprocessing tokens in the unary operator expression are removed.

标准中的一个例子是:

#define LISTING(x) PRAGMA(listing on #x)
#define PRAGMA(x) _Pragma(#x)
LISTING ( ..\listing.dir )

相当于:

#pragma listing on "..\listing.dir"

因此,这就是您正在寻找的:

#define PRAGMA(x) _Pragma(#x)
#define ISR(x) PRAGMA(isr=x)

关于c - #define ISR(x) #pragma isr=x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4286715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com