Shared library extern (C) variables

Mineko uminekorox at gmail.com
Sun Jan 5 11:55:49 PST 2014


On Sunday, 5 January 2014 at 19:47:46 UTC, FreeSlave wrote:
>
> Some code snippets of what you try to do would help.
>
> Maybe this example explain you something:
>
> //mod.d
> extern(C) int foo = 42;
>
> void changeFoo(int val)
> {
>     foo = val;
> }
>
> //main.d
> import std.stdio;
> import mod;
>
> int main()
> {
>     writeln(foo);
>     changeFoo(15);
>     writeln(foo);
>     return 0;
> }
>
> Compile:
> dmd -H -c mod.d && //to generate .di file (not required)
> dmd -shared mod.d -oflibmod.so && //to generate shared library
> dmd -L-L. -L-lmod -L-rpath=. main.d //rpath is special argument 
> to linker, so executable will able to find shared library in 
> its directory. You can omit rpath, but then you must put your 
> .so file to one of standard directory like /usr/lib or 
> /usr/local/lib. That's all for Linux. On Windows you would have 
> different building steps.
>
> Also probably you want to make your varialbe __gshared, so it 
> will not be thread-local. That's default D behavior to make all 
> global variables thread-local. And I don't know how other 
> languages will handle D thread-local variable.

Ahh I appreciate it, but I already have that part down and good. 
:)

I was wondering about how to use export correctly, I apologize 
for not being clear.

Also I'll keep in mind the __gshared, never even knew about it.


More information about the Digitalmars-d-learn mailing list