Category: Matlab

Build Matlab mex files with CMake

To use CMake to manage the build and install process of your mex files, you need to change the compilation rules of CMake. CMake does not support “mex” compiler naturally, so you need to insert the following commands in your CMakeLists.txt.


SET( CMAKE_CXX_COMPILER	mex )
SET( CMAKE_C_COMPILER	mex )

# Suffix and Prefix of the output target file
SET( CMAKE_SHARED_LIBRARY_SUFFIX .mexa64 )	# set suffix to .mexa64
SET( CMAKE_SHARED_LIBRARY_PREFIX )		# remove the "lib" prefix

# Variables controlling the build-phrase
SET( CMAKE_CXX_FLAGS "-cxx -largeArrayDims CXXFLAGS='$$CXXFLAGS -std=c++11'" )
SET( CMAKE_SHARED_LIBRARY_CXX_FLAGS )		# remove the -fPIC option. mex does not accept the "-fPIC" option

SET( CMAKE_CXX_COMPILE_OBJECT 
	"<CMAKE_CXX_COMPILER> <DEFINES> <FLAGS> -outdir <OBJECT_DIR> -c <SOURCE>; mv <OBJECT_DIR>/$$(basename <SOURCE> .cxx ).o <OBJECT>"
)

# Variables controlling the linking-phase
SET( CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS )	# remove -shared options. mex does not accept the "-shared" option

SET(
 	CMAKE_CXX_CREATE_SHARED_LIBRARY
	"<CMAKE_CXX_COMPILER> -cxx <LINK_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> -output <TARGET> <OBJECTS> <LINK_LIBRARIES>"
)

# Variables controlling the installation RPATH
SET( CMAKE_INSTALL_RPATH "\$ORIGIN" )
# CMake will reset RPATH at the installation phase, so we need to specify CMAKE_INSTALL_RPATH

MESSAGE( STATUS "mex.cmake is loaded.\n" )


## To add an mex file, you can:
#  ADD_LIBRARY( myprogram SHARED myprogram.cxx )
## Make sure you specify it as SHARED, and make sure you use the cxx extension.  

Advertisements

Setting RPATH for Matlab mex compilation

Platform: Linux
Situation:
A mex file seems to be compiled successfully, but at run-time Matlab issues error because dependent libraries cannot be found.

Reason:
At run-time, Matlab failed locate the correct dependent library for the mex file.

Solution:
(1) Set environmental variable LD_LIBRARY_PATH in Matlab, OR
(2) Set RPATH in the mex file during the linking procedure.

Set LD_LIBRARY_PATH:
Use the setenv and getenv command in Matlab.

Set RPATH:
You can insert the following option in the command line invokation of mex:
  LDFLAGS=’$LDFLAGS -Wl,-rpath=Your_RPATH’
For example:
  mex LDFLAGS=’$LDFLAGS -Wl,-rpath=Your_RPATH’ mycode.cxx
Comments:
  LDFLAGS=’$LDFLAGS …’ appends “…” to LDFLAGS after mex has setup its compilation variables, so Matlab’s setup is automatically get involved.
  -Wl,aaa,bbb (comma separated, not blanks) will be passed to ld as options aaa bbb (blank separated).

If you want to permanently add this to your mex compilation, you can modify the mexopts.sh script:
  A. Locate the mexopts.sh script, and locate the lines setting “RPATH”.
  B. Insert “-rpath-line,Your_RPATH,-rpath=Your_RPATH” to the value of RPATH. For example,
The original value might be: RPATH=”-Wl,-rpath-link,$TMW_ROOT/bin/$Arch”.
You can replace with:
RPATH=”-Wl,-rpath-link,/home/xxx/mylib:$TMW_ROOT/bin/$Arch,-rpath=/home/xxx/mylib:$TMW_ROOT/bin/$Arch”.

If you want to use relative paths, then use ‘\\\$ORIGIN’ to stand for the path of the mex file. For example:
in the command line
  mex LDFLAGS=’$LDFLAGS -Wl,-rpath=\\\$ORIGIN’ mycode.cxx
Or in the mexopts.sh
  RPATH=”-Wl,-rpath-link,$TMW_ROOT/bin/$Arch,-rpath=’\\\$ORIGIN’:$TMW_ROOT/bin/$Arch”.
In the compiled mex, you will find “$ORIGIN” is listed in the rpath of the mex file.
Comment: “\” is the escape character. After twice variable expansion, \\\$ORIGIN will become ‘$ORIGIN’

Inspect the RPATH of the mex file with “chrpath” command, with the “-l” option. For example,
chrpath -l myfunc.mex