简体   繁体   English

使用MATLAB Engine和g ++的C ++源代码编译

[英]C++ source compilation using MATLAB Engine and g++

如果您可以提供有关如何使用MATLAB Engine和g ++在Ubuntu环境中编译c ++源代码文件的一些指导,将对您有所帮助。

I assume that you want to know the procedure for compiling the c++ code (which calls MATLAB engine) using g++ from Linux Terminal. 我假设您想了解使用Linux Terminal中的g ++编译c ++代码(称为MATLAB引擎)的过程。 To do so, follow the steps below: 为此,请按照以下步骤操作:

  1. Include following paths in PATH variable: 在PATH变量中包括以下路径:

    a) Location of MATLAB ie $matlabroot/bin b) $matlabroot/sys/os a)MATLAB的位置,即$ matlabroot / bin b)$ matlabroot / sys / os

    You can do this by using the command 'setenv PATH $matlabroot/bin:$matlabroot/sys/os:$PATH ' . 您可以使用命令'setenv PATH $ matlabroot / bin:$ matlabroot / sys / os:$ PATH'来执行此操作。

  2. In the command prompt, navigate to the directory where the cpp code is located using cd command. 在命令提示符下,使用cd命令导航到cpp代码所在的目录。 For instance, if you are compiling engdemo.cpp, you need to navigate to $matlabroot/extern/examples/eng_mat/engdemo.cpp 例如,如果要编译engdemo.cpp,则需要导航至$ matlabroot / extern / examples / eng_mat / engdemo.cpp

  3. You need to call the compiler with required include files and libraries. 您需要使用所需的包含文件和库来调用编译器。 For this you can use -I and -L switches for that. 为此,您可以使用-I和-L开关。 Note that the order is important. 请注意,顺序很重要。 So you need to use the command as below: 因此,您需要使用以下命令:

    g++ engdemo.cpp -I "$matlabroot/extern/include" -L "$matlabroot/bin/glnxa64" -leng -lmat -lmex -lut -o engdemo.o g ++ engdemo.cpp -I“ $ matlabroot / extern / include” -L“ $ matlabroot / bin / glnxa64” -len -lmat -lmex -lut -o engdemo.o

  4. The above command will generate an object file engdemo.o. 上面的命令将生成一个目标文件engdemo.o。 To execute this, use the command ./engdemo.o 要执行此操作,请使用命令./engdemo.o

    You can refer to the document at http://www.umiacs.umd.edu/~jsp/Downloads/MatlabEngine/MatlabEngine.pdf for more help regarding C++ and MATLAB. 您可以参考http://www.umiacs.umd.edu/~jsp/Downloads/MatlabEngine/MatlabEngine.pdf上的文档,以获取有关C ++和MATLAB的更多帮助。

The compilation process in C/C++ is divided in two phases: C / C ++中的编译过程分为两个阶段:

First, the compilation where source code is transformed into machines code with multiples object files (.o or .obj). 首先,将源代码转换为带有多个目标文件(.o或.obj)的机器代码的编译。

Then, the link to transform object files into a single executable file (.dll or .exe). 然后,将对象文件转换为单个可执行文件(.dll或.exe)的链接。

C/C++ programs that run matlab engine need three things: 运行matlab引擎的C / C ++程序需要三件事:

1> A compiler that is compatible with matlab engine. 1>与matlab引擎兼容的编译器。

2> Reference to API header files('.h' for c or '.hpp' for c++) for compilation. 2>引用API头文件(对于c是'.h'或对于c ++是'.hpp')进行编译。

3> Reference to the libraries('.lib' for windows,'.so' for linux) for external symbol link. 3>对于外部符号链接,请参考库(对于Windows为'.lib',对于Linux为'.so')。

You can see comptatible linux based system compiler here . 您可以在此处看到基于linux的兼容系统编译器。 The GCC C/C++ 4.9.x is compatible so you can use g++. GCC C / C ++ 4.9.x是兼容的,因此您可以使用g ++。

As this pdf suggested, the API header files should be there $matlabroot/extern/include and the .so files should be in $matlabroot/ bin/glnax64 where $matlabroot is your matlab install folder 如该pdf所示 ,API头文件应位于$ matlabroot / extern / include中 ,.so文件应位于$ matlabroot / bin / glnax64中 ,其中$ matlabroot是您的matlab安装文件夹

Set up Environment variables 设置环境变量

Open your temnial with ctrl + alt + T and type : ctrl + alt + T打开您的temnial并输入:

setenv PATH $matlabroot/bin:$matlabroot/sys/os:$PATH

You can then go to the folder where source file is located, let's say $matlabroot/extern/examples/eng_mat/ with the following command : 然后,您可以使用以下命令转到源文件所在的文件夹,例如$ matlabroot / extern / examples / eng_mat /

cd $matlabroot/extern/examples/eng_mat/

You need to do the compilation with : 您需要使用以下命令进行编译:

g++ -c engDemo.cpp -I '$matlabroot/extern/include' -leng -lmat -lmex -lut

After that, a file named engDemo.o should be created. 之后,应创建一个名为engDemo.o的文件。 The -leng -lmat -lmex -lut options are probably needed among other things because of the usage of the matlab interpreter that should be located in $matlabroot/bin 除其他事项外,可能还需要-leng -lmat -lmex -lut选项,因为使用了应位于$ matlabroot / bin中的matlab解释器

And the external symbol link with : 并且外部符号链接与:

g++ -o engDemo -L '$matlabroot/bin/glnax64'

Be careful as this path sugested that you are on a x64 architecture machine, if you are not,the path might be slightly different. 小心,因为此路径表明您使用的是x64体系结构计算机,否则,该路径可能会略有不同。

Then you can execute your file just by doing ./engDemo 然后,您只需执行./engDemo即可执行文件

I can't install the matlab engine on the laptot I am using so I'm unable to test the instruction I gave you but It should be done this way. 我无法在正在使用的laptot上安装matlab引擎,因此无法测试我给您的指令,但应以这种方式完成。

Hope it helps !! 希望能帮助到你 !!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM