FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using static d libs in C++

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
Chrono



Joined: 27 Sep 2007
Posts: 2

PostPosted: Thu Sep 27, 2007 7:50 pm    Post subject: Using static d libs in C++ Reply with quote

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.dSad.text+0x8): undefined reference to `_Dmodule_ref'
sub.oSad.data+0x30): undefined reference to `_D3std5stdio12__ModuleInfoZ'
collect2: ld returned 1 exit status



I've also found this...
Idea 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.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined reference to `_deh_beg'
internal/deh2.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined reference to `_deh_beg'
internal/deh2.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined reference to `_deh_end'
internal/deh2.dSad.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.cSad.text+0x18): undefined reference to `pthread_mutexattr_init'
monitor.cSad.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.cSad.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.dSad.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.dSad.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.dSad.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.dSad.gnu.linkonce.t_D3std6thread6Thread5pauseMFZv+0x13): undefined reference to `pthread_kill'
std/thread.dSad.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.dSad.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.dSad.gnu.linkonce.t_D3std6thread6Thread8pauseAllFZv+0x40): undefined reference to `pthread_kill'
std/thread.dSad.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.dSad.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.dSad.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.dSad.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.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): undefined reference to `_deh_beg'
internal/deh2.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): undefined reference to `_deh_beg'
internal/deh2.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): undefined reference to `_deh_end'
internal/deh2.dSad.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): undefined reference to `_deh_end'
collect2: ld returned 1 exit status


any ideas? Rolling Eyes
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Fri Sep 28, 2007 6:37 am    Post subject: Reply with quote

Why don't you try linking with dmd instead of g++?
dmd -ofmyprogram main.o sub.o -L-lstdc++
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Chrono



Joined: 27 Sep 2007
Posts: 2

PostPosted: Fri Sep 28, 2007 8:36 am    Post subject: Reply with quote

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
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Fri Sep 28, 2007 10:15 am    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
Destructionator



Joined: 17 Sep 2007
Posts: 9
Location: New York State

PostPosted: Fri Sep 28, 2007 5:35 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group