字符串内的 GCC 宏扩展参数
我遇到这样的情况,
#define PRE 0xF1
#define SR0 0B0000
#define SR1 0B0001
#define SR2 0B0010
#define SR3 0B0011
#define VIOTA(A0) asm(".byte PRE, A0")
int main()
{
VIOTA(SR1);
return 0;
}
我有一个扩展的顶级宏,但是扩展包含更多宏。这些没有被扩展并导致一些问题。
我想要的行为是最终扩展为
asm(".byte 0xF1, 0B0000")
这里内部宏已被扩展。我真的不确定我做错了什么。有什么建议吗?
I have a situation like this
#define PRE 0xF1
#define SR0 0B0000
#define SR1 0B0001
#define SR2 0B0010
#define SR3 0B0011
#define VIOTA(A0) asm(".byte PRE, A0")
int main()
{
VIOTA(SR1);
return 0;
}
I have a top-level macro that expands out however the expansion contains more macros. These aren't being expanded and causing some problems.
The behaviour that I desire is that the end expansion is
asm(".byte 0xF1, 0B0000")
Here the inner macros have been expanded. I'm really not sure what I'm doing wrong at all. Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅此处的详细信息:C 预处理器,字符串化宏的结果
See details here: C Preprocessor, Stringify the result of a macro
使用字符串化运算符
#
将标记转换为字符串。但是,由于字符串化运算符只能应用于宏参数,因此您需要添加一些额外的宏层:在预处理之后,这会扩展为:
字符串常量在编译时连接,因此这相当于
asm( “.byte 0xF1,0B0001”);
。Use the stringizing operator
#
to convert tokens into strings. However, since the stringizing operator can only be applied to macro parameters, you need to add some extra layers of macros:After preprocessing, this expands to this:
String constants get concatenated at compile time, so this is equivalent to
asm(".byte 0xF1, 0B0001");
.您正在将 PRE 作为传递给 asm() 的字符串的一部分。字符串中的宏不会展开。
这似乎对我有用:
You are making PRE part of the string that is passed to asm(). Macros in strings are not expanded.
This seems to work for me: