我有一个 cpp 代码,我想在其中调用一个 c 函数。 两者都可以很好地编译为 .o 文件,但是当执行 clang++ 进行编译时,我收到以下错误:
file.cpp:74:12: error: expected unqualified-id
extern "C"
^
cpp文件中的代码如下:
void parseExtern(QString str)
{
#ifdef __cplusplus
extern "C"
{
#endif
function_in_C(str);
#ifdef __cplusplus
}
#endif
}
如何避免错误?我不能用clang++编译c文件,我真的需要用extern。谢谢。
最佳答案
extern "C"
链接规范是您附加到函数声明的内容。你不要把它放在调用站点。
在您的情况下,您会将以下内容放入头文件中:
#ifdef __cplusplus
extern "C"
{
#endif
void function_in_C(char const *); /* insert correct prototype */
/* add other C function prototypes here if needed */
#ifdef __cplusplus
}
#endif
然后在您的 C++ 代码中,您只需像调用任何其他函数一样调用它。无需额外装饰。
char const * data = ...;
function_in_C(data);
关于c++ - 错误 : expected unqualified-id on extern "C",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30440369/