Lau*_*llo 1 c++ clang extern-c
我有一个cpp代码,我想在其中调用ac函数.两者都编译好.o文件,但是当clang ++正在执行编译时,我收到以下错误:
file.cpp:74:12: error: expected unqualified-id
extern "C"
^
Run Code Online (Sandbox Code Playgroud)
cpp文件中的代码如下:
void parseExtern(QString str)
{
#ifdef __cplusplus
extern "C"
{
#endif
function_in_C(str);
#ifdef __cplusplus
}
#endif
}
Run Code Online (Sandbox Code Playgroud)
如何避免错误?我无法使用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
Run Code Online (Sandbox Code Playgroud)
然后在你的C++代码中,你就像任何其他函数一样调用它.无需额外装饰.
char const * data = ...;
function_in_C(data);
Run Code Online (Sandbox Code Playgroud)