Clear Filters
Clear Filters

Undefined referece when calling MATLAB from C++ (cmake)

23 views (last 30 days)
Hello! I am trying to call MATLAB from C++ within a project made in the Clion environment but I get many "undefined reference to..." errirs. (using Matlab R2022a, Windows machine)
Since the same code works on one of my computers but not on the other, I think that there is some misconfiguration problem.
Test program example:
My CMakeList.txt file is:
cmake_minimum_required(VERSION 3.22)
project(prova2)
set(CMAKE_CXX_STANDARD 14)
include_directories("C:/Program Files/MATLAB/R2022a/extern/include/")
add_executable(prova2 main.cpp)
target_link_libraries(${PROJECT_NAME} "C:/Program Files/MATLAB/R2022a/extern/lib/win64/mingw64/libMatlabEngine.lib")
target_link_libraries(${PROJECT_NAME} "C:/Program Files/MATLAB/R2022a/extern/lib/win64/mingw64/libMatlabDataArray.lib")
The main.cpp file is the following:
#include <iostream>
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
The output messages when I build the project are:
"C:\Program Files\JetBrains\CLion 2022.1.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\valen\Documents\prova2\cmake-build-debug --target all -j 9
[1/1] Linking CXX executable prova2.exe
FAILED: prova2.exe
cmd.exe /C "cd . && C:\PROGRA~2\MINGW-~1\I686-8~1.0-P\mingw32\bin\C__~1.EXE -g CMakeFiles/prova2.dir/main.cpp.obj -o prova2.exe -Wl,--out-implib,libprova2.dll.a -Wl,--major-image-version,0,--minor-image-version,0 "C:/Program Files/MATLAB/R2022a/extern/lib/win64/mingw64/libMatlabEngine.lib" "C:/Program Files/MATLAB/R2022a/extern/lib/win64/mingw64/libMatlabDataArray.lib" -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
CMakeFiles/prova2.dir/main.cpp.obj: In function `setPropertyAsyncImpl':
C:/Program Files/MATLAB/R2022a/extern/include/MatlabEngine/detail/matlab_engine_impl.hpp:210: undefined reference to `cpp_engine_feval_with_completion'
C:/Program Files/MATLAB/R2022a/extern/include/MatlabEngine/detail/matlab_engine_impl.hpp:212: undefined reference to `cpp_engine_cancel_feval_with_completion'
CMakeFiles/prova2.dir/main.cpp.obj: In function `initSession':
C:/Program Files/MATLAB/R2022a/extern/include/MatlabEngine/detail/engine_factory_impl.hpp:20: undefined reference to `cpp_engine_create_session'
CMakeFiles/prova2.dir/main.cpp.obj: In function `ZN6matlab9execution30convertUTF8StringToUTF16StringERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE':
C:/Program Files/MATLAB/R2022a/extern/include/MatlabEngine/engine_interface_util.hpp:29: undefined reference to `util_utf8_to_utf16'
C:/Program Files/MATLAB/R2022a/extern/include/MatlabEngine/engine_interface_util.hpp:30: undefined reference to `util_destroy_utf16'
... and many others...
CMakeFiles/prova2.dir/main.cpp.obj:C:/Program Files/MATLAB/R2022a/extern/include/MatlabDataArray/detail/ExceptionHelpers.hpp:79: more undefined references to `get_function_ptr' follow
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
What could be the issue?
Thank you in advance!

Answers (1)

Ramtej
Ramtej on 13 Sep 2023
Hi Mario,
As per my understanding, you are trying to call MATLAB from C++.
The error you are encountering might be due to a misconfiguration in your environment setup. Make sure to properly configure and test your environment before proceeding with building your project.
You can leverage the below documentation for building and testing your environment:
Hope this resolves your query!

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!