来自C ++源代码的C-DLL [英] C-DLL from C++ source
问题描述
我的C ++框架有一个C-Wrapper.由于这应该可以在Mac和Windows上运行,因此我使用的是 scons :
I have a C-Wrapper for my C++ Framework. Since this should run on mac and windows I am using scons:
env = Environment()
env.Append(CPPPATH = ['./'])
env.Append(LIBS = 'kernel32.lib')
env.Append(LIBPATH = 'C:/Program Files/Microsoft SDKs/Windows/v6.0A/Lib')
env.SharedLibrary(target='warpLib', source='warplib.cpp')
warplib.cpp和warplib.h的简单版本如下:
Simple Versions of warplib.cpp and warplib.h look like this:
warplib.cpp
#define DllExport __declspec( dllexport )
#include "warplib.h"
extern "C" {
DllExport int foo(int a) {
return a;
}
}
warplib.h
#define DllExport __declspec( dllexport )
extern "C" {
DllExport int foo(int a);
}
有人可以告诉我这是怎么回事吗?我尝试了几乎所有可能使用'extern"C"的组合,但是它总是使我感到类似错误C2732:链接规范与早期的'...'规范相矛盾".
Can anybody tell me what is wrong with that? I tried almost all the combinations possible of 'extern "C"' but it always throws me something like "error C2732: linkage specification contradicts earlier specification for '...'".
如果我跳过'extern"C",它可以工作,但是看不到.lib文件,我很确定我真的需要使用该库.
If I skip 'extern "C"' it works but I see no .lib file and I am pretty sure I need that to really use the library.
推荐答案
您只需要在声明中使用 extern"C"
.然后包括该标头的任何人都将期望使用C链接标准而不是C ++装饰的形式针对它进行链接.如果包含了 warplib.h
,则 warplib.cpp
源文件和后续的目标文件将正确公开函数 foo
.
You should only need extern "C"
on the declaration. Anyone then including that header will expect to link against it using the C linking standard, rather than the C++ decorated form. The warplib.cpp
source file, and subsequent object file will expose the function foo
correctly if warplib.h
is included.
使用MSVC时,有大量半有用的脚本,并提供了构建环境"控制台快捷方式,并提供了许多与开发相关的环境变量和路径.我建议找到合适的脚本来执行,以将这些变量插入您的环境中,或运行开发控制台.
When using MSVC, there are a plethora of semi-useful scripts, and "build environment" console shortcuts provided, with a lot of dev-related environment variables and paths provided. I recommend locating a suitable script to execute to insert these variables into your environment, or running the dev console.
这篇关于来自C ++源代码的C-DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!