error: expected unqualified-id on extern “C”

让人想犯罪 __ 提交于 2019-12-14 02:32:16

问题


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"
           ^

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

}

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


回答1:


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

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

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


来源:https://stackoverflow.com/questions/30440369/error-expected-unqualified-id-on-extern-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!