简体   繁体   中英

C-DLL from C++ source

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')

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);
}

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 '...'".

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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