Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top