From: Nicholas Kinar on
Hello---

I've written some code in C++ which I would like to compile with the
Matlab MEX compiler. I am using the current release of Matlab (R2010a,
32-bit), which is running on Mac OS X 10.6.3 (Snow Leopard). I've
installed the most current version of the XCode tools (3.2.2, 64-bit),
which ships with i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc.
build 5659).

The code that I've written in C++ links with the MPFR and GMP libraries,
which provide capabilities for handling large numbers using
arbitrary-precision arithmetic. I am also using the MPFR C++ wrapper
(http://www.holoborodko.com/pavel/?page_id=12).

To compile my code, I invoke MEX in the following way:

mex -I/usr/local/include mpreal.cpp T_Zeta.cpp -lmpfr -lgmp -v -g

However, although I've compiled, checked and installed the MPFR and GMP
libraries, the MEX invocation fails in the following manner.

Why is MEX complaining about "Undefined symbols"? What would I have to
do to ensure that the code will compile?

Here is the output of the MEX invocation:



mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION)
FILE = /Users/nicholaskinar/.matlab/R2010a/mexopts.sh
----------------------------------------------------------------
-> MATLAB = /Applications/MATLAB_R2010aSV.app
-> CC = gcc-4.0
-> CC flags:
CFLAGS = -fno-common -no-cpp-precomp -arch i386
-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
-fexceptions
CDEBUGFLAGS = -g
COPTIMFLAGS = -O2 -DNDEBUG
CLIBS =
-L/Applications/MATLAB_R2010aSV.app/bin/maci -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> CXX = g++-4.0
-> CXX flags:
CXXFLAGS = -fno-common -no-cpp-precomp -fexceptions
-arch i386 -isysroot /Developer/SDKs/MacOSX10.5.sdk
-mmacosx-version-min=10.5
CXXDEBUGFLAGS = -g
CXXOPTIMFLAGS = -O2 -DNDEBUG
CXXLIBS =
-L/Applications/MATLAB_R2010aSV.app/bin/maci -lmx -lmex -lmat -lstdc++
arguments = -DMX_COMPAT_32
-> FC = gfortran
-> FC flags:
FFLAGS = -fexceptions -fbackslash
FDEBUGFLAGS = -gdwarf-2
FOPTIMFLAGS = -O
FLIBS =
-L/Applications/MATLAB_R2010aSV.app/bin/maci -lmx -lmex -lmat -L
-lgfortran -L -lgfortranbegin
arguments = -DMX_COMPAT_32
-> LD = gcc-4.0
-> Link flags:
LDFLAGS = -Wl,-twolevel_namespace -undefined error
-arch i386 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk
-mmacosx-version-min=10.5 -bundle
-Wl,-exported_symbols_list,/Applications/MATLAB_R2010aSV.app/extern/lib/maci/mexFunction.map
LDDEBUGFLAGS = -g
LDOPTIMFLAGS = -O
LDEXTENSION = .mexmaci
arguments = -lmpfr -lgmp
-> LDCXX =
-> Link flags:
LDCXXFLAGS =
LDCXXDEBUGFLAGS =
LDCXXOPTIMFLAGS =
LDCXXEXTENSION =
arguments = -lmpfr -lgmp
----------------------------------------------------------------

-> g++-4.0 -c -I/usr/local/include
-I/Applications/MATLAB_R2010aSV.app/extern/include
-I/Applications/MATLAB_R2010aSV.app/simulink/include -DMATLAB_MEX_FILE
-fno-common -no-cpp-precomp -fexceptions -arch i386 -isysroot
/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -DMX_COMPAT_32
-g "mpreal.cpp"

-> g++-4.0 -c -I/usr/local/include
-I/Applications/MATLAB_R2010aSV.app/extern/include
-I/Applications/MATLAB_R2010aSV.app/simulink/include -DMATLAB_MEX_FILE
-fno-common -no-cpp-precomp -fexceptions -arch i386 -isysroot
/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 -DMX_COMPAT_32
-g "T_Zeta.cpp"

-> gcc-4.0 -g -Wl,-twolevel_namespace -undefined error -arch i386
-Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5
-bundle
-Wl,-exported_symbols_list,/Applications/MATLAB_R2010aSV.app/extern/lib/maci/mexFunction.map
-o "mpreal.mexmaci" mpreal.o T_Zeta.o -lmpfr -lgmp
-L/Applications/MATLAB_R2010aSV.app/bin/maci -lmx -lmex -lmat -lstdc++

Undefined symbols:
"_mpfr_mul_d", referenced from:
mpfr::mpreal::operator*=(double)in T_Zeta.o
"_mpfr_div_d", referenced from:
mpfr::mpreal::operator/=(double)in T_Zeta.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

mex: link of ' "mpreal.mexmaci"' failed.

??? Error using ==> mex at 222
Unable to complete successfully.


