【问题标题】:compile c++ program eclipse that uses matlab engine编译使用matlab引擎的c++程序eclipse
【发布时间】:2012-07-18 13:54:28
【问题描述】:

我正在尝试配置 eclipse 来编译和运行使用 matlab 引擎的示例 engdemo.cpp

我按照here写的说明操作,但还是有错误:

make all 
Building target: matlabEngine
Invoking: GCC C++ Linker
g++ -L/usr/local/MATLAB/R2011a/bin/glnx86 -Xlinker -rpath-link -Xlinker /usr/local/MATLAB/R2011a/bin/glnx86 -o"matlabEngine"  ./engdemo.o   -leng -lm -lmat -lmex -lut
/usr/bin/ld: ./engdemo.o: undefined reference to symbol 'mxDestroyArray'
/usr/bin/ld: note: 'mxDestroyArray' is defined in DSO /usr/local/MATLAB/R2011a/bin/glnx86/libmx.so so try adding it to the linker command line
/usr/local/MATLAB/R2011a/bin/glnx86/libmx.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make: *** [matlabEngine] Errore 1

当我从 shell 编译程序时,我使用这些命令,并且没有错误,所以我可以运行它。

g++ -c  -I/usr/local/MATLAB/R2011a/extern/include -I/usr/local/MATLAB/R2011a/simulink/include -DMATLAB_MEX_FILE -ansi -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/local/MATLAB/R2011a/extern/include/cpp -I/usr/local/MATLAB/R2011a/extern/include -DGLNX86 -DGCC  -DMX_COMPAT_32 -O -DNDEBUG  "engdemo.cpp"
g++ -O  -o  "engdemo"  engdemo.o  -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnx86 -L/usr/local/MATLAB/R2011a/bin/glnx86 -leng -lmx -lm

但是我需要在eclipse中编译。 有什么帮助吗?

【问题讨论】:

  • eclipse 命令行中似乎缺少 libmx,您是否添加了该库?
  • @Rolle,你认为链接器如何知道 .so 的名称和库的路径?

标签: c++ eclipse matlab linker matlab-engine


【解决方案1】:

通常链接器错误很难调试。在这种情况下,错误信息非常丰富。

undefined reference to symbol 'mxDestroyArray' 表示未链接包含名为 mxDestroyArray 的符号的库。但是,它确实告诉您该库名为 libmx.so 并在 /usr/local/MATLAB/R2011a/bin/glnx86 中找到,以便您知道在哪里看。

在您的 g++ -O ... 命令中,您包含了此库 (... -L/usr/local/MATLAB/R2011a/bin/glnx86 ...) 的路径,这解释了它的工作原理。

您还需要使用-l 标志在链接器命令中引用库本身,这在g++ -O ... 命令中的-lmx 中可见。

您需要在 Eclipse 中做同样的事情。

在 Eclipse 中,您可以通过选择Project 菜单并转到Properties 来找到项目路径和符号。展开 C/C++ General 并选择 Paths and Symbols。此对话框允许您告诉 Eclipse 链接器所需的库位于何处,类似于 g++ 中的 -L 参数。

为此,请将库名称和路径添加到 Library PathsLibraries 选项卡下的列表中。

注意:添加路径还不够,还需要指定库名(在g++中是-l参数,本例为-lmx

Libraries 选项卡上,添加一个名为mx 的新库。这应该将-lmx 添加到您的 Eclipse 链接器命令中。

【讨论】:

  • 是的,我已经这样做了。事实上,在 Eclipse 控制台中,它使用的命令显示它的路径是正确的: g++ -L/usr/local/MATLAB/R2011a/bin/glnx86 -Xlinker -rpath-link -Xlinker /usr/local/MATLAB/R2011a/bin /glnx86 -o"matlabEngine" ./engdemo.o -leng -lm -lmat -lmex -lut
  • 是的,您已经包含了库路径,但没有包含在 eclipse 构建命令中找不到的实际库(由 -lmx 标识)。我在答案中添加了一条注释,以更详细地解释这一点。
  • 谢谢!现在代码编译并链接。但是当我尝试运行时,它显示 matlab: Command not found。 matlab 命令在我的搜索路径中,事实上我可以从我的 shell 中调用它。我在 Eclipse 控制台中找不到此命令。我该如何设置它?感谢支持
猜你喜欢
  • 2017-08-13
  • 2023-03-29
  • 2010-12-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多