I am having issues writing a mex file for simulink using c++ and armadillo.
I'm using the Legacy Code Tool to compile a classical c++ file and use it as a block function in simulink.
Everything works fine except when I try to use the pinv() function provided by the armadillo library. I am trying to execute the following code:
1 2 3 4 5 6
p = pinv(unit);
But I obtain an error message by matlab saying that there has been a segmentation violation and then it asks me to end the program. The strange thing is if I compile the program in a file called test.cpp and compile it g++ test.cpp -larmadillo I do not have any problem. Could you please help me to solve this problem?
This may be way off base, but I thought I'd mention it anyway.
If you are on a UNIX/Linux machine, be aware that there is almost always a program called "test" that is used in the shell. Make sure when you run your program that you specify ./test instead of just test.
Actually if I compile as said in my previous post that command does work. The problem is when I execute the simulink program with my S-function in it. Here the first rows of the stack trace provided by matlab
The impression I have is that armadillo, instead of using the system lapack library, uses the one loaded on matlab (mkl_lapack_dgesvd). I'm trying to overcome the problem. One solution I'm thinking about is to build armadillo again with lapack a blas statically compiled in it. But I cannot find the proper way of doing it (armadillo is not well documented at all, in my opinion). Can anybody tell me how to do it? Another solution is appreciated anyway :)
Here I am with a work around. The problem is caused by the fact that armadillo invokes lapack and blas with 32bit integer pointerd. Matlab instead uses the mkl version of the above libraries and invokes them with a 64bit pointer. This causes the segmentation fault. To avoid this problem it is necessary to export to environment variable that force matlab to use the system libraries. This can be done prompting the two following commands in the same terminal from which matlab is launched
this is the case of my machine (running ubuntu 12.04). Use the locate command to find the libraries on your system. However, this solution reduces the efficiency of matlab, therefore is just a workaround for the people that are forced to write S-Functions using armadillo (just like me). Hence, do no include this exports in your .bashrc file!