undefined symbol: _Dmain when loading library at runtime on Linux

FreeSlave freeslave93 at gmail.com
Sun Sep 29 16:29:20 PDT 2013


//mod.d:

module mod;

export extern(C) int testThrow()
{
     throw new Exception("Just a test");
}

//main.d:

import std.stdio;
import std.c.linux.linux;
import std.string : toStringz;
import std.conv : to;

int main(string args[])
{
     version(Binding)
     {
         version(LDC)
         {
             string name = "./libmodldc.so";
         }
         else
         {
             string name = "./libmod.so";
         }
         void* handle = dlopen(toStringz(name), RTLD_LAZY);
         if (!handle)
         {
             writeln(to!string(dlerror()));
             return 1;
         }
         auto testThrow = cast(void function())dlsym(handle, 
toStringz("testThrow"));
         if (!testThrow)
         {
             writeln(to!string(dlerror()));
             return 1;
         }
     }
     else
     {
         import mod;
     }

     try {
         testThrow();
     }
     catch(Exception e)
     {
         writeln(e.msg);
     }

     return 0;
}

Script to build them all and start executables in place:

#! /bin/bash
export LD_LIBRARY_PATH=.
ldmd2 -shared -fPIC mod.d -oflibmodldc.so
ldmd2 main.d -L-L. -L-lmodldc -ofmainldc
ldmd2 main.d -version=Binding -ofmainldcbind

dmd -shared -fPIC mod.d -oflibmod.so
dmd main.d -L-L. -L-lmod -ofmain
dmd main.d -version=Binding -ofmainbind -L-ldl

./main
./mainbind
./mainldc
./mainldcbind

Both main and mainldc work fine (those which was linked against 
shared library). But when I start "bind" version compiled by 
ldmd2 I got error:

./libmodldc.so: undefined symbol: _Dmain

If I add -L--export-dynamic option to command line then it works 
fine. But dmd does not require this option - mainbind has correct 
output.

Another strange thing is that ldmd2 does not require linking 
against dlfcn library.


More information about the digitalmars-d-ldc mailing list