简体   繁体   English

通过宏传递宏参数

[英]Passing macro arguments by macro

I'm trying to pass a RGB color into a macro that takes three arguments. 我正在尝试将RGB颜色传递给需要三个参数的宏。 A normal call would look like this: MACRO(255, 255, 255) The macro converts the arguments and together with an operator it is then sent over SPI to a graphics controller. 普通调用如下所示: MACRO(255, 255, 255)宏将转换参数,然后与运算符一起通过SPI发送到图形控制器。 A call in C code looks like this: cmd(MACRO(0,0,0)); 用C代码进行的调用如下所示: cmd(MACRO(0,0,0));

As the color is used on multiple occasions, but might be subject to change, i thought it might be a good idea to define it via another macro. 由于颜色多次使用,但可能会发生变化,我认为通过另一个宏定义颜色可能是一个好主意。 So defining a color eg #define BLACK 0,0,0 and inserting it into the other macro should be ok: MACRO( BLACK ) -> cmd(MACRO( BLACK )); 因此定义一个颜色,例如#define BLACK 0,0,0并将其插入另一个宏应该没问题: MACRO( BLACK ) -> cmd(MACRO( BLACK )); But the compiler outputs an error: 但是编译器输出错误:

macro "MACRO" requires 3 arguments, but only 1 given 宏“ MACRO”需要3个参数,但仅给出1个

Getting this i assume the macro providing the arguments is not being expanded. 得到这个我假设提供参数的宏没有被扩展。 I tried to figure out what was wrong reading through the gcc macro documentation, but was not able to sole it. 我试图找出通过阅读gcc宏文档有什么问题,但无法根除它。 I assume it is one of those stupid simple things... Thank you! 我认为这是那些愚蠢的简单事情之一……谢谢!

More info: There are multiple macros provided by a driver taking three numbers (rgb) as an argument. 更多信息:驱动程序提供了多个宏,它们以三个数字(rgb)作为参数。 I would like to define a color scheme to be able to change a color for every occurrence without changing more than those three numbers. 我想定义一种配色方案,以便能够为每次出现的情况更改颜色,而不会更改三个以上的数字。

The function where the macro is inserted is taking an unsigned long and that is what the macro is producing. 插入宏的函数占用的时间是无符号的,这就是宏所产生的。

Why is the term BLACK not simply substituted by 0,0,0? 为什么术语BLACK不能简单地用0,0,0代替?

Quote from the gcc macro documentation: 引用gcc宏文档:

"All arguments to a macro are completely macro-expanded before they are substituted into the macro body. After substitution, the complete text is scanned again for macros to expand, including the arguments." “宏的所有参数在被替换为宏主体之前,都已完全进行宏扩展。替换后,将再次扫描整个文本以查找宏以扩展,包括参数。”

You'd have to ensure that your combination MACRO(BLACK) evaluates the BLACK part once more. 您必须确保您的组合MACRO(BLACK)再次评估BLACK零件。 Otherwise it doesn't "see" the commas separating the zeros. 否则,它不会“看到”逗号分隔零。 What you could to is rename your existing MACRO to MACRO3 (it receives 3 arguments) and then have 您可以将现有的MACRO重命名为MACRO3 (它接收3个参数),然后具有

#define MACRO(...) MACRO3(__VA_ARGS__)

You may want to define your macro like this: 您可能需要这样定义宏:

#define MACRO(x, y, z) somestuffhere
#define BLACK MACRO(0,0,0)

And then call it like that 然后这样称呼它

cmd(BLACK);

This is a more logical implementation. 这是一个更合逻辑的实现。 If you define a macro that take 3 arguments, it should take 3 arguments and not one. 如果定义一个带有3个参数的宏,则它应带3个参数而不是一个。 However you can call macro inside another macro definition. 但是,您可以在另一个宏定义内调用宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM