错误:外部"C"上预期的不合格ID. [英] error: expected unqualified-id on extern "C"

查看:155
本文介绍了错误:外部"C"上预期的不合格ID.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cpp代码,我想在其中调用c函数. 两者都可以很好地编译为.o文件,但是当执行clang ++进行编译时,我收到以下错误:

I have a cpp code in which I want to call a c function. Both compile well to .o files, but when the clang++ is executing for compilation, I receive the following error:

file.cpp:74:12: error: expected unqualified-id
    extern "C"
           ^

cpp文件中的代码如下:

The code in the cpp file is the following:

void parseExtern(QString str)
{
#ifdef __cplusplus
    extern "C"
    {
#endif
        function_in_C(str);
#ifdef __cplusplus
    }
#endif

}

如何避免该错误?我无法使用clang ++编译c文件,我确实需要使用extern.谢谢.

How can I avoid the error ? I can't compile the c file with clang++, I really need to use extern. Thanks.

推荐答案

extern "C"链接规范是您附加到函数声明的内容.您不会将其放在呼叫站点.

The extern "C" linkage specification is something you attach to a function declaration. You don't put it at the call site.

在您的情况下,请将以下内容放在头文件中:

In your case, you'd put the following in a header file:

#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 ++代码中,就像其他任何函数一样调用它.不需要额外的装饰.

Then in your C++ code, you just call it like any other function. No extra decoration required.

char const * data = ...;
function_in_C(data);

这篇关于错误:外部"C"上预期的不合格ID.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