从C src调用C++函数

Zak*_*kir 2 c c++ compiler-errors visual-studio-2010

我在MS Visual Studio 2010项目中混合使用C/C++代码库,并尝试从C src文件调用C++文件中定义的静态函数.现在我通过将C src重命名为CPP来实现它(ac - > a.cpp)只是想知道是否有一种更优雅的方式绕过它(转换一些神奇的编译器标志等)而不进行任何大规模的手术代码库(比如使用线程中建议的不透明指针)

请注意我的代码库非常复杂,我已经创建了这个小的VS片段,用最小的可证明的代码重现错误

AC

#include "b.h"

void test()
{
  B::func();
}
Run Code Online (Sandbox Code Playgroud)

BH

#ifdef __cplusplus
extern "C" {
#endif

class B{
public:
  static void func();
};

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

b.cpp

#include "b.h"

#ifdef __cplusplus
extern "C" {
#endif

void B::func()
{
  return;
}

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

错误: - 在MS Visual Studio 2010中

1>c:\.....\b.h(5): error C2061: syntax error : identifier 'B'
1>c:\.....\b.h(5): error C2059: syntax error : ';'
1>c:\.....\b.h(5): error C2449: found '{' at file scope (missing function header?)
1>c:\.....\b.h(8): error C2059: syntax error : '}'
Run Code Online (Sandbox Code Playgroud)

小智 5

首先,::在C中无效.

其次,包括标题相当于将.h文件复制粘贴到您的C文件中.您的标题必须有效C.以下是一些更深入的见解:

如何从C调用C++函数?

优雅地从C调用C++

虽然,我的另一个建议是,将C编译为C++.有可能只需要很少或根本没有工作就可以成为有效的C++.