Bug 10711 – shared phobos library should not depend on _Dmain

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-24T07:16:00Z
Last change time
2013-08-15T10:40:22Z
Keywords
dll
Assigned to
nobody
Creator
code

Comments

Comment #0 by code — 2013-07-24T07:16:13Z
When building shared D libraries the function _Dmain remains unresolved. This is because a C main function is part of druntime. To fix this we should generate the C main function in the glue layer of the compiler and only do that if a D main function is present. This needs some refactoring in druntime first so that the function becomes reasonably simple, e.g. something along this line. extern(C) int main(int argc, char*[] argv) { auto rc = rt_init(argv[0 .. argc]); if (rc == 0) { runMain(); rc = rt_term(); } return rc; }
Comment #1 by bugzilla — 2013-08-08T00:23:21Z
To reproduce: main.c: #include <stdio.h> #include <stdlib.h> #include <dlfcn.h> int main() { printf("+main()\n"); void *lh = dlopen("/home/walter/tmp/libdll.so", RTLD_LAZY); if (!lh) { fprintf(stderr, "dlopen error: %s\n", dlerror()); exit(1); } printf("libdll.so is loaded\n"); int (*fn)() = dlsym(lh, "dll"); char *error = dlerror(); if (error) { fprintf(stderr, "dlsym error: %s\n", error); exit(1); } printf("dll() function is found\n"); (*fn)(); printf("unloading libdll.so\n"); dlclose(lh); printf("-main()\n"); return 0; } dll.d: import core.stdc.stdio; extern (C) int dll() { printf("dll()\n"); return 0; } static this() { printf("libdll.so construction\n"); } static ~this() { printf("libdll.so destruction\n"); } Build: dmd -c dll.d -fPIC dmd -oflibdll.so dll.o -shared -defaultlib=libphobos2.so -L-rpath=/home/walter/cbx/mars/phobos/generated/linux/release/64 gcc -c main.c gcc -rdynamic main.o -o main -ldl ./main Results: +main() dlopen error: /home/walter/cbx/mars/phobos/generated/linux/release/64/libphobos2.so.0.64: undefined symbol: _Dmain
Comment #2 by bugzilla — 2013-08-10T01:10:30Z
Comment #3 by github-bugzilla — 2013-08-11T16:41:09Z
Commits pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/5373575c109cf997f4c9cfa09af7802b4ce8b7c4 fix Issue 10711 - shared phobos library should not depend on _Dmain https://github.com/D-Programming-Language/druntime/commit/f6692feb43904bf231ebde76317190084796b63a Merge pull request #563 from WalterBright/fix10711 fix Issue 10711 - shared phobos library should not depend on _Dmain
Comment #4 by bugzilla — 2013-08-14T23:07:10Z
Comment #5 by github-bugzilla — 2013-08-15T10:30:25Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/0082669292b0a4e535420ea9aabcd57fd7844923 fix Issue 10711 - shared phobos library should not depend on _Dmain https://github.com/D-Programming-Language/dmd/commit/ab3a4030977d369b33f4d42b6964a134c7a800b5 Merge pull request #2476 from WalterBright/fix10711 fix Issue 10711 - shared phobos library should not depend on _Dmain