c++ - 与 extern "C"的 C 和 C++ 链接

标签 c++ c extern linkage

我在 .h 文件中定义了一个 C++ 函数,如下所示,并在 .cpp 文件中实现:

extern "C" void func(bool first, float min, float* state[6], float* err[6][6])
{
    //uses vectors and classes and other C++ constructs
}

如何在 C 文件中调用 func?如何设置我的文件架构/makefile 来编译它?

谢谢!

最佳答案

您以正常方式从 C 调用该函数。但是,您需要包装 extern "C"在预处理器宏中以防止 C 编译器看到它:

#ifndef __cplusplus
extern "C"
#endif
void func(bool first, float min, float* state[6], float* err[6][6]);

假设您正在使用 GCC,然后使用 gcc 编译 C 代码, 用 g++ 编译 C++ 代码, 然后链接 g++ .

关于c++ - 与 extern "C"的 C 和 C++ 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580968/

相关文章:

c - 在 C 中将文件中的多个整数扫描到整数数组中

c - MSG_PEEK 套接字读取/接收时的错误行为

c++ - 外部变量的内存将存储在哪个文件中

c++ - 在 copy-and-swap 习语中实现交换

c++ - 来自数据库的 QT ComboBox ItemData

c++ - 实现 C++17 兼容的 STL 容器?

c++ - 从 8 个连接的像素列表中提取片段

c - C语言错误

c - C 中不同类型的外部变量的声明

javascript - 我们可以使用 Google Closure Compiler 从完整的 .js 文件创建 externs 吗?