1
votes

I have to use mat.h for open a .mat file in my C++ code. My code is that:

#include "mat.h"
using namespace std;
int main() {
MATFile *pmat;
pmat = matOpen("ns3Da.mat","r");
return 0;
}

The command I use to compile is that:

g++ program.cpp -I/usr/local/MATLAB/R2012a/extern/include -L/usr/local/MATLAB/R2012a/bin/* -L/usr/local/MATLAB/R2012a/extern/lib -o program

The error I obtain is that:

/tmp/ccSWqTnb.o: In function 'main': programma_c.cpp:(.text+0x13): undefined reference to 'matOpen' collect2: error: ld returned 1 exit status

I use Linux Ubuntu 16.04 LTS and Matlab 2012a version.

How can I resolve this error?

1
You are not linking a required library. You only specify the library search paths.nakiya
^ --- i.e. add -lmat to the build commandStoryTeller - Unslander Monica
OK, who thought that "mat.h" was a good name for a header? It looks like if someone had had misspelled "math.h"Cubic

1 Answers

1
votes

The -L flag in compilation specifies the path to search for libraries. -l flag should be used to specify the name of library without lib, for instance, in your case -lmat. So your compilation command should be something like

g++ program.cpp -I/usr/local/MATLAB/R2012a/extern/include -L/usr/local/MATLAB/R2012a/bin/glnxa64 -L/usr/local/MATLAB/R2012a/extern/lib -lmat -o program