【问题标题】:Macro compiles in C but not in C++ (MSP432 BSL invocation)宏在 C 中编译,但不在 C++ 中(MSP432 BSL 调用)
【发布时间】:2019-09-06 07:42:55
【问题描述】:

我正在尝试在 TI MSP432P401R 设备上调用 BSL(引导加载程序)。 以下宏在 C 中正确编译,但在使用 C++ 时失败,并出现错误“函数参数过多”。 C++ 预处理器/编译器有何不同?

/******************************************************************************
* BSL                                                                         *
******************************************************************************/
#define BSL_DEFAULT_PARAM                        ((uint32_t)0xFC48FFFF)          /*!< I2C slave address = 0x48, Interface selection = Auto */
#define BSL_API_TABLE_ADDR                       ((uint32_t)0x00202000)          /*!< Address of BSL API table */
#define BSL_ENTRY_FUNCTION                       (*((uint32_t *)BSL_API_TABLE_ADDR))

#define BSL_AUTO_INTERFACE                       ((uint32_t)0x0000E0000)         /*!< Auto detect interface */
#define BSL_UART_INTERFACE                       ((uint32_t)0x0000C0000)         /*!< UART interface */
#define BSL_SPI_INTERFACE                        ((uint32_t)0x0000A0000)         /*!< SPI interface */
#define BSL_I2C_INTERFACE                        ((uint32_t)0x000080000)         /*!< I2C interface */

#define BSL_INVOKE(x)                            ((void (*)())BSL_ENTRY_FUNCTION)((uint32_t) x) /*!< Invoke the BSL with parameters */

int main()
{
    BSL_INVOKE(BSL_UART_INTERFACE);
}

【问题讨论】:

    标签: c++ c embedded msp432


    【解决方案1】:

    在 C 中,void f() 类型的函数表示接受任何参数的函数 - 这是 C 中过时的样式,但仍然允许。

    在 C++ 中,void f() 表示等效于void f(void) 的函数,因此您不能向其传递任何参数。

    你不应该在 C 和 C++ 中使用这一行:

    ((void (*)())BSL_ENTRY_FUNCTION)((uint32_t) x)
    

    将它以及函数声明更改为:

    ((void (*)(uint32_t))BSL_ENTRY_FUNCTION)((uint32_t) x)
    

    【讨论】:

    • 不要错过正确 C++ 链接的‘extern "C"' 声明符
    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2013-05-03
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    相关资源
    最近更新 更多