windows - Can I mix C and C++ exports in a single DLL header? -
help me out here, because i'm half convinced can't want , half convinced there should suitable workaround.
i have dll that's implemented in c++ , exports classes other c++ modules link it. that's fine. want link dll c module (another dll), provide "flattened" c interface , handle c++ stuff internally. that's fine.
the problem want supply c or c++ clients single .h , associated .lib. have similar following in dll:
#ifdef dll_exports #define dll_api __declspec(dllexport) #else #define dll_api __declspec(dllimport) #endif // export class c++ clients class dll_api cexportedclass { public: cexportedclass(); // etc. etc. }; // export flattened c interface non-c++ clients #ifdef __cplusplus extern "c" { #endif dll_api void dosomethinginternally(); // i.e. implementation uses cexportedclass #ifdef __cplusplus } #endif
of course, works fine when imported c++ module, fails compile when imported c module because doesn't recognise class
declaration.
so wrong think can this? need split 2 headers? correct , acceptable use #ifdef __cplusplus
around class
declarations (or other kind of #ifdef
)?
really struggling "clean" answer here.
there couple of articles on msdn mixing c , c++:
http://msdn.microsoft.com/en-us/library/aa270933%28v=vs.60%29.aspx
http://msdn.microsoft.com/en-us/library/s6y4zxec%28v=vs.60%29.aspx
i think can take @ windows.h or similar headers, works fine both c , c++ without problems.
basically how works:
at beginning of header file
#ifndef _myheader__h #define _myheader__h #ifdef __cplusplus extern "c" { #endif //decalrations //........ //........ //bottom of header #ifdef __cplusplus } #endif #endif
so header should looks this:
#ifdef dll_exports #define dll_api __declspec(dllexport) #else #define dll_api __declspec(dllimport) #endif #ifdef __cplusplus //this part of header not visible ansi c compiler // export class c++ clients class dll_api cexportedclass { public: cexportedclass(); // etc. etc. }; #endif #ifdef __cplusplus extern "c" { #endif dll_api void dosomethinginternally(); // i.e. implementation uses cexportedclass #ifdef __cplusplus } #endif
this how looks ansi c compiler:
#ifdef dll_exports #define dll_api __declspec(dllexport) #else #define dll_api __declspec(dllimport) #endif dll_api void dosomethinginternally();
this how looks c++ compiler:
#ifdef dll_exports #define dll_api __declspec(dllexport) #else #define dll_api __declspec(dllimport) #endif class dll_api cexportedclass { public: cexportedclass(); // etc. etc. }; extern "c" { dll_api void dosomethinginternally(); }
however, declare class in header, c compiler not happy this, should place out of "c" declarations.
take here:
Comments
Post a Comment