How to use existing C++ code with external library in MATLAB

12 views (last 30 days)
I got a C++ code (writen in Unix and compiled by gcc, works fine). The code uses the head files in the boost library, which I have installed in my Windows system. The main function of the code requires inputs of two file names, and write the solution a default text file.
Now, I want to use it in MATLAB under Windows OS. I have searched around and found maybe Mex could achieve this. But I have two difficulties:
  1. The return of the main function is void type. How to deal with this?
  2. It always complains that the head files from the boost library can't be found.
Thank you.
Part of the codes:
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <iomanip>
int main(int argc, char**argv) {
mallopt(M_MMAP_MAX, 0);
mallopt(M_TRIM_THRESHOLD, -1);
.......
......
print_summary(std::cout, attained_measures_precision_time);
if (out_base_name.compare("") != 0) {
std::string nome(out_base_name + "_paths.txt");
std::ofstream res_file(nome.c_str());
save_path_flows_solution(g, centroids, res_file, paths_matrix, D, all_centroids, p_star, edge_matrix);
res_file.close();
}
return 0;
}
  2 Comments
James Tursa
James Tursa on 20 Apr 2023
Can you post a minimal example that reproduces the problems?
Zhibin Deng
Zhibin Deng on 25 Apr 2023
Actually, I got more problems than this. Now, my plan is to compile the source C++ code into a DLL file and then use loadlibrary function to interface MATLAB with the functions in my C++ code. It turns out that, I can't even achieve this.
Here is my sample C++ source file "mylib.cpp"
// mylib.cpp
#include "mylib.h"
int add(int a, int b)
{
return a + b;
}
int multiply(int a, int b)
{
return a * b;
}
and the head file "mylib.h"
// mylib.h
#ifdef __cplusplus
extern "C" {
#endif
int add(int a, int b);
int multiply(int a, int b);
#ifdef __cplusplus
}
endif
I then complie the code in Windows CMD using the command
g++ -shared -o mylib.dll mylib.cpp
I sucessfully get the "mylib.dll" file. And in MATLAB 2022b, I issue the command
loadlibrary('mylib.dll','mylib.h')
The error message shows:
Error using loadlibrary
Building mylib_thunk_pcwin64 failed. Compiler output is:
C:\ProgramData\MATLAB\SupportPackages\R2022b\3P.instrset\mingw_w64.instrset\bin\gcc -I"D:\Program Files\MATLAB\R2022b\extern\include" -fexceptions -fno-omit-frame-pointer -I"C:\Users\Zhibin Deng\Documents\MATLAB\ASMO-ACG\ISMO"
-I"C:\Users\Zhibin Deng\Documents\MATLAB\ASMO-ACG\ISMO" "mylib_thunk_pcwin64.c" -o "mylib_thunk_pcwin64.dll" -shared
collect2.exe: fatal error: CreateProcess: No such file or directory
compilation terminated.
Would you please help me to solve this problem?

Sign in to comment.

Answers (0)

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!