我在 .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/