【问题标题】:error: expected unqualified-id on extern "C"错误:extern "C" 上的预期 unqualified-id
【发布时间】:2015-08-07 01:18:10
【问题描述】:

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

【问题讨论】:

  • @Mat:这是一个答案!!是的,它很短,但是,这是一个简单的问题:P
  • 那么从 CPP 调用我的 C 函数的最佳方式是什么?关于 QString,我可以很容易地转换为 char *.
  • @LaurentCrivello:用extern "C" 声明函数。像任何其他函数一样调用它。
  • @LaurentCrivello - 我认为 This answer 解决了您的评论问题。

标签: c++ clang extern-c


【解决方案1】:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多