简体   繁体   中英

Multi-Argument macro definition error

I defined this macro:

#define DEF_CONCAT(a, b)        a ## b
#define _internal_RCC(gpio, io) DEF_CONCAT(RCC_GPIO, gpio)
#define _internal_IO(gpio, io)  DEF_CONCAT(GPIO, io)

#define IO_CFG_OUTPUT(gpio) {rcc_periph_clock_enable(_internal_RCC(gpio));gpio_set_mode(gpio, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, _internal_IO(gpio));}

And I want to call this with:

IO_CFG_OUTPUT(LED_STATE);

LED_STATE is defined as: #define LED_STATE C,12

But when I run my program, the compiler tells me:

In file included from inc/Robot.hpp:6:0,
                 from src/Robot.cpp:1:
src/Robot.cpp: In member function 'void Robot::setup()':
inc/IODefines.hpp:13:19: error: 'C' was not declared in this scope
 #define LED_STATE C,12
                   ^

What did I do wrong?

Run gcc -E on the source to see what your macros expand to...

What it expands to is {rcc_periph_clock_enable(RCC_GPIOC);gpio_set_mode(C,12, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);}; . Or with more readable formatting:

{
    rcc_periph_clock_enable(RCC_GPIOC);
    gpio_set_mode(C,12, 
                  GPIO_MODE_OUTPUT_50_MHZ, 
                  GPIO_CNF_OUTPUT_PUSHPULL, 
                  GPIO12);
};

Note the questionable practice of putting a semi-colon after the } , it could result in surprising behavior. The normal way to expand to a compound statement that works as a normal is to use do { body; } while(0) do { body; } while(0)

The problem is that C is not defined anywhere.

The error message looks a bit confusing though. There's nothing formally wrong with your define directive. Whether they do what you want on the other hand I cannot tell, you should be able to tell that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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