c++ - 错误 : expected unqualified-id on extern "C"

标签 c++ clang extern-c

我有一个 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/

相关文章:

c - 这种对 int64_t 的处理是 GCC 和 Clang 错误吗?

c++ - extern "C"在 C 中是否有任何影响?

c++ - 如何通过按位运算在 double 之间执行求和

c++ - 静态变量 & For 循环

c++ - 当它们应用于构造函数时,括号初始值设定项列表中是否有序列点?

c++ - extern “C”在C++中有什么作用?

c++ - C++中extern "C"有什么作用?

c++ - 递归地使用单个函数显示具有最小值和最大值的三角形

c++ - QFileDialog : using getOpenFileName allow for non-existent files

gcc - 创建共享库时出现 Clang 错误