Link mat.h in a C++ file

依然范特西╮ 提交于 2021-02-05 09:20:50

问题


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:


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


来源:https://stackoverflow.com/questions/44564442/link-mat-h-in-a-c-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!