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 statussrc/CMakeFiles/mat_engine.dir/build.make:86: recipe for target '../bin/mat_engine' failedmake[2]: *** [../bin/mat_engine] Error 1CMakeFiles/Makefile2:90: recipe for target 'src/CMakeFiles/mat_engine.dir/all' failedmake[1]: *** [src/CMakeFiles/mat_engine.dir/all] Error 2Makefile:83: recipe for target 'all' failedmake: *** [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