1

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?

nakiya
  • 14,063
  • 21
  • 79
  • 118
Sara Savio
  • 25
  • 1
  • 8

1 Answers1

1

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
dlmeetei
  • 9,905
  • 3
  • 31
  • 38