MATLAB: How to control the order of linking arguments in MEX

MATLABmexmex compiler

Does anyone know how I can get mex to maintain the linking order of library files? For example, I am trying to compile a mex file with a static and a shared library:
mex -n -largeArrayDims -I${PROJECT_SOURCE_DIR}/include -L${PROJECT_SOURCE_DIR}/lib -ldataloop /usr/local/cuda/lib64/libcudart.so -outdir ${PROJECT_SOURCE_DIR}/bin ${PROJECT_SOURCE_DIR}/src/mex_dataloop.cpp
However, the 3 command lines produced are
Building with 'g++'.
Warning: You are using gcc version '4.8.2'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
Warning: You are using gcc version '4.8.2-19ubuntu1)'. The version of gcc is not supported. The version currently supported with MEX is '4.7.x'. For a list of currently supported compilers see: http://www.mathworks.com/support/compilers/current_release.
/usr/bin/g++ -c -D_GNU_SOURCE -DMATLAB_MEX_FILE -I/home/samarth/Documents/sandbox/mex_dataloop/build/..//include -I"/usr/local/MATLAB/R2014b/extern/include" -I"/usr/local/MATLAB/R2014b/simulink/include" -ansi -fexceptions -fPIC -fno-omit-frame-pointer -pthread -O -DNDEBUG /home/samarth/Documents/sandbox/mex_dataloop/build/..//src/mex_dataloop.cpp -o /tmp/mex_289352688213564_29302/mex_dataloop.o
/usr/bin/g++ -pthread -Wl,--no-undefined -shared -O -Wl,--version-script,"/usr/local/MATLAB/R2014b/extern/lib/glnxa64/mexFunction.map" /tmp/mex_289352688213564_29302/mex_dataloop.o /usr/local/cuda/lib64/libcudart.so -ldataloop -L/home/samarth/Documents/sandbox/mex_dataloop/build/..//lib -Wl,-rpath-link,/usr/local/MATLAB/R2014b/bin/glnxa64 -L"/usr/local/MATLAB/R2014b/bin/glnxa64" -lmx -lmex -lmat -lm -lstdc++ -o /home/samarth/Documents/sandbox/mex_dataloop/build/..//bin/mex_dataloop.mexa64
rm -f /tmp/mex_289352688213564_29302/mex_dataloop.o
MEX completed successfully.
Note how in the linking phase (i.e. second command line),
/usr/local/cuda/lib64/libcudart.so -ldataloop
appears before
-L/home/samarth/Documents/sandbox/mex_dataloop/build/..//lib
which causes the linking to fail. I want
-L/home/samarth/Documents/sandbox/mex_dataloop/build/..//lib -ldataloop /usr/local/cuda/lib64/libcudart.so
but cannot figure out how to acheive that through mex. Thanks!

Best Answer

Couldn't figure out how change the order, so I solved the problem by compiling libdataloop to be shared.
Related Question