Thursday, 18 November 2010

gcc and gmp and mac

This is only relevant if you have a mac and want to use libgmp (gnu multi precision library), with perl.

My problems were as follows:
a) the default c compiler was giving me grief.
b) In CPAN, the library by default was not being detected even though it was being built.
c) going out of cpan and doing things 'manually' allowed me to specify the directory the library was installed in.
d) the architecture was still wrong. (because my perl was 32 bit)

1. gcc 4.0 is not supported. gcc 4.2 is..
make clean
CC=gcc-4.2 CXX=g++4.2 ./configure
make
make check
[tests pass, -m64 is visible in libtool]
sudo make install
file usr/local/
file /usr/local/lib/libgmp.10.dylib
/usr/local/lib/libgmp.10.dylib: Mach-O 64-bit dynamically linked shared library x86_64

2. CC=gcc-4.2 CXX=g++4.2 CFLAGS='-m64' ./configure
file /usr/local/lib/libgmp.10.dylib
/usr/local/lib/libgmp.10.dylib: Mach-O 64-bit dynamically linked shared library x86_64

(no change.. the problem is we want to link with a 32 bit perl, partly because that's the system provided one, partly because I don't want to recompile perl at this stage).

3. CC=gcc-4.2 CXX=g++4.2 CFLAGS='-m32' ./configure
..
configure: error: Oops, mp_limb_t is 32 bits, but the assembler code
in this configuration expects 64 bits.

...
reading
info ./doc/gmp.info

"By default GMP chooses the best ABI available for a given system,
and this generally gives significantly greater speed. But an ABI can
be chosen explicitly to make GMP compatible with other libraries, or
particular application requirements. For example,

./configure ABI=32
"
...
so..

4. CC=gcc-4.2 CXX=g++4.2 CFLAGS='-m32' ./configure ABI=32
make
make check
[all going well]
sudo make install
mw6$ file /usr/local/lib/libgmp.10.dylib
/usr/local/lib/libgmp.10.dylib: Mach-O dynamically linked shared library i386

that looks like a win.
Now try and install math::bigint::gmp in perl...
>perl512 ./Makefile.PL INC="-I/usr/local/include" LIBS="-L/usr/local/lib -lgmp"
Writing Makefile for Math::BigInt::GMP
>make
>make test
t/bigfltpm.t .. ok
t/bigintg.t ... ok
t/bigintpm.t .. ok
t/biglog.t .... ok
t/bigroot.t ... ok
t/pod.t ....... ok
t/pod_cov.t ... ok
t/storable.t .. ok
t/threads.t ... skipped: Perl compiled without ithreads
All tests successful.
Files=9, Tests=5537, 4 wallclock secs ( 0.64 usr 0.06 sys + 3.84 cusr 0.14 csys = 4.68 CPU)
Result: PASS

> sudo make install
Files found in blib/arch: installing files in blib/lib into architecture dependent library tree
Installing /Users/mw6/usr/local/lib/perl5/site_perl/5.12.0/darwin-2level/auto/Math/BigInt/GMP/GMP.bs
Installing /Users/mw6/usr/local/lib/perl5/site_perl/5.12.0/darwin-2level/auto/Math/BigInt/GMP/GMP.bundle
Installing /Users/mw6/usr/local/lib/perl5/site_perl/5.12.0/darwin-2level/Math/BigInt/GMP.pm
Installing /Users/mw6/usr/local/share/man/man3/Math::BigInt::GMP.3
Appending installation info to /Users/mw6/usr/local/lib/perl5/5.12.0/darwin-2level/perllocal.pod


WIN!

Relevant Links:
GMP: The GNU Multiple Precision Arithmetic Library
Math::BigInt::GMP: Use the GMP library for Math::BigInt routines (v1.32 23 Sept 2010)