www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D with gmp via swig

reply Oliver <oliver.ruebenkoenig web.de> writes:
Hello I am trying to get to a gmp interface for D2. I have some trouble to get
that to work, however. Perhaps someone has an idea what I am doing wrong. I
must admit I am not sure this is a D issue or a swig issue or something else.

The interface file for swig

cat gmpd.i
%module gmpd
%{
#include "gmp.h"
%}

%include "gmp.h"


A main with a call to gmpd

cat main.d
import std.stdio;
import gmpd;

void main() {
        writeln("Hello");
}

This is what I do then:
/usr/local/swig-204/bin/swig -I/usr/include/ -d -d2 gmpd.i

/opt/usr/local/bin/gdc -c main.d
/opt/usr/local/bin/gdc -c gmpd.d
/opt/usr/local/bin/gdc -c gmpd_im.d

/opt/usr/local/bin/gdc -c gmpd_wrap.c
/opt/usr/local/bin/gdc main.o -oa.out gmpd.o gmpd_im.o gmpd_wrap.o -lgmp

I get messages of the type:

/usr/lib64/gcc/x86_64-suse-linux/4.5/../../../../x86_64-suse-linux/bin/ld:
__gmp_set_memory_functions: TLS definition in gmpd_im.o section .tbss
mismatches non-TLS reference in gmpd_wrap.o
gmpd_wrap.o: could not read symbols: Bad value
collect2: ld returned 1 exit status


I have tried dmd and/or gcc (for the wrapper compilation and linking) but no
joy.
 Also -fPIC did not help.

Thanks,
Oliver
Aug 14 2011
next sibling parent reply Iain Buclaw <ibuclaw ubuntu.com> writes:
== Quote from Oliver (oliver.ruebenkoenig web.de)'s article
 Hello I am trying to get to a gmp interface for D2. I have some trouble to get
 that to work, however. Perhaps someone has an idea what I am doing wrong. I
 must admit I am not sure this is a D issue or a swig issue or something else.
 The interface file for swig
 cat gmpd.i
 %module gmpd
 %{
 #include "gmp.h"
 %}
 %include "gmp.h"
 A main with a call to gmpd
 cat main.d
 import std.stdio;
 import gmpd;
 void main() {
         writeln("Hello");
 }
 This is what I do then:
 /usr/local/swig-204/bin/swig -I/usr/include/ -d -d2 gmpd.i
 /opt/usr/local/bin/gdc -c main.d
 /opt/usr/local/bin/gdc -c gmpd.d
 /opt/usr/local/bin/gdc -c gmpd_im.d
 /opt/usr/local/bin/gdc -c gmpd_wrap.c
 /opt/usr/local/bin/gdc main.o -oa.out gmpd.o gmpd_im.o gmpd_wrap.o -lgmp
 I get messages of the type:
 /usr/lib64/gcc/x86_64-suse-linux/4.5/../../../../x86_64-suse-linux/bin/ld:
 __gmp_set_memory_functions: TLS definition in gmpd_im.o section .tbss
 mismatches non-TLS reference in gmpd_wrap.o
 gmpd_wrap.o: could not read symbols: Bad value
 collect2: ld returned 1 exit status

This will be a bug in swig. Whatever storage class it is setting __gmp_set_memory_functions, it is wrong (I assume it should be defining it as __gshared). Regards Iain
Aug 14 2011
parent Oliver <oliver.ruebenkoenig web.de> writes:
Thanks Iain,

I'll contact the swig team about it.

Oliver
Aug 14 2011
prev sibling next sibling parent reply David Nadlinger <see klickverbot.at> writes:
On 8/14/11 1:29 PM, Oliver wrote:
 Hello I am trying to get to a gmp interface for D2. I have some trouble to get
 that to work, however. Perhaps someone has an idea what I am doing wrong. I
 must admit I am not sure this is a D issue or a swig issue or something else.

 […]

 /opt/usr/local/bin/gdc main.o -oa.out gmpd.o gmpd_im.o gmpd_wrap.o -lgmp

SWIG currently only supports dynamically loading the wrapper library. Drop -lgmp and gmpd_wrap.o when linking the D executable and compile a shared library (.so, .dylib/.bundle, .dll) out of the wrapper code instead (by default, the name would be libgmpd_wrap.<ext>). David
Aug 14 2011
next sibling parent reply Oliver <oliver.ruebenkoenig web.de> writes:
Thanks David,

so when I

gcc -fPIC -c gmpd_wrap.c
gcc -shared -Wl,-soname,libgmpd_wrap.so.1 -o libgmpd_wrap.so.1.0.1 gmpd_wrap.o
-lgmp

ln -s libgmpd_wrap.so.1.0.1 libgmpd_wrap.so

/opt/usr/local/bin/gdc main.d gmpd.d gmpd_im.d -I. -L. -lgmpd_wrap

I get the
/usr/bin/ld: __gmpn_ior_n: TLS definition in /tmp/ccmF49fr.o section .tbss
mismatches non-TLS reference in ./libgmpd_wrap.so
./libgmpd_wrap.so: could not read symbols: Bad value
collect2: ld returned 1 exit status

again, a mismatch.

Oliver
Aug 14 2011
parent reply David Nadlinger <see klickverbot.at> writes:
On 8/14/11 2:34 PM, Oliver wrote:
 /opt/usr/local/bin/gdc main.d gmpd.d gmpd_im.d -I. -L. -lgmpd_wrap

You don't need to link the .so in (although I'm surprised to see that it produces linker errors, I didn't remember that there are name collisions with the default settings), it will be loaded at runtime. This decision was made to easily support Windows, where the COFF/OMF situation complicates matters a lot… David
Aug 14 2011
parent reply Oliver <oliver.ruebenkoenig web.de> writes:
== Quote from David Nadlinger (see klickverbot.at)'s article
 On 8/14/11 2:34 PM, Oliver wrote:
 /opt/usr/local/bin/gdc main.d gmpd.d gmpd_im.d -I. -L. -lgmpd_wrap

produces linker errors, I didn't remember that there are name collisions with the default settings), it will be loaded at runtime. This decision was made to easily support Windows, where the COFF/OMF situation complicates matters a lot… David

David, thanks this seems to work. /opt/usr/local/bin/gdc main.d gmpd.d gmpd_im.d I did have to copy the libgmpd_wrap* to some place were the linker could find it, of course. Oliver
Aug 14 2011
parent reply David Nadlinger <see klickverbot.at> writes:
On 8/14/11 3:03 PM, Oliver wrote:
 I did have to copy the libgmpd_wrap* to some place were the linker could find
it,
 of course.

Alternatively, you could set LD_LIBRARY_PATH to where your library resides. If there is demand, I could definitely look into adding static linking support to SWIG (it is on the TODO list already), but currently, I'm rather busy with my Thrift GSoC project. David
Aug 14 2011
parent Oliver <oliver.ruebenkoenig web.de> writes:
== Quote from David Nadlinger (see klickverbot.at)'s article
 On 8/14/11 3:03 PM, Oliver wrote:
 I did have to copy the libgmpd_wrap* to some place were the linker could find
it,
 of course.

resides. If there is demand, I could definitely look into adding static linking support to SWIG (it is on the TODO list already), but currently, I'm rather busy with my Thrift GSoC project. David

David, I do not have any preference - I think, however, as a quick fix, it might be beneficial to add the current status, that a) you need dynamic linking and b) that -L. -lgmpd_wrap does not work as expected to the documentation. That would have helped me. Oliver
Aug 14 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
This seems ancient, but maybe it could help:
http://www.dsource.org/projects/bindings/browser/trunk/gmp
Aug 14 2011
prev sibling parent Caligo <iteronvexor gmail.com> writes:
--20cf3071c6a48226b904aa7658b9
Content-Type: text/plain; charset=ISO-8859-1

Shouldn't there be an interface to GMP and MPFR in Phobos by default?

--20cf3071c6a48226b904aa7658b9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Shouldn&#39;t there be an interface to GMP and MPFR in Phobos by default?=
=A0 <br>

--20cf3071c6a48226b904aa7658b9--
Aug 14 2011