Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to link during Matlab's MEX compilation
    text
    copied!<p>I've written a program of the following form:</p> <pre><code>#include "stuff_I_need.h" int main(){ construct_array(); // uses OpenMP pragma's print_array(); return(0); } </code></pre> <p>that compiles, links, and runs correctly with the following command:</p> <pre><code>`gcc44 -I/home/matteson/sundials/include/ main.c -lm -L/home/matteson/sundials/lib -lsundials_cvode -lsundials_nvecserial -fopenmp -o /home/matteson/MPI_test/CVODE_test/main_test` </code></pre> <p>"gcc44" is simply gcc version 4.4 and is named like this because it's being compiled on a cluster that maintains several versions of gcc. The libraries sundials_cvode and sundials_nvecserial are used in the solving of several differential equations during the construction of the array.</p> <p>Now when I want to transfer over to Matlab and try to compile the mex file of the form:</p> <pre><code>#include "stuff_I_need.h" void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[]){ construct_array(); // uses OpenMP pragma's print_array(); } </code></pre> <p>and try to compile with the following command in Matlab:</p> <pre><code>&gt;&gt; mex -v CC="gcc44" CFLAGS="\$CFLAGS -I/home/matteson/sundials/include/ -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp -L/home/matteson/sundials/lib -lsundials_cvode -lsundials_nvecserial" mex_cvode.c </code></pre> <p>I get the following messages culminating in a link error:</p> <pre><code> -&gt; mexopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION) FILE = /home/matteson/.matlab/R2010b/mexopts.sh ---------------------------------------------------------------- -&gt; MATLAB = /misc/linux/64/opt/pkg/matlab/R2010b -&gt; CC = gcc44 -&gt; CC flags: CFLAGS = -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -I/home/matteson/sundials/include/ -L/home/matteson/sundials/lib -lsundials_cvode -L/home/matteson/sundials/lib -lsundials_nvecserial CDEBUGFLAGS = -g COPTIMFLAGS = -O -DNDEBUG CLIBS = -Wl,-rpath-link,/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -L/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++ arguments = -DMX_COMPAT_32 -&gt; CXX = g++ -&gt; CXX flags: CXXFLAGS = -ansi -D_GNU_SOURCE -fPIC -fno-omit-frame-pointer -pthread CXXDEBUGFLAGS = -g CXXOPTIMFLAGS = -O -DNDEBUG CXXLIBS = -Wl,-rpath-link,/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -L/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -lmx -lmex -lmat -lm arguments = -DMX_COMPAT_32 -&gt; FC = g95 -&gt; FC flags: FFLAGS = -fexceptions -fPIC -fno-omit-frame-pointer FDEBUGFLAGS = -g FOPTIMFLAGS = -O FLIBS = -Wl,-rpath-link,/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -L/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -lmx -lmex -lmat -lm arguments = -DMX_COMPAT_32 -&gt; LD = gcc44 -&gt; Link flags: LDFLAGS = -pthread -shared -Wl,--version-script,/misc/linux/64/opt/pkg/matlab/R2010b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -fopenmpofopenmp LDDEBUGFLAGS = -g LDOPTIMFLAGS = -O LDEXTENSION = .mexa64 arguments = -&gt; LDCXX = -&gt; Link flags: LDCXXFLAGS = LDCXXDEBUGFLAGS = LDCXXOPTIMFLAGS = LDCXXEXTENSION = arguments = ---------------------------------------------------------------- Warning: You are using gcc version "4.4.4". The version currently supported with MEX is "4.3.4". For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release/ -&gt; gcc44 -c -I/misc/linux/64/opt/pkg/matlab/R2010b/extern/include -I/misc/linux/64/opt/pkg/matlab/R2010b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -I/home/matteson/sundials/include/ -L/home/matteson/sundials/lib -lsundials_cvode -L/home/matteson/sundials/lib -lsundials_nvecserial -DMX_COMPAT_32 -O -DNDEBUG "mex_cvode.c" -&gt; gcc44 -O -pthread -shared -Wl,--version-script,/misc/linux/64/opt/pkg/matlab/R2010b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -fopenmpofopenmp -o "mex_cvode.mexa64" mex_cvode.o -Wl,-rpath-link,/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -L/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++ mex_cvode.o: In function `mexFunction': mex_cvode.c:(.text+0x2b2): undefined reference to `N_VNew_Serial' mex_cvode.c:(.text+0x2db): undefined reference to `N_VNew_Serial' mex_cvode.c:(.text+0x35b): undefined reference to `CVodeCreate' mex_cvode.c:(.text+0x39c): undefined reference to `CVodeInit' mex_cvode.c:(.text+0x3dd): undefined reference to `CVodeSVtolerances' mex_cvode.c:(.text+0x412): undefined reference to `CVodeSetUserData' mex_cvode.c:(.text+0x449): undefined reference to `CVDense' mex_cvode.c:(.text+0x482): undefined reference to `CVDlsSetDenseJacFn' mex_cvode.c:(.text+0x50c): undefined reference to `CVode' mex_cvode.c:(.text+0x5b4): undefined reference to `N_VDestroy_Serial' mex_cvode.c:(.text+0x5c0): undefined reference to `N_VDestroy_Serial' mex_cvode.c:(.text+0x5cc): undefined reference to `CVodeFree' collect2: ld returned 1 exit status mex: link of ' "mex_cvode.mexa64"' failed. ??? Error using ==&gt; mex at 208 Unable to complete successfully. </code></pre> <p>Somehow, I'm not giving the correct flags to link appropriately. As I get the same set of errors (plus a few more) if I remove the commands to link in the gcc44 command, I'm pretty sure that I'm not getting the compiler to "see" the libraries.</p> <p>My questions are :</p> <ul> <li>If my analysis of the error is correct, what flags do I need to pass to the mex compilation command to successfully link? </li> <li>Alternatively, what are the gcc flags to compile and link outside of the Matlab environment to compile a .mex64 executable?</li> <li>If my analysis is wrong, where to go from here?</li> </ul> <p>I think I've ruled out the unsupported compiler warning since I've been able to compile simple mex with OpenMP programs using gcc 4.4, but these did not have to link against anything except the math library. Also, if I compile with version gcc version 4.1.2 or 4.3.4 with or without the "-fopenmp" flags I get the same error.</p> <p>In the end I do need version 4.4 because of certain OpenMP support that did not appear in prior versions.</p> <p>Thanks in advance for the help.</p> <p>--Andrew</p> <p>Edits: (@KWATFORD)</p> <p>So I tried the command with the statements outside the the quotes, and got the error:</p> <pre><code>-&gt; gcc44 -c -I/home/matteson/sundials/include/ -I/misc/linux/64/opt/pkg/matlab/R2010b/extern/include -I/misc/linux/64/opt/pkg/matlab/R2010b/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -fexceptions -fPIC -fno-omit-frame-pointer -pthread -fopenmp -DMX_COMPAT_32 -O -DNDEBUG "mex_cvode.c" -&gt; gcc44 -O -pthread -shared -Wl,--version-script,/misc/linux/64/opt/pkg/matlab/R2010b/extern/lib/glnxa64/mexFunction.map -Wl,--no-undefined -fopenmp -o "mex_cvode.mexa64" mex_cvode.o -L/home/matteson/sundials/lib -lsundials_cvode -lsundials_nvecserial -Wl,-rpath-link,/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -L/misc/linux/64/opt/pkg/matlab/R2010b/bin/glnxa64 -lmx -lmex -lmat -lm -lstdc++ /usr/bin/ld: /home/matteson/sundials/lib/libsundials_cvode.a(cvode.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC /home/matteson/sundials/lib/libsundials_cvode.a: could not read symbols: Bad value collect2: ld returned 1 exit status mex: link of ' "mex_cvode.mexa64"' failed. ??? Error using ==&gt; mex at 208 Unable to complete successfully. </code></pre> <p>I'm a bit confused about the suggestion to recompile with "-fPIC" because when I look at the gcc44 command I see the -fPIC as an option.</p> <p>Are they saying to recompile the library with -fPIC?</p> <p>I don't have the source for the library, if the suggestion is to recompile the library is there a workaround?</p> <p>What does "relocation against local object" mean?</p> <p>My continued thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload