Question

I'm trying to compile an application pipeline and link it with my library matlabengine, which acts as a wrapper to Matlab calls. Matlab uses it's own older version of libstc++ which is mixed in Matlab's lib folder .../bin/glnxa64

matlabengine compiles ok, but pipeline fails for unresolved symbols (because pipeline uses Matlab's libstdc++ version)

Basically i have two problem and solving either one should fix this:

  1. Remove arguments -leng -lmx -L/usr/local/MATLAB/R2013a/bin/glnxa64 from pipeline linking, which origin from subdirectory mex. When I compile matlabengine by hand and let cmake link it to pipeline, these flags do not appear and everything is fine. Why are these added to pipeline's linking when it works perfectly without them?

  2. Since Matlab's old libstc++ is used before the the system-wide libstdc++ i get unresolved symbols. fixing the order c++ searches for these libraries should also solve this.

Any help is much appreciated...

CMakeList.txt

link_directories("/usr/local/MATLAB/R2013a}/bin/glnxa64")
include_directories(mex)
add_subdirectory(mex)
ADD_EXECUTABLE( pipeline pipeline.cpp )
TARGET_LINK_LIBRARIES( pipeline matlabengine ${OpenCV_LIBS})

mex/CMakeList.txt (this should emulate what matlab's mex compiler wrapper does)

# mex specific c++ flags
set(MATLAB_PATH "/usr/local/MATLAB/R2013a")
set(CMAKE_CXX_COMPILER "gcc-4.4")
set(CMAKE_CXX_FLAGS "-D_GNU_SOURCE -fexceptions -DMX_COMPAT_32 -O -DNDEBUG -fPIC -DMATLAB_MEX_FILE")

include_directories("${MATLAB_PATH}/extern/include")
link_directories("${MATLAB_PATH}/bin/glnxa64")
add_library(matlabengine SHARED matlabengine.cpp)
target_link_libraries(matlabengine eng mx m)
Was it helpful?

Solution

Issue 1 can be solved by telling cmake to turn off transitive linking

mex/CMakeList.txt

set_target_properties(matlabengine PROPERTIES LINK_INTERFACE_LIBRARIES "")

Then, when linking the compiler will search for the library in the library path environment variable. Because of issue 2, the system standard library folder should precede the matlab library folder like so in linux:

export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:/usr/local/MATLAB/R2013a/bin/glnxa64/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top