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')];
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;
}