MATLAB: R2017b CMakeFile problem with Matlab Engine

ccmakecmakelistsmatlab enginer2017b

Hi there,
I need to buid a c++ code that includes some matlab engine features. Before doing that, I wanted to test with a simpler example: https://github.com/niosus/Matlab-Engine-CMake-Test
Instead of using custom cmake/FindMatlab.cmake in the original repo, I used FINDMATLAB command provided by Cmake 3.13.4. Here is the directory structure of the project:
Versions: MATLAB R2017b, CMake 3.13.4, Ubuntu 16.04.
- bin
-- test_function.m
- build (cmake)
- CMakeLists.txt (CMakeLists #1)
- README.md
- src
-- matlab_engine.cpp (https://github.com/niosus/Matlab-Engine-CMake-Test/blob/master/src/matlab_engine.cpp)
-- CMakeLists.txt (CMakeLists #2)
CMakeLists #1
cmake_minimum_required(VERSION 2.8)
project(MEX)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
find_package(Matlab REQUIRED ENG_LIBRARY MX_LIBRARY MAT_LIBRARY)
IF(MATLAB_FOUND)
message(STATUS "MATLAB Found, MATLAB MEX will be compiled.")
add_subdirectory(src)
ELSE(MATLAB_FOUND)
MESSAGE("MATLAB not found...nothing will be built.")
ENDIF(MATLAB_FOUND)
include_directories(${Matlab_INCLUDE_DIRS})
target_link_libraries(mat_engine ${MATLAB_ENG_LIBRARY} ${MATLAB_MX_LIBRARY} ${MATLAB_MAT_LIBRARY})
SET(CMAKE_CXX_FLAGS "-std=c++11")
CMakeLists #2
include_directories(${Matlab_INCLUDE_DIRS})
link_directories(${Matlab_LIBRARIES} ${MATLAB_ENG_LIBRARY})
add_executable (mat_engine matlab_engine.cpp)
target_link_libraries(mat_engine ${MATLAB_MAT_LIBRARY} ${MATLAB_ENG_LIBRARY} ${MATLAB_MX_LIBRARY})
message(STATUS ${MATLAB_ENG_LIBRARY})
message(STATUS ${MATLAB_MAT_LIBRARY})
SET(CMAKE_VERBOSE_MAKEFILE ON)
SET(CMAKE_CXX_FLAGS "-std=c++11")
Note that I can execute following command in MATLAB successfully:
mex -v GCC='/usr/bin/g++-4.9' -client engine matlab_engine.cpp
Although I make sure that I am using the correct g++ version for R2017b (4.9), I run into following error:
/usr/bin/g++-4.9 -std=c++11 -rdynamic CMakeFiles/mat_engine.dir/matlab_engine.cpp.o -o ../../bin/mat_engine -L/usr/local/MATLAB/R2017b/bin/glnxa64/libmex.so -L/usr/local/MATLAB/R2017b/bin/glnxa64/libmx.so -L/usr/local/MATLAB/R2017b/bin/glnxa64/libeng.so -L/usr/local/MATLAB/R2017b/bin/glnxa64/libmat.so
CMakeFiles/mat_engine.dir/matlab_engine.cpp.o: In function `main':
matlab_engine.cpp:(.text+0x20): undefined reference to `mxCreateDoubleMatrix'
matlab_engine.cpp:(.text+0x38): undefined reference to `mxCreateDoubleMatrix'
matlab_engine.cpp:(.text+0x48): undefined reference to `mxGetData'
matlab_engine.cpp:(.text+0x58): undefined reference to `mxGetData'
matlab_engine.cpp:(.text+0x88): undefined reference to `engOpen'
matlab_engine.cpp:(.text+0xd5): undefined reference to `engPutVariable'
matlab_engine.cpp:(.text+0xea): undefined reference to `engPutVariable'
matlab_engine.cpp:(.text+0xfb): undefined reference to `engEvalString'
matlab_engine.cpp:(.text+0x10c): undefined reference to `engGetVariable'
matlab_engine.cpp:(.text+0x11c): undefined reference to `mxGetData'
collect2: error: ld returned 1 exit status
src/CMakeFiles/mat_engine.dir/build.make:86: recipe for target '../bin/mat_engine' failed
make[2]: *** [../bin/mat_engine] Error 1
CMakeFiles/Makefile2:90: recipe for target 'src/CMakeFiles/mat_engine.dir/all' failed
make[1]: *** [src/CMakeFiles/mat_engine.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I'd be greatful if someone can point out the problem and offer a solution. It seems like there is not a problem with linking target libraries, yet executable cannot be created.

Best Answer

Hi i had the same issue and manage to fix it.
Here you see you will need to link the following libraries;
1.1)libmat.so 1.2)libmx.so , specially your error coming from missing libmx.so the matrix library.
you need to add the (Linux) export LD_LIBRARY_PATH = matlabroot/bin/glnxa64:matlabroot/sys/os/glnxa64
to your env, or rather in your ~/.profile file.
This 2 steps will fix the errors you have.
Related Question