shared libs for OSX

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Sun May 24 14:05:06 PDT 2015


On Sun, 24 May 2015 15:40:04 -0400, bitwise <bitwise.pvt at gmail.com> wrote:

> [snip]
>
> So at this point, it seems like these two fixes work as expected, but  
> now, I'm having some new and very strange problems.
>
> I have a simple shared library and program I've been using to test this:
>
> [main.d]
> module main;
> import std.stdio;
> import std.conv;
> import std.string;
> import core.sys.posix.dlfcn;
>
> void main(string[] args)
> {
>      alias void function() fnType;
>
>      void *handle = dlopen("myShared.dylib", RTLD_NOW);
>      assert(handle);
>
>      fnType init = cast(fnType)dlsym(handle, "initLib");
>      assert(init);
>      init();
>
>      fnType term = cast(fnType)dlsym(handle, "termLib");
>      assert(term);
>      term();
>
>      dlclose(handle);
>      writeln("done");
> }
>
> [myShared.d]
> module myShared;
> import core.runtime;
> import std.stdio;
>
> extern(C) void initLib() {
>      writeln("Initializing Runtime");
>      Runtime.initialize();
> }
>
> extern(C) void termLib() {
>      writeln("Terminating Runtime");
>      Runtime.terminate();
> }
>
>
> So, when I run the above program, rt_init() should be called once for  
> the main program, and once for the shared library. However, when I run  
> the above program, rt_init() from the main program seems to get called  
> twice. To clarify, I mean that when I retrieve "initLib()" with dlsym()  
> and call it, rt_init() from the main module gets called.
>
> This seems to prove the above:
>
> In dmain2.d, I have modified rt_init() as follows:
>
> extern (C) int rt_init()
> {
>      import core.sys.posix.dlfcn;
>      Dl_info info;
>      if(dladdr(&rt_init, &info))
>          fprintf(stdout, "RT INIT: %s\n", info.dli_fname);   // this  
> prints "main" for both calls
>
>      if (atomicOp!"+="(_initCount, 1) > 1)
>      {
>          fprintf(stdout, "RT ALREADY INITIALIZED\n");
>          return 1;
>      }
>
> // ...
>
>      fprintf(stdout, "RT INIT COMPLETE\n");
> }
>
> When the main program calls rt_init(), the output correctly reads "RT  
> INIT COMPLETE".
> When I load the dynamic library however, I get the output "RT ALREADY  
> INITIALIZED"
>
> How is this possible? I am not using a shared druntime afaik..

I just found that gcc has "-fvisibility=hidden" and  
"-fvisibility-ms-compat" which are equivalent. The lack of this  
functionality for dmd seems to explain the above problem.

Is this correct?
Any ideas?

   Bit


More information about the Digitalmars-d mailing list