Could not find matlabEngine using the following names: libMatlabEngine

10 views (last 30 days)
Hi,
I am currently trying to run some MATLAB code in a c++ project. Obviously, to make things more complicated I am also new to both c++ and cmake.
So here is my top-Level CMakelists.txt:
cmake_minimum_required(VERSION 3.19)
project(matlabapi)
set(CMAKE_CXX_STANDARD 14)
# sets the matlab root directory
set(Matlab_ROOT_DIR /usr/local/MATLAB/R2021a)
# included directories
include_directories(${Matlab_ROOT_DIR}/extern/bin/glnxa64/)
# print all included directories
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "Included Directory is: '${dir}'")
endforeach()
# Libraries
find_library(matlabEngine libMatlabEngine REQUIRED)
# Executable
add_executable(matlabapi main.cpp)
# Linker stuff
target_link_libraries(matlabapi matlabEngine)
and the main.cpp which is the executable:
#include <iostream>
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
From my understanding, Cmake should find the library here:
/usr/local/MATLAB/R2021a/extern/bin/glnxa64/
which includes two files:
libMatlabDataArray.so libMatlabEngine.so
What am I missing? Cmake throws the error in cmake configuration time:
CMake Error at CMakeLists.txt:17 (find_library):
Could not find matlabEngine using the following names: libMatlabEngine

Accepted Answer

Kojiro Saito
Kojiro Saito on 30 Jun 2021
According to this document, we can know which compile options to be passed to g++.
g++ -std=c++11 -I <matlabroot>/extern/include/ -L <matlabroot>/extern/bin/glnxa64/ -pthread myEngineApp.cpp -lMatlabDataArray -lMatlabEngine
And when executing the MATLAB Engine API for C++, we need to add LD_LIBRARY_PATH.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:matlabroot/extern/bin/glnxa64:matlabroot/sys/os/glnxa64
With that, CMakeLists.txt would be the following.
cmake_minimum_required(VERSION 3.19)
project(matlabapi)
# As of R2021a, C++ version ~11 and 14 are supported
set(CMAKE_CXX_STANDARD 14)
# To avoid "undefined reference to symbol 'pthread_create@@GLIBC_x.x.x" error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
# Set the matlab root directory
set(Matlab_ROOT_DIR /usr/local/MATLAB/R2021a)
# Set LD_LIBRARY_PATH for execution
set(LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${Matlab_ROOT_DIR}/extern/bin/glnxa65:${Matlab_ROOT_DIR}/sys/os/glnxa64)
# Included directories
include_directories(${Matlab_ROOT_DIR}/extern/include/)
# Print all included directories
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message(STATUS "Included Directory is: '${dir}'")
endforeach()
# Add linker search directories
link_directories(${Matlab_ROOT_DIR}/extern/bin/glnxa64)
# Executable
add_executable(matlabapi main.cpp)
# Linker stuff
# Linker to libMatlabDataArray in link_directories
target_link_libraries(matlabapi MatlabDataArray)
# Linker to libMatlabEngine in link_directories
target_link_libraries(matlabapi MatlabEngine)
Next, create a build directory and do cmake.
mkdir build
cd build
cmake ..
You will get CMakeCache.txt, CMakeFiles, cmake_install.cmake and Makefile. Build this file.
make
You will get matlabapi execution file. Then, run
./matlabapi

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!