From: Nicholas Kinar on
> Undefined symbols:
> "_mpfr_mul_d", referenced from:
> mpfr::mpreal::operator*=(double)in T_Zeta.o
> "_mpfr_div_d", referenced from:
> mpfr::mpreal::operator/=(double)in T_Zeta.o
> ld: symbol(s) not found
> collect2: ld returned 1 exit status
>
> mex: link of ' "mpreal.mexmaci"' failed.
>
> ??? Error using ==> mex at 222
> Unable to complete successfully.


Perhaps the undefined symbols could be an issue with linking between
32-bit and 64-bit software, as claimed in this thread:

http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/260419

However, how might I force the GMP and MPFR libraries to compile as
32-bit on Snow Leopard? This might be required to be able to compile
MEX file on 32-bit Matlab.

From: Nicholas Kinar on
>
> Perhaps the undefined symbols could be an issue with linking between
> 32-bit and 64-bit software, as claimed in this thread:
>
> http://www.mathworks.co.kr/matlabcentral/newsreader/view_thread/260419
>
> However, how might I force the GMP and MPFR libraries to compile as
> 32-bit on Snow Leopard? This might be required to be able to compile MEX
> file on 32-bit Matlab.
>

I forced the GMP and MPFR libraries to compile as 32-bit by passing the
following arguments during the ./configure process.

CFLAGS="-arch i386"
LDFLAGS="-arch i386"

For the GMP library, I also had to pass ABI=32.

This created 32-bit libraries. I tried once again running MEX in
Matlab, and I found that the error still occurs.

mex -I/usr/local/include T_Zeta.cpp mpreal.cpp -lmpfr -lgmp -v -g

Undefined symbols:
"_mpfr_mul_d", referenced from:
mpfr::mpreal::operator*=(double)in T_Zeta.o
"_mpfr_div_d", referenced from:
mpfr::mpreal::operator/=(double)in T_Zeta.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

mex: link of ' "T_Zeta.mexmaci"' failed.

So why am I still getting unresolved symbols during linking of the MEX
files?
From: Nicholas Kinar on
> So why am I still getting unresolved symbols during linking of the MEX
> files?

So now what I did was run the following at the Matlab command-line:

mex -I/usr/local/include T_Zeta.cpp mpreal.cpp /usr/local/lib/libgmp.a
/usr/local/lib/libmpfr.a -v -g

What I did was replace the "-l" switch with the absolute path to the
library.

However, now the following linking error results:

ld: absolute addressing (perhaps -mdynamic-no-pic) used in ___gmpn_add_n
from /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/libgmp.a(add_n.o) not
allowed in slidable image. Use '-read_only_relocs suppress' to enable
text relocs
collect2: ld returned 1 exit status

mex: link of ' "T_Zeta.mexmaci"' failed.


What does this mean, and how do I get rid of this error?

From: Nicholas Kinar on
On 10-04-09 2:45 PM, Nicholas Kinar wrote:
>> So why am I still getting unresolved symbols during linking of the MEX
>> files?
>
> So now what I did was run the following at the Matlab command-line:
>
> mex -I/usr/local/include T_Zeta.cpp mpreal.cpp /usr/local/lib/libgmp.a
> /usr/local/lib/libmpfr.a -v -g
>
> What I did was replace the "-l" switch with the absolute path to the
> library.
>
> However, now the following linking error results:
>
> ld: absolute addressing (perhaps -mdynamic-no-pic) used in ___gmpn_add_n
> from /Developer/SDKs/MacOSX10.5.sdk/usr/local/lib/libgmp.a(add_n.o) not
> allowed in slidable image. Use '-read_only_relocs suppress' to enable
> text relocs
> collect2: ld returned 1 exit status
>
> mex: link of ' "T_Zeta.mexmaci"' failed.
>
>
> What does this mean, and how do I get rid of this error?
>

So now what I did was try to link with dynamic libraries:

mex -I/usr/local/include T_Zeta.cpp mpreal.cpp
/usr/local/lib/libgmp.dylib /usr/local/lib/libmpfr.dylib -v -g

Surprisingly, this compiles!

However, when I try to run the MEX file, I get the following error,
which complains of a library not being loaded:

[re, im] = T_Zeta(13.000)

??? Invalid MEX-file
'/Volumes/FILES/DEVELOPMENT/reconstruction-algorithms/calculate-T-Zeta/T_Zeta.mexmaci':
dlopen(/Volumes/FILES/DEVELOPMENT/reconstruction-algorithms/calculate-T-Zeta/T_Zeta.mexmaci,
1): Library not loaded: /usr/local/lib/libgmp.3.dylib
Referenced from:
/Volumes/FILES/DEVELOPMENT/reconstruction-algorithms/calculate-T-Zeta/T_Zeta.mexmaci
Reason: Incompatible library version: T_Zeta.mexmaci requires version
9.0.0 or later, but libgmp.3.dylib provides version 8.0.0.

How do I get rid of this error? What does this error mean? I am fairly
certain that both libraries are of the same version.