Jump to page: 1 2
Thread overview
D with gmp via swig
Aug 14, 2011
Oliver
Aug 14, 2011
Iain Buclaw
Aug 14, 2011
Oliver
Aug 14, 2011
David Nadlinger
Aug 14, 2011
Caligo
Aug 14, 2011
Oliver
Aug 14, 2011
David Nadlinger
Aug 14, 2011
Oliver
Aug 14, 2011
David Nadlinger
Aug 14, 2011
Oliver
Aug 14, 2011
Andrej Mitrovic
August 14, 2011
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
August 14, 2011
== 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

August 14, 2011
Thanks Iain,

I'll contact the swig team about it.

Oliver
August 14, 2011
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
August 14, 2011
Shouldn't there be an interface to GMP and MPFR in Phobos by default?


August 14, 2011
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
August 14, 2011
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
August 14, 2011
== 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
> 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

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
August 14, 2011
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

August 14, 2011
== 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.
> 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

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
« First   ‹ Prev
1 2