MATLAB: Calling MATLAB Functions from C++ Application Built with CMake in Visual Studio

ccmakeMATLABmatlab apimatlab enginevisual studio

I am trying to follow the example from: https://www.mathworks.com/help/matlab/calling-matlab-engine-from-cpp-programs.html to call MATLAB code from a C++ application I have built in Visual Studio.
I've made some modifications to the example. I am using an h and cpp file as well as using cmake for the build.
I have testFeval.h
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>
void callSQRT();
testFeval.cpp
#include "testFeval.h"
void callSQRT() {
using namespace matlab::engine;
// Start MATLAB engine synchronously
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB();
//Create MATLAB data array factory
matlab::data::ArrayFactory factory;
// Define a four-element typed array
matlab::data::TypedArray<double> const argArray =
factory.createArray({ 1,4 }, { -2.0, 2.0, 6.0, 8.0 });
// Call MATLAB sqrt function on the data array
matlab::data::Array const results = matlabPtr->feval(u"sqrt", argArray);
// Display results
for (int i = 0; i < results.getNumberOfElements(); i++) {
double a = argArray[i];
std::complex<double> v = results[i];
double realPart = v.real();
double imgPart = v.imag();
std::cout << "Square root of " << a << " is " <<
realPart << " + " << imgPart << "i" << std::endl;
}
}
I am using CMake to try and run the MATLAB engine from Visual Studio.
My top level CMake file is:
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject1" C CXX)
# Find Matlab
FIND_PACKAGE(Matlab REQUIRED)
# Include sub-projects.
INCLUDE_DIRECTORIES("${Matlab_ROOT_DIR}/extern/include")
add_subdirectory ("src") #Main Project
The project CMake is:
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
set(HDR
MATLABExampleCall/testFeval.h
CMakeProject1.h
) #List all header files. This way project is rebuilt if a header file is modified.
set(SRC
MATLABExampleCall/testFeval.cpp
CMakeProject1.cpp
) #List all source files
add_executable (CMakeProject1 ${SRC} ${HDR}) #Executable uses all source and header files
I'm not sure what I'm doing wrong with my structure/usage of the MATLAB Engine? I am able to include hpp files, yet I'm getting Linker errors when I try to build. Is there something I'm missing? Ideally when this is working I want to use the same engine to instantiate MATLAB objects and call their methods.

Best Answer

So I ended up getting the CMakeLists.txt to work correctly with MATLAB C++ API, Visual Studio, & Linux. Posting the solution to my issue for anyone else.
My CMakeList.txt in the project directory looks like this now:
add_executable(${PROJECT_NAME} ${SRC} ${HDR})
# Link C++ API
if(WIN32)
message(STATUS "Linking WINDOWS C++")
target_link_libraries(${PROJECT_NAME} ${Matlab_ROOT_DIR}/extern/lib/win64/microsoft/libMatlabEngine.lib)
target_link_libraries(${PROJECT_NAME} ${Matlab_ROOT_DIR}/extern/lib/win64/microsoft/libMatlabDataArray.lib)
endif(WIN32)
if(UNIX)
message(STATUS "Linking UNIX C++")
target_link_libraries(${PROJECT_NAME} ${Matlab_ROOT_DIR}/extern/bin/glnxa64/libMatlabEngine.so)
target_link_libraries(${PROJECT_NAME} ${Matlab_ROOT_DIR}/extern/bin/glnxa64/libMatlabDataArray.so)
find_package(Threads)
target_link_libraries(${PROJECT_NAME} ${CMAKE_THREAD_LIBS_INIT})
endif(UNIX)
This fixes all the issues with the lines:
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
And I'm able to start up a session of MATLAB & work with the engine now.