相关疑难解决方法(0)

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

我知道这个.

从C++调用C函数:

如果我的应用程序是在C++中,我不得不从用C编写的库调用函数.那么我会使用

//main.cpp

extern "C" void C_library_function(int x, int y);//prototype
C_library_function(2,4);// directly using it.
Run Code Online (Sandbox Code Playgroud)

这不会破坏名称C_library_function,链接器会在其输入*.lib文件中找到相同的名称,问题就解决了.

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

但是在这里我扩展了一个用C语言编写的大型应用程序,我需要使用一个用C++编写的库.C++的名称错误导致了麻烦.Linker抱怨未解决的符号.好吧,我不能在我的C项目中使用C++编译器,因为这会破坏很多其他东西.出路是什么?

顺便说一句,我正在使用MSVC

c c++ visual-c++ extern-c

74
推荐指数
4
解决办法
8万
查看次数

从C src调用C++函数

我在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: …
Run Code Online (Sandbox Code Playgroud)

c c++ compiler-errors visual-studio-2010

2
推荐指数
1
解决办法
330
查看次数