尝试使用 header从 C++ 访问 MATLAB
trying to access matlab from c++ using header
>我正在尝试编译一个包含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
您指定的第一个 g++ 命令用于编译,您只需要-I
选项即可。给它engine.h
所在的文件夹的路径(-I$MATLABROOT/extern/include
- 假设MATLABROOT
指向 Matlab 安装的根目录,在本例中为 /usr/local/matlab
)。
第二个 g++ 命令用于链接,您需要将 -L 和 -l(s) 放在那里。类似的东西 -L$MATLABROOT/bin/glnxa64 -leng -lmx
所以我们最终得到这个序列:
g++ -c MLP.cpp -I$MATLABROOT/extern/include
g++ MLP.o -o main -L$MATLABROOT/bin/glnxa64 -leng -lmx
为了获得相同的内容,但在一行中:
g++ MLP.c -o main -I$MATLABROOT/extern/include -L$MATLABROOT/bin/glnxa64 -leng -lmx
注意:当您要运行此可执行文件时,libeng.so
和libmx.so
必须可访问,因此在尝试运行main
之前,请使用文件夹:$MATLABROOT/bin/glnxa64
扩展LD_LIBRARY_PATH
或PATH
。
在 64 位 Linux 上,您可能希望将 lib 路径更改为:
${MATLABROOT}/extern/lib/glnxa64
引擎程序的最简单方法是使用 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 共享库。
有关详细信息,请参阅这些页面。
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- 访问者访问变体并返回不同类型时出错
- 尝试通过多个向量访问变量时,向量下标超出范围
- 无法访问嵌套类.类的使用无效
- 写入位置0x0000000C时发生访问冲突
- 我们可以访问一个不存在的联盟的成员吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 我的简单if-else语句是如何无法访问的代码
- 从C++dll访问C#中的一行主要参数
- 概念TS检查忽略私有访问修饰符
- 访问被拒绝后,c++中的故障保护代码
- 在c++中访问int到类对象的映射时出错
- 我想访问std::unique_ptr中的一个特定元素
- 为什么示例代码访问IUnknown中已删除的内存
- C++:无法访问声明的受保护成员
- 通过指向指针数组的指针访问子类的属性
- 如何使用单独文件中的派生类访问友元函数对象