www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - loading D into R in OSX. Dynamic libraries problem

reply Michael <lachmann eva.mpg.de> writes:
I'm probably trying to complicate things too much while not really
understanding what I'm doing... I want to be able to call D code from R. (R is
a language for doing statistics)
When I try to load the code, I get an error:
dlopen( PATH/test.so, 6): Symbol not found: __minfo_beg

what I did is the following: I have a small D program:
--
import tango.io.Stdout;
extern (C) void Dfunc()
{
 Stdout("got to D").newline ;
}
--
I compile it with
dmd -fPIC -c test.d
then I make it into a dynamic library:
R CMD SHLIB   /usr/local/lib/libtango-user-dmd.a
/usr/local/lib/libtango-base-dmd-d.a test.o
which calls:
gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
-mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module
-multiply_defined suppress -L/usr/local/lib -o test.so test.o
/usr/local/lib/libtango-user-dmd.a /usr/local/lib/libtango-base-dmd-d.a
-F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
-Wl,CoreFoundation

then I load the dynamic library into R, and get the above error.
dyn.load("test.so")

The equivalent in C works well:
--
#include <stdio.h>
void test()
{
   printf("got to C\n") ;
}
--
gcc -fPIC -c test.c
R CMD SHLIB test.o
which calls:
gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
-mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module
-multiply_defined suppress -L/usr/local/lib -o test.so test.o
-F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
-Wl,CoreFoundation

At this point loading the resulting dynamic library from R causes no problems.

What am I doing wrong, other than trying to complicate things?
Jun 26 2010
parent reply Jacob Carlborg <doob me.com> writes:
On 2010-06-26 11:59, Michael wrote:
 I'm probably trying to complicate things too much while not really
 understanding what I'm doing... I want to be able to call D code from R. (R is
 a language for doing statistics)
 When I try to load the code, I get an error:
 dlopen( PATH/test.so, 6): Symbol not found: __minfo_beg

 what I did is the following: I have a small D program:
 --
 import tango.io.Stdout;
 extern (C) void Dfunc()
 {
   Stdout("got to D").newline ;
 }
 --
 I compile it with
 dmd -fPIC -c test.d
 then I make it into a dynamic library:
 R CMD SHLIB   /usr/local/lib/libtango-user-dmd.a
 /usr/local/lib/libtango-base-dmd-d.a test.o
 which calls:
 gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
 -mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module
 -multiply_defined suppress -L/usr/local/lib -o test.so test.o
 /usr/local/lib/libtango-user-dmd.a /usr/local/lib/libtango-base-dmd-d.a
 -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
 -Wl,CoreFoundation

 then I load the dynamic library into R, and get the above error.
 dyn.load("test.so")

 The equivalent in C works well:
 --
 #include<stdio.h>
 void test()
 {
     printf("got to C\n") ;
 }
 --
 gcc -fPIC -c test.c
 R CMD SHLIB test.o
 which calls:
 gcc -arch i386 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
 -mmacosx-version-min=10.4 -undefined dynamic_lookup -single_module
 -multiply_defined suppress -L/usr/local/lib -o test.so test.o
 -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework
 -Wl,CoreFoundation

 At this point loading the resulting dynamic library from R causes no problems.

 What am I doing wrong, other than trying to complicate things?

First, have you built Tango as a dynamic library, using "bob" with the "-d" flag? Oh I see now that you use an older version of Tango. Download the latest version from trunk and use "bob" with the "-d" flag to build Tango as a dynamic library. Then you should be able to build dynamic libraries. BTW the "-fPIC" flag is not necessary on Mac, it's on by default. -- /Jacob Carlborg
Jun 26 2010
parent Michael <lachmann eva.mpg.de> writes:
== Quote from Jacob Carlborg (doob me.com)'s article
 First, have you built Tango as a dynamic library, using "bob" with the
 "-d" flag? Oh I see now that you use an older version of Tango. Download
 the latest version from trunk and use "bob" with the "-d" flag to build
 Tango as a dynamic library. Then you should be able to build dynamic
 libraries. BTW the "-fPIC" flag is not necessary on Mac, it's on by default.

Thank you! That worked! Just to explain what I did. svn co http://svn.dsource.org/projects/tango/trunk cd trunk/ ./build/script/bob.rb -vu -d . That created libtango.dylib, and linking with that library was successful. R CMD SHLIB /usr/local/lib/libtango.dylib test.o and in R:
 dyn.load("test.so")
 .C("Dfunc")

Thanks!
Jun 26 2010