最大的问题是从C++代码调用C函数,反之亦然.在这种情况下,您需要确保将该功能标记为使用"C"链接extern "C".您可以直接使用以下命令在头文件中执行此操作:
#if defined( __cplusplus )
extern "C" {
#endif
extern int myfunc( const char *param, int another_one );
#if defined( __cplusplus )
}
#endif
Run Code Online (Sandbox Code Playgroud)
你需要#ifs,因为包含它的C代码将无法理解extern "C".
如果您不想(或不能)更改头文件,可以在C++代码中执行此操作:
extern "C" {
#include "myfuncheader.h"
}
Run Code Online (Sandbox Code Playgroud)
您可以将C++函数标记为具有相同方式的C链接,然后可以从C代码中调用它.您不能为重载函数或C++类执行此操作.
除此之外,混合C和C++应该没有问题.我们有许多几十年前的C函数,我们的C++代码仍在使用它们.
| 归档时间: |
|
| 查看次数: |
1639 次 |
| 最近记录: |