This forum has been archived. All content is frozen. Please use KDE Discuss instead.

How to mex .cpp (include external Eigen header) in Matlab?

Tags: None
(comma "," separated)
SL_TYM
Registered Member
Posts
2
Karma
0
OS
Hi, I just started to use Eigen lib to do matrix manipulation instead of Matlab.
I start from the example code, it can be compiled in VS 2010
Code: Select all
#include <iostream>
#include <Eigen/Eigen>
using Eigen::MatrixXd;
void main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << m << std::endl;
}


I want to call this function in matlab, therefore modify it:
Code: Select all
#include <mex.h>
#include <Eigen/Eigen>
using Eigen::MatrixXd;
void mexFunction(int nlhs, mxArray*plhs[], int nrhs, mxArray*prhs[])
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
}


But it seems like Matlab cannot use the Eigen header files, and gives the following error info
Cannot open include file: 'Eigen/Eigen': No such file or directory


Can anyone give me some clue about how to solve this problem? Or where can i find a tutorial about this?

Many thanks!!
mattd
Registered Member
Posts
28
Karma
0
Remember: in standard C++ it's "int main", not "void main". // http://www.parashift.com/c++-faq-lite/n ... l#faq-29.3

As for mex, you have to use the -I (that's "I" like "Include") option, just like you had to tell your compiler how to find the directory including Eigen header files:
http://www.mathworks.se/help/techdoc/ref/mex.html
SL_TYM
Registered Member
Posts
2
Karma
0
OS
mattd wrote:Remember: in standard C++ it's "int main", not "void main". // http://www.parashift.com/c++-faq-lite/n ... l#faq-29.3

As for mex, you have to use the -I (that's "I" like "Include") option, just like you had to tell your compiler how to find the directory including Eigen header files:
http://www.mathworks.se/help/techdoc/ref/mex.html



Thanks a lot.
Your suggestion works!
I also tried armdillo lib, it works there too!
hpatel
Registered Member
Posts
1
Karma
0
Hey i also come across same problem to include eigen library in mex file.

i tried with following code but i am not successful in that.

I used below command ... and path is given as shown below:
Code: Select all
mex -I"C:\Program Files (x86)\Eigen\include" mexample.c


i am using following in mex file.
Code: Select all
#include <Eigen/Dense>


Can you please help me as i got following error. :(

Error using mex (line 206)
Unable to complete successfully.

Error in ex (line 1)
mex -I "C:\Program Files (x86)\Eigen\include" mexample.c
chenjiang
Registered Member
Posts
2
Karma
0
For the last post, your path of Eigen headers is wrong.

if your Eigen path is "x:\Eigen" which not only contains the heards but also contans docs and cmake,
your mex build command should be
Code: Select all
mex -I"x:\Eigen" xxxx.cpp
.

It works for me. Enjoy.
bergman
Registered Member
Posts
1
Karma
0
I am having a similar issue. I've used Eigen with mex before but it's been years.
I have the newest Octave install in a Windows (8) computer.
Eigen is in x\\eigen-eigen-6b38706d90a9\eigen-eigen-6b38706d90a9
That's where it placed itself when downloaded.
If I use the command mex -I"x\\eigen-eigen-6b38706d90a9\eigen-eigen-6b38706d90a9" mexfile.cpp
it does not recognize <Eigen/Core>.

Oddly I've used the same code before with Octave and the path provided above does not cause problems when included as an include path in a Visual Studio project.

Thanks for your help.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient