View previous topic :: View next topic |
Author |
Message |
Chrono
Joined: 27 Sep 2007 Posts: 2
|
Posted: Thu Sep 27, 2007 7:50 pm Post subject: Using static d libs in C++ |
|
|
Hello!
I'm new to d. And I'd like to use some d functions in my c++ projects by building them static and linking them.
my platform: dmd and g++ on linux
sub.d
Code: |
import std.stdio;
extern (C) int testproc(int somevar)
{
return (somevar + 1);
}
|
main.cpp
Code: |
#include <iostream>
extern "C" int testproc(int somevar);
int main(void)
{
int myvar = 5;
printf("myvar: %d\n", testproc(myvar));
return 0;
}
|
Compiling them wihout linking.
./dmd -c sub.d
g++ -c main.cpp
Everything works fine...
main.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
sub.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped
... but
g++ -o myprogram main.o sub.o
Quote: |
sub.o: In function `gcc2_compiled.':
sub.d.text+0x8): undefined reference to `_Dmodule_ref'
sub.o.data+0x30): undefined reference to `_D3std5stdio12__ModuleInfoZ'
collect2: ld returned 1 exit status
|
I've also found this...
http://www.digitalmars.com/d/archives/digitalmars/D/learn/4309.html
and tried...
g++ -o program main.o sub.o -fPIC -lphobos
Quote: |
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(deh2.o): In function `_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined reference to `_deh_beg'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined reference to `_deh_beg'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined reference to `_deh_end'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): undefined reference to `_deh_end'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(monitor.o): In function `_STI_monitor_staticctor':
monitor.c.text+0x18): undefined reference to `pthread_mutexattr_init'
monitor.c.text+0x27): undefined reference to `pthread_mutexattr_settype'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(monitor.o): In function `_STD_monitor_staticdtor':
monitor.c.text+0x77): undefined reference to `pthread_mutexattr_destroy'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread5startMFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread5startMFZv+0xb8): undefined reference to `pthread_create'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread4waitMFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread4waitMFZv+0x38): undefined reference to `pthread_join'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread4waitMFkZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread4waitMFkZv+0xdf): undefined reference to `pthread_cancel'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread5pauseMFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread5pauseMFZv+0x13): undefined reference to `pthread_kill'
std/thread.d.gnu.linkonce.t_D3std6thread6Thread5pauseMFZv+0x25): undefined reference to `sem_wait'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread6resumeMFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread6resumeMFZv+0x13): undefined reference to `pthread_kill'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread8pauseAllFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread8pauseAllFZv+0x40): undefined reference to `pthread_kill'
std/thread.d.gnu.linkonce.t_D3std6thread6Thread8pauseAllFZv+0x7b): undefined reference to `sem_wait'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread4initMFkZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread4initMFkZv+0x20): undefined reference to `pthread_attr_setstacksize'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread11thread_initFZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread11thread_initFZv+0xca): undefined reference to `sem_init'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(thread.o): In function `_D3std6thread6Thread12pauseHandlerUiZv':
std/thread.d.gnu.linkonce.t_D3std6thread6Thread12pauseHandlerUiZv+0x71): undefined reference to `sem_post'
collect2: ld returned 1 exit status
|
and last but not least tried this...
g++ main.o sub.o -o program -lphobos -lpthread
Quote: |
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(deh2.o): In function `_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined reference to `_deh_beg'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined reference to `_deh_beg'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined reference to `_deh_end'
internal/deh2.d.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): undefined reference to `_deh_end'
collect2: ld returned 1 exit status
|
any ideas? |
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Fri Sep 28, 2007 6:37 am Post subject: |
|
|
Why don't you try linking with dmd instead of g++?
dmd -ofmyprogram main.o sub.o -L-lstdc++ |
|
Back to top |
|
|
Chrono
Joined: 27 Sep 2007 Posts: 2
|
Posted: Fri Sep 28, 2007 8:36 am Post subject: |
|
|
because I thought dmd uses gcc for linking under linux.
running:
./dmd/bin/dmd -ofmyprogram main.o sub.o -L-lstdc++
automaticcly runs:
gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L./dmd/bin/../lib -lphobos -lpthread -lm
Code: |
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../lib/libphobos.a(deh2.o): In function `_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined reference to `_deh_beg'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined reference to `_deh_beg'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined reference to `_deh_end'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): undefined reference to `_deh_end'
collect2: ld returned 1 exit status
--- errorlevel 1
|
|
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Fri Sep 28, 2007 10:15 am Post subject: |
|
|
Bummer. I've seen that other people have had that problem, but I don't remember how they fixed it.
Perhaps you'll get more answers by posting on the newsgroups. Go to the learn newsgroup and ask there. Use a ng reader (Thunderbird, Opera, Outlooke Express, etc.) if you have one. |
|
Back to top |
|
|
Destructionator
Joined: 17 Sep 2007 Posts: 9 Location: New York State
|
Posted: Fri Sep 28, 2007 5:35 pm Post subject: |
|
|
It is complaining because the main() function is wrong.
main() in D is called by another main() in phobos that sets up exception catching and the garbage collector and all kinds of other fun stuff.
main() in C++ is different.
A simple way to solve this is to put the main() function in the D source file rather than the C++. |
|
Back to top |
|
|
|