www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using static d libs in C++

reply Chrono <chrono mta-international.net> writes:
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.


import std.stdio;

extern (C) int testproc(int somevar)
{
        return (somevar + 1);
}



#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, I get main.o and sub.o but then

gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L./dmd/bin/../lib
-lphobos -lpthread -lm



/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




any ideas?
Sep 28 2007
next sibling parent reply Mike Wey <no.spam example.com> writes:
Chrono wrote:
 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.
 

 import std.stdio;
 
 extern (C) int testproc(int somevar)
 {
         return (somevar + 1);
 }

 

 #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, I get main.o and sub.o but then
 
 gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L./dmd/bin/../lib
 -lphobos -lpthread -lm
 
 
 
 /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
 
 
 
 
 any ideas?
It looks like gcc is looking for libphobos.a in: /usr/lib/libphobos.a while judging from what you use on the command line: -L./dmd/bin/../lib it's not located there. try specifying the full path.
Sep 29 2007
parent reply Chrono <chrono mta-international.net> writes:
Hi Mike,

thank you for your help.

now I tried:

gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L/root/dmd/lib 
-lphobos -lpthread -lm

/root/dmd that's the original dmd linux package.

but same error as before:

/root/dmd/lib/libphobos.a(deh2.o): In function 
`_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh21
DHandlerTable+0x9): 
undefined reference to `_deh_beg'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh21
DHandlerTable+0xe): 
undefined reference to `_deh_beg'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213
HandlerTable+0x14): 
undefined reference to `_deh_end'
internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213
HandlerTable+0x37): 
undefined reference to `_deh_end'
collect2: ld returned 1 exit status
Sep 29 2007
parent BCS <ao pathlink.com> writes:
Reply to Chrono,

 Hi Mike,
 
 thank you for your help.
 
 now I tried:
 
 gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L/root/dmd/lib
 -lphobos -lpthread -lm
 
 /root/dmd that's the original dmd linux package.
 
[...] try: wc /root/dmd/lib/libphobos.a this will fail if the current user cant read the file (as they shouldn't if your not running as root, and if you are running as root, shame on you) If your not sure where to put stuff, I have an install script that works most of the time here http://svn.dsource.org/projects/scrapple/trunk/scripts/dmd_update be warned it will attempt to download dmd.zip from the web, so if that is a problem (either you want a different version or you don't want to burn the bandwidth again) you will want to tweak it. Either way there are a few option at the top you might want to twiddle a bit (it attempt to save off old copies of everything for instance) And I just noticed I might be missing a few things, if you try it, please tell me how well it worked.
Sep 29 2007
prev sibling parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Chrono wrote:
 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.
[snip]
 Compiling them wihout linking.
 ./dmd -c sub.d
 g++ -c main.cpp
 
 everything works fine, I get main.o and sub.o but then
 
 gcc main.o sub.o -o myprogram -m32 -lstdc++ -Xlinker -L./dmd/bin/../lib
 -lphobos -lpthread -lm
 
 /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'
[snip]
 
 any ideas?
IIRC those symbols are emitted when DMD sees the main() function. Any chance you could write your main() in D?
Sep 29 2007