0
votes

I am working on a 64-bit Ubuntu 14.04.3 machine with Matlab R2015a installed. GCC is:

gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-12ubuntu1) 

Matlab's mex is also configured to use exactly this compiler. Now, I would like to use the GNU GMP arbitrary precision arithmetic library. I downloaded the current version 6.0.0 and installed it:

./configure
make
make check
sudo make install

which all works fine; i get "gmp.h" and "libgmp.a" as a result. I can also write a small native C-program to demonstrate, that the GNU GMP library works:

#include <stdio.h>
#include "gmp.h"
int main() {
    mpz_t integ; /* Initialize the GMP variable */
    mpz_init(integ); /* Initialize the GMP variable */
    mpz_set_si(integ, 10); /* Assign a value */
    int test = (int)mpz_get_si(integ); /* Read the value into an int */
    printf("test = %.1f\n",(double)test); /*Print it */
return 0;
}

This program compiles and works with

gcc Test.c libgmp.a

as expected (libgmp.a and gmp.h are in the same dir).

However, I now want to create a C-program which I can compile using Matlab's mex compiler (The program should later interface to Matlab). Here is the adapted minimal example from above to be compiled with Matlab's mex:

#include <stdio.h>
#include "mex.h"
#include "gmp.h"
void mexFunction( int nlhs, mxArray *plhs[],
    int nrhs, const mxArray *prhs[])
{
    mpz_t integ; /* Initialize the GMP variable */
    mpz_init(integ); /* Initialize the GMP variable */
    mpz_set_si(integ, 10); /* Assign a value */
    int test = (int)mpz_get_si(integ); /* Read the value into an int */
    mexPrintf("test = %.1f\n",(double)test); /*Print it */
}

Again, libgmp.a and gmp.h are in the working directory. I now try to compile this program using Matlab's mex:

mex MatlabTest.c libgmp.a

However, I get this error:

Error using mex
/usr/bin/ld: libgmp.a(realloc.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
libgmp.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status

Remember, that both compilers are the same. I did recompile the GMP library with once the -fpic flag and once the -fPIC flag on, both times I got the same error from Matlab.

Matlab's build output (-v option in mex) is:

CXX="g++"
CFLAGS="-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32   -D_GNU_SOURCE -DMATLAB_MEX_FILE "
CXXFLAGS="-ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -DMX_COMPAT_32   -D_GNU_SOURCE -DMATLAB_MEX_FILE "
COPTIMFLAGS="-O -DNDEBUG"
CXXOPTIMFLAGS="-O -DNDEBUG"
CDEBUGFLAGS="-g"
CXXDEBUGFLAGS="-g"
LD="/usr/bin/gcc"
LDXX="g++"
LDFLAGS="-pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2015a/bin/glnxa64 -shared  libgmp.a  -L"/usr/local/MATLAB/R2015a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -Wl,--version-script,"/usr/local/MATLAB/R2015a/extern/lib/glnxa64/mexFunction.map""
LDDEBUGFLAGS="-g"
GCC : /usr/bin/gcc
CPPLIBS : /usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so
MATLABROOT : /usr/local/MATLAB/R2015a
ARCH : glnxa64
SRC : /home/mario/GMPtest/Matlab-Test/MatlabTest.c
OBJ : /tmp/mex_52646937964974_11644/MatlabTest.o
OBJS : /tmp/mex_52646937964974_11644/MatlabTest.o 
SRCROOT : /home/mario/GMPtest/Matlab-Test/MatlabTest
DEF : /tmp/mex_52646937964974_11644/MatlabTest.def
EXP : MatlabTest.exp
LIB : MatlabTest.lib
EXE : MatlabTest.mexa64
ILK : MatlabTest.ilk
MANIFEST : MatlabTest.mexa64.manifest
TEMPNAME : MatlabTest
EXEDIR : 
EXENAME : MatlabTest
OPTIM : -O -DNDEBUG
LINKOPTIM : -O
-------------------------------------------------------------------
Building with 'gcc'.
/usr/bin/gcc -c -DMX_COMPAT_32   -D_GNU_SOURCE -DMATLAB_MEX_FILE  -I"/usr/local/MATLAB/R2015a/extern/include" -I"/usr/local/MATLAB/R2015a/simulink/include" -ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/mario/GMPtest/Matlab-Test/MatlabTest.c -o /tmp/mex_52646937964974_11644/MatlabTest.o
/usr/bin/gcc -pthread -Wl,--no-undefined -Wl,-rpath-link,/usr/local/MATLAB/R2015a/bin/glnxa64 -shared  -O -Wl,--version-script,"/usr/local/MATLAB/R2015a/extern/lib/glnxa64/mexFunction.map" /tmp/mex_52646937964974_11644/MatlabTest.o   libgmp.a  -L"/usr/local/MATLAB/R2015a/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o MatlabTest.mexa64

Any ideas of why it won't compile with matlab's mex, although the underlying compilers are the same?

Best and many thanks! - Mario

1
The error message is quite clear, mex tries to produce a shared library (a plugin), so it cannot include any code not prepared for it (-fPIC), which archives usually aren't. Can't you use the systems GMP library (just -lgmp if you have the package libgmp-dev installed)? - Marc Glisse

1 Answers

0
votes

Aah, that's it - I had to compile the GMP library to produce shared libraries:

/configure --enable-shared --disable-static

Then, I get the GMP library as libgmp.so (and libgmp.so.10, libgmp.so.10.2.0) which can then be used to compile the C-file with mex:

mex MatlabTest.c libgmp.so

Thanks for the Tipp!