export keyword messes up static linking

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Jan 26 03:38:23 PST 2007


torhu wrote:
> I think I'll try to restate the problem in a simpler way.
> 
> I have a C lib, that I want to link with D apps.  So I write a D 
> 'header', like so:
> 
> module clib;
> 
> export extern extern (C) {
>     int foo;
>     char* bar;
> }
> 
> 
> This works just fine, if I link dynamically with the C lib.  But if I 
> want to link statically, it doesn't work.  This is because 'export' 
> can't be there when you link statically.  foo and bar ptr get random 
> values in the D app.
> 
> In this example 'export' works like msvc's __declspec(dllimport).  So in 
> C code, you could use a macro to turn it off for static linking.
> 
> 
> Does Optlink have a switch for this, or can someone tell me how to write 
> a .def file so I don't have to declare the variables 'export'?
> 
> This C lib is cross-platform, so I'd also like to know if gcc (for dmd 
> use), or at least gdc has a solution for this.  Currently we use a 
> script that just does 's/export//g'.

Put "version(C_Lib_Dll) export:" before your declarations.
If you then pass "-version=C_Lib_Dll" (no quotes) to DMD (or something 
like "-fversion=C_Lib_Dll" to GDC) when compiling, or put "version = 
C_Lib_Dll" in the source above that line, 'export' will be applied to 
all declarations after that. If you don't do any of those things, it 
won't. This means you have to put all non-exported symbols (if any) 
above this line, though.
Of course, the "C_Lib_Dll" should be changed to something unique to your 
C library (and preferably indicate it is to be loaded as a DLL).
Normally, the scope of such a statement can be limited by putting it in 
a {} block, but those aren't allowed at module scope.


More information about the Digitalmars-d-learn mailing list