1

我正在尝试编译一个包含 Matlab 提供的引擎头文件的 c++ 程序。文件 MLP.cpp 包含:

#include <engine.h>
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;

并引用下面错误中突出显示的 matlab 函数。运行时:

g++ -c MLP.cpp -I/usr/local/matlab/extern/include -L/usr/local/matlab/extern/lib -llibeng -llibmx -lmatlab
g++ MLP.o -o main

我收到以下错误:

MLP.o: In function `MatLabPredictor::MatLabPredictor(char*)': 

MLP.cpp:(.text+0x18): undefined reference to `engOpen'

MLP.cpp:(.text+0x36): undefined reference to `engEvalString'

MLP.cpp:(.text+0x4a): undefined reference to `engEvalString'

MLP.cpp:(.text+0x5e): undefined reference to `mxCreateDoubleMatrix'

MLP.cpp:(.text+0x79): undefined reference to `mxGetPr'

MLP.o: In function `MatLabPredictor::~MatLabPredictor()':

MLP.cpp:(.text+0xa1): undefined reference to `engClose'

MLP.o: In function `MatLabPredictor::retrain(double)':

MLP.cpp:(.text+0x104): undefined reference to `engPutVariable'

MLP.cpp:(.text+0x118): undefined reference to `engEvalString'

MLP.cpp:(.text+0x12c): undefined reference to `engEvalString'

MLP.cpp:(.text+0x140): undefined reference to `engEvalString'

MLP.o: In function `MatLabPredictor::predict_next_value()':

MLP.cpp:(.text+0x162): undefined reference to `engEvalString'

MLP.cpp:(.text+0x176): undefined reference to `engGetVariable'

MLP.cpp:(.text+0x186): undefined reference to `mxGetData'

MLP.cpp:(.text+0x199): undefined reference to `mxDestroyArray'
collect2: ld returned 1 exit status

我还尝试将编译命令更改为:

g++ -c MLP.cpp -I/usr/local/matlab/extern/include -L/usr/local/matlab/bin/glnxa64 -llibeng -llibmx -lmatlab
g++ MLP.o -o main
4

3 回答 3

1

First g++ command you specify is for compiling, you need only -I option for that. Give it path to folder where engine.h is (-I$MATLABROOT/extern/include - Let's say MATLABROOT points to root directory of Matlab installation, in this case /usr/local/matlab).

Second g++ command is for linking, you need to put -L and -l(s) there. Something like -L$MATLABROOT/bin/glnxa64 -leng -lmx

So we end up with this sequence:

g++ -c MLP.cpp -I$MATLABROOT/extern/include

g++ MLP.o -o main -L$MATLABROOT/bin/glnxa64 -leng -lmx

For getting the same, but in a single line:

g++ MLP.c -o main -I$MATLABROOT/extern/include -L$MATLABROOT/bin/glnxa64 -leng -lmx

Note: libeng.so and libmx.so must be reachable when you want to run this executable, so extend LD_LIBRARY_PATH or PATH with folder: $MATLABROOT/bin/glnxa64 before attempting to run main.

于 2013-11-15T11:00:36.613 回答
0

在 64 位 Linux 上,您可能希望将 lib 路径更改为:

${MATLABROOT}/extern/lib/glnxa64

于 2013-11-13T23:33:10.540 回答
0

编译引擎程序的最简单方法是使用该mex命令以及提供的选项文件engopts.sh

>> engopts = fullfile(matlabroot,'bin','engopts.sh');
>> mex('-f',engopts, 'MLP.cpp')

如果您愿意,您可以使用详细标志运行上述代码mex -v ...,并将生成的编译和链接标志复制到您自己的构建系统中。

(我认为问题在于您应该lib从库名称中删除该部分g++ file.cpp -I${MROOT}/extern/include -L${MROOT}/extern/lib/${ARCH} -leng -lmx:)

注意:不要忘记设置,LD_LIBRARY_PATH以便您的程序在运行时能够找到所需的 MATLAB 共享库。

有关更多信息,请参阅这些 页面

于 2013-11-14T17:13:09.210 回答