将Matlab与C ++链接时出错 [英] Error linking Matlab with C++
问题描述
#include< cstdlib>
#include< cstdio>
#include< string.h>
#includeengine.h
using namespace std;
int main(int argc,char ** argv){
Engine * mweng = engOpen();
engEvalString(mweng,n = func(5));
printf(%d,engGetVariable(mweng,n));
engClose(mweng);
返回0;
}
我使用g ++编译我的项目,包括 MATLABROOT \\ \\ extern \include
目录,并且出现以下错误:
build / Debug / Cygwin-Windows / main.o:在函数`main'中:
/cygdrive/d/Projects/Task1/main.cpp:10:对`_engOpen'的$ undefined引用
/ cygdrive / d / Projects / Task1 / main .cpp:11:对'_engEvalString'的未定义引用
/cygdrive/d/Projects/Task1/main.cpp:12:未定义对'_engGetVariable'的引用
/ cygdrive / d / Projects / Task1 / main .cpp:14:未定义引用'_engClose'
collect2:ld返回1退出状态$ b $ make [2]:*** [dist / Debug / Cygwin-Windows / task1.exe]错误1
make [1]:*** [.build-conf]错误2
make:*** [.build-impl]错误2
我想我在连接一些Matlab库时遇到了问题。
更新:
我也有VS2012和windows编译器的问题。
目录 extern\include
包含到其他包含目录
,与 extern相同\lib\win64\microsoft
添加到链接器其他库依赖项
。
根据@jucestain评论,在链接器选项中添加到其他依赖项
中的 libeng.lib
文件
更新2:
我们检测到兼容性问题:32位gcc不支持64位的Matlab引擎。因此,我将32位Matlab的 / extern
文件夹与我的 / extern
文件夹合并,将包含的链接程序库更改为 / extern / lib / win32 / lcc
,我的代码已经编译成功。感谢@aircooled!
这是我在cygwin提示符下使用以获取main.exe的行:
$ g ++ main.cpp -o main.exe -I / cygdrive / c / Program \ Files / MATLAB / R2009b / extern / include - L / cygdrive / c / Program \ Files / MATLAB / R2009b / extern / lib / win32 / microsoft -llibeng
请注意,为 gcc
提供源文件和库文件的顺序非常重要 - 首要来源,而非库文件。
I'm trying to use Matlab library in C++ program:
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include "engine.h"
using namespace std;
int main(int argc, char** argv) {
Engine* mweng = engOpen("");
engEvalString(mweng, "n = func(5)");
printf ("%d",engGetVariable(mweng, "n"));
engClose(mweng);
return 0;
}
I compile my project with g++ with included MATLABROOT\extern\include
directory and have a following error:
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/d/Projects/Task1/main.cpp:10: undefined reference to `_engOpen'
/cygdrive/d/Projects/Task1/main.cpp:11: undefined reference to `_engEvalString'
/cygdrive/d/Projects/Task1/main.cpp:12: undefined reference to `_engGetVariable'
/cygdrive/d/Projects/Task1/main.cpp:14: undefined reference to `_engClose'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/task1.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
I suppose I have problems with linking some Matlab libs.
UPDATE:
I have same problem with VS2012 and windows compiler.
Directory extern\include
included to Additional include directories
, same as extern\lib\win64\microsoft
added to linker Additional library dependencies
.
According to @jucestain comment, the libeng.lib
file added to Additional dependencies
in linker options.
UPDATE 2:
We detected a compatibility problem: 32-bit gcc doesn't work with 64-bit Matlab engine. So, I merged /extern
folder from 32-bit Matlab with my /extern
folder, changed included linker library to /extern/lib/win32/lcc
, and my code has compiled successfully. Thanks to @aircooled!
This is the line I use on my cygwin prompt to get main.exe:
$ g++ main.cpp -o main.exe -I/cygdrive/c/Program\ Files/MATLAB/R2009b/extern/include -L/cygdrive/c/Program\ Files/MATLAB/R2009b/extern/lib/win32/microsoft -llibeng
Note that order of giving sources and libs for gcc
is important - first sources, than libs.
这篇关于将Matlab与C ++链接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!