通过g++编译c++代码,在Ubuntu 16.04中使用MATLAB引擎

时间:2022-09-04 23:41:19

I would like to ask for analytic instructions on how to compile c++ code that uses the MATLAB Engine in Ubuntu 16.04 through g++.

我想询问关于如何编译c++代码的分析说明,这些代码使用的是Ubuntu 16.04到g++中的MATLAB引擎。

For the purposes of your answer assume that you use one of the sample codes provided by the default matlab installation for this purpose.

对于您的答案,假设您为此使用了默认的matlab安装提供的示例代码之一。

Please while answering consider that I am a beginner with linux, g++ and gcc compilation tools.

在回答的时候,请考虑我是一个linux, g++和gcc编译工具的初学者。

2 个解决方案

#1


4  

I assume that you want to know the procedure for compiling the c++ code (which calls MATLAB engine) using g++ from Linux Terminal. To do so, follow the steps below:

我假设您希望了解使用Linux终端的g++编译c++代码(它调用MATLAB引擎)的过程。为此,请执行以下步骤:

  1. Include following paths in PATH variable:

    在路径变量中包含以下路径:

    a) Location of MATLAB i.e. $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. For instance, if you are compiling engdemo.cpp, you need to navigate to $matlabroot/extern/examples/eng_mat/engdemo.cpp

    在命令提示符中,使用cd命令导航到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. Note that the order is important. So you need to use the command as below:

    您需要调用包含文件和库的编译器。对于这个你可以用-I和-L开关。注意顺序很重要。因此,您需要使用如下命令:

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

    g++ engdemo。cpp -I“$matlabroot/ outside n/include”-L“$matlabroot/bin/glnxa64”-leng -lmat -lmex -lut -o engdemo.o

  4. The above command will generate an object file engdemo.o. To execute this, use the command ./engdemo.o

    上面的命令将生成一个对象文件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.

    有关c++和MATLAB的更多帮助,请参阅http://www.umiacs.umd.edu/~jsp/Downloads/MatlabEngine/MatlabEngine.pdf。

#2


1  

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.

>与matlab引擎兼容的编译器。

2> Reference to API header files('.h' for c or '.hpp' for c++) for compilation.

引用API头文件('。h代表c或。用于编译的hpp。

3> Reference to the libraries('.lib' for windows,'.so' for linux) for external symbol link.

>对库的引用('。*“windows”。因此,对于linux)外部符号链接。

You can see comptatible linux based system compiler here. The GCC C/C++ 4.9.x is compatible so you can use g++.

您可以在这里看到基于编译器的linux系统编译器。GCC 4.9 C / c++。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,所以文件应该在$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. 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

之后,一个名为engDemo的文件。o应该创建。由于使用的matlab解释器应该位于$matlabroot/bin中,所以可能需要-leng -lmat -lmex -lut选项

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 !!

希望它帮助! !

#1


4  

I assume that you want to know the procedure for compiling the c++ code (which calls MATLAB engine) using g++ from Linux Terminal. To do so, follow the steps below:

我假设您希望了解使用Linux终端的g++编译c++代码(它调用MATLAB引擎)的过程。为此,请执行以下步骤:

  1. Include following paths in PATH variable:

    在路径变量中包含以下路径:

    a) Location of MATLAB i.e. $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. For instance, if you are compiling engdemo.cpp, you need to navigate to $matlabroot/extern/examples/eng_mat/engdemo.cpp

    在命令提示符中,使用cd命令导航到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. Note that the order is important. So you need to use the command as below:

    您需要调用包含文件和库的编译器。对于这个你可以用-I和-L开关。注意顺序很重要。因此,您需要使用如下命令:

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

    g++ engdemo。cpp -I“$matlabroot/ outside n/include”-L“$matlabroot/bin/glnxa64”-leng -lmat -lmex -lut -o engdemo.o

  4. The above command will generate an object file engdemo.o. To execute this, use the command ./engdemo.o

    上面的命令将生成一个对象文件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.

    有关c++和MATLAB的更多帮助,请参阅http://www.umiacs.umd.edu/~jsp/Downloads/MatlabEngine/MatlabEngine.pdf。

#2


1  

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.

>与matlab引擎兼容的编译器。

2> Reference to API header files('.h' for c or '.hpp' for c++) for compilation.

引用API头文件('。h代表c或。用于编译的hpp。

3> Reference to the libraries('.lib' for windows,'.so' for linux) for external symbol link.

>对库的引用('。*“windows”。因此,对于linux)外部符号链接。

You can see comptatible linux based system compiler here. The GCC C/C++ 4.9.x is compatible so you can use g++.

您可以在这里看到基于编译器的linux系统编译器。GCC 4.9 C / c++。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,所以文件应该在$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. 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

之后,一个名为engDemo的文件。o应该创建。由于使用的matlab解释器应该位于$matlabroot/bin中,所以可能需要-leng -lmat -lmex -lut选项

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 !!

希望它帮助! !