Carnez Davis
Carnez Davis

Reputation: 863

Difficulty in Compiling Mex and Armadillo

I am having difficulty compiling an example mex and armadillo program and was wondering if anyone could assist me. I am using Mac OS and have had successes with the installation.

code:

#include "mex.h"
#include "math.h"
#include<armadillo>


using namespace arma;

void matlab2arma(mat& A, const mxArray *mxdata){
// delete [] A.mem; // don't do this!
access::rw(A.mem)=mxGetPr(mxdata);
access::rw(A.n_rows)=mxGetM(mxdata); // transposed!
access::rw(A.n_cols)=mxGetN(mxdata);
access::rw(A.n_elem)=A.n_rows*A.n_cols;
};

void freeVar(mat& A, const double *ptr){
access::rw(A.mem)=ptr;
access::rw(A.n_rows)=1; // transposed!
access::rw(A.n_cols)=1;
access::rw(A.n_elem)=1;
};

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 2)
mexErrMsgTxt("Incorrect number of input arguments");
if (nlhs != 1)
mexErrMsgTxt("Incorrect number of output arguments");

mat D1(1,1);
const double* D1mem=access::rw(D1.mem);
matlab2arma(D1,prhs[0]); // First create the matrix, then change it to point to the matlab data.

mat D2(1,1);
const double* D2mem=access::rw(D2.mem);
matlab2arma(D2,prhs[1]);

// check if the input corresponds to what you are expecting
if( D1.n_rows != D2.n_rows )
mexErrMsgTxt("Columns of D1 and D2 must be of equal length!");

if( D1.n_cols != D2.n_cols )
mexErrMsgTxt("Rows of D1 and D2 must be of equal length!");

plhs[0] = mxCreateDoubleMatrix(D1.n_rows, D1.n_cols, mxREAL);
mat output(1,1);
const double* outputmem=access::rw(output.mem);
matlab2arma(output,plhs[0]);

output=D1+D2;
// output.print();

freeVar(D1,D1mem); // Change back the pointers!!
freeVar(D2,D2mem);
freeVar(output,outputmem);
return;
}

Upvotes: 1

Views: 1568

Answers (2)

nripesh
nripesh

Reputation: 1

if you have armadillo set up correctly, just doing the following is enough (the code also worked successfully for me):

mex your_code.cpp -larmadillo

Upvotes: 0

Alex
Alex

Reputation: 501

It does work on my system (Ubuntu 12.04, 64-bit, MatlabR2013a, g++) using the following compilation command:

mex mexTest.cpp -llapack -larmadillo -lblas

where mexTest.cpp contains the code snippet you have provided. Note that for proper compilation of Armadillo under Matlab, it is required that your library paths are properly set. These can be updated in mexopts.sh or by redirecting the symbolic links in $matlabroot/sys/os/XXXXX, where XXXXX may vary according to your system (32/64-bit & OS).

Upvotes: 1

Related Questions