首页 > 解决方案 > 在 C++ 中使用 C 函数 使用 extern "C"

问题描述

我正在尝试使用 extern "C" 在 Cpp 代码中访问我的两个 C 函数。我使用它如下:

#include <stdio.h>
#include <stdlib.h>
#include <cstdio>


extern "C"
{
#include "pltfrm.h"
#include "xil_printf.h"

}



int main()
{
    init_platform();

    print("Hello World\n\r");
    print("Successfully ran Hello World application");
    cleanup_platform();
    return 0;
}

函数"init_platform""cleanup_platform"在内部声明"pltfrm.h"

我在两个函数上都收到“未定义的引用”错误。我做错了什么?iam 使用 c++ 空项目模板的 eclipse 构建项目选项进行编译。

标签: c++ceclipseg++

解决方案


该消息来自链接步骤,当编译所有源文件并且链接器尝试将所有函数连接到函数调用时。

您可能需要链接到某个库或将源文件(对于您添加的 c 头文件)添加到您的项目中。


推荐阅读