How to properly include header Cpp files from ALGLIB in mex

8 views (last 30 days)
Hello guys,
I am struggling to get a mexfunction to work from C++ file. Below you can find my code first the mex file to compile and call and a header and the corresponding cpp file.
And now I have 2 questions:
  • A basic question; that code compiles and runs, but normaly I have learned to include the header files #include "Test.h" instead of #include "Test.cpp" in the cSpl_CPP.cpp file. But if I do so I get the following error, so I assume it does not find the Test function. Why is that so?
Error using mex
C:\Users\XXX\AppData\Local\Temp\mex_26596607590260_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0x35):
undefined reference to `Test()'
collect2.exe: error: ld returned 1 exit status
  • Further this is just a basic test to implement the ALGLIB C++ files, which is a numerical analysis C++ code. The part I want to implement can be found in the Test.h header file within #include "interpolation.h". If I uncomment //real_1d_array x_old = "[-1.0,-0.5,0.0,+0.5,+1.0]"; in the Test.cpp file, the function should access the interpolation.h enviroment to use any necessary function within the namespace alglib. But it returns the following error. Seems to me the same problem as above, it does not find the function. Why? By the way calling all this within a C++ enviroment does work, of course with an abstracted cSpl_CPP.cpp file.
Error using mex
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0x8f):
undefined reference to `alglib::real_1d_array::real_1d_array(char const*)'
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0xb0):
undefined reference to `alglib::real_1d_array::~real_1d_array()'
C:\Users\XXX\AppData\Local\Temp\mex_27656676634838_19292\cSpl_CPP.obj:cSpl_CPP.cpp:(.text+0xf7):
undefined reference to `alglib::real_1d_array::~real_1d_array()'
collect2.exe: error: ld returned 1 exit status
Just to avoid any problems with the compilation command; I use:
ipath = ['-I' fullfile(pwd,'cSpl','src')]; %Folder where the c & h files except cSpl_CPP.cpp are located
mex(ipath,'cSpl_CPP.cpp')
That works for Question 1, so it seems to have the correct folder included, as it does find the Test.cpp, though obviously not the Test.h...
For me it seems like I have a general problem including header files, but I don't know which one. Has any one an idea how to solve that issue???
Thanks a lot guys!
Greetings from Germany
Pablo
Mex-Function called cSpl_CPP.cpp to compile and call:
#include "math.h"
#include "matrix.h"
#include "mex.h"
#include "Test.cpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World!\n"); // prints !!!Hello World!!!
int res;
res = Test();
mexPrintf("Hello World @%d!\n",res); // prints !!!Hello World!!!
return;
}
Test header file Test.h:
#ifndef TEST_H_
#define TEST_H_
#include "interpolation.h"
using namespace alglib;
#include <iostream>
using namespace std;
int Test();
#endif /* TEST_H_ */
Test cpp file Test.cpp:
#include "Test.h"
int Test()
{
int c = 55;
cout << "Test works!" << endl;
//real_1d_array x_old = "[-1.0,-0.5,0.0,+0.5,+1.0]";
cout << "worked good\n";
return c;
}

Accepted Answer

Pablo Noever
Pablo Noever on 5 Nov 2020
Well actually the solution is to compile all cpp files that you will possibly use according to the command in Matlab, having all files in the same folder:
File = 'cSpl_CPP.cpp'; % Mexfunction
list = dir('**/*.cpp'); % Collect all cpp files
cppfiles = {list.name}; % Gather file names
cppfiles(find(strcmp(cppfiles,File))) = []; % delete the Mexfuntion from list
cppfiles = {File,cppfiles{:}}; % Put the Mexfuntion on the first position
mex(cppfiles{:}); % compile all files
of course all #include commands only contain *.h - files.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!