g++ 宏连接 vs c++ 宏连接

g++ macro concatenation vs c++ macro concatenation

我正在将一个基于 windows 的 c++ 项目移植到 windows 中,我 运行 出现以下错误。

UeiDaqAnsiC.cpp:105:24
error: expected ')' before string constant
UEI_ERROR(__FUNCTION__ " error: %s\n", e.getErrorMessage());

看起来 linux 无法将 __FUNCTION__ 宏与以下字符串连接起来。这令人困惑,因为项目在 Windows 中编译和构建得很好。

作为快速修复,似乎只需在 __FUNCTION__"error: %s\n", e.getErrorMessage() 之间添加一个 , 即可完全修复它。

新的固定线路是这样的UEI_ERROR(__FUNCTION__, " error: %s\n", e.getErrorMessage());

我来这里是因为我不熟悉 linux g++ 编译器,我想知道这是否是解决此错误的有效方法,然后再修复发生此错误的所有 130 行。

编辑:我还想问问逗号是否保留了简单连接宏和字符串的功能

EDIT2:UEI_ERROR 定义为 #define UEI_ERROR(...) UeiPalTraceOutputWithLevel(UeiPalTraceError, __VA_ARGS__)

__FUNCTION__ 不是字符串文字,预处理器不能将其与其他字符串文字连接。

你的 "fix" 通过添加 , 改变了意思,主要是

printf(__FUNCTION__ " format %i", 42);        // MyFunction format 42
printf("MyFunction", "unused format %i", 42); // MyFunction

真正的解决方法是更改​​格式和重新排序参数:

UEI_ERROR("%s error: %s\n", __FUNCTION__, e.getErrorMessage());// MyFunction error: error message.