简体   繁体   English

来自C ++源代码的C-DLL

[英]C-DLL from C++ source

I have a C-Wrapper for my C++ Framework. 我的C ++框架有一个C-Wrapper。 Since this should run on mac and windows I am using scons : 由于这应该在mac和Windows上运行,因此我正在使用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')

Simple Versions of warplib.cpp and warplib.h look like this: warplib.cpp和warplib.h的简单版本如下所示:

warplib.cpp warplib.cpp

#define DllExport __declspec( dllexport )
#include "warplib.h"

extern "C" {
  DllExport int foo(int a) {
    return a;
  }
}

warplib.h warplib.h

#define DllExport __declspec( dllexport )

extern "C" {
  DllExport int foo(int a);
}

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”的组合,但是它总是使我感到类似“错误C2732:链接规范与早期的'...'规范矛盾”。

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”,它可以工作,但是看不到.lib文件,我很确定我需要它才能真正使用该库。

You should only need extern "C" on the declaration. 您只需要在声明中使用extern "C" Anyone then including that header will expect to link against it using the C linking standard, rather than the C++ decorated form. 然后包括该标头的任何人都将期望使用C链接标准而不是C ++装饰的形式针对它进行链接。 The warplib.cpp source file, and subsequent object file will expose the function foo correctly if warplib.h is included. 所述warplib.cpp源文件,和随后的对象文件将暴露函数foo正确如果warplib.h是包括在内。

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. 使用MSVC时,有大量半有用的脚本,并提供了“构建环境”控制台快捷方式,并提供了许多与开发相关的环境变量和路径。 I recommend locating a suitable script to execute to insert these variables into your environment, or running the dev console. 我建议找到合适的脚本来执行以将这些变量插入您的环境中,或者运行开发控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM