linuxmatlab編譯
1. MATLAB 2017 安裝步驟(linux)
MATLAB 是一款用於演算法開發、數據可視化、數據分析以及數值計算的高級技術計算語言和互動式環境的商業數學軟體,由美國 MathWorks 公司出品。
安裝 MATLAB 2017 在 CentOS7 環境的步驟如下:
1、首先,將 ISO 文件掛載到 /mnt 目錄。使用命令:
#mount R2017b_glnxa64.iso /mnt -o loop
2、接著,運行以下命令啟動安裝交互界面:
#./install -javadir ./sys/java/jre/glnxa64/jre/
安裝過程中,會彈出圖形界面,請根據提示進行操作。
3、選擇「Use a File installation Key」,然後輸入購買的軟體序列號,例如「12345-12345-12345-12345-12345」,並點擊下一步。
4、在第六步中,設置安裝路徑。默認路徑即可,或手動輸入。
5、可以依據自身需求選擇是否安裝額外模塊。如果不需要使用多核計算,可以不選擇「MATLAB Distributed Computing Server 6.7」。
6、點擊「Install」開始安裝。整個安裝過程根據機器配置的不同,可能需要幾十分鍾到幾個小時。
7、安裝完成後,不要立即啟動 MATLAB,等待激活過程完成。
8、激活後,在證書所在目錄下復制證書到目標目錄。
9、設置環境變數。為了方便調用 MATLAB 及相關命令,將 MATLAB 安裝目錄的 bin 文件夾添加到 PATH 環境變數中。在用戶目錄下輸入 Vi .bashrc,並添加以下命令:
export PATH=/usr/local/MATLAB/R2017b/bin:$PATH
10、刷新環境變數,運行 source ~/.bashrc。
11、最後,運行 matlab 命令,即可啟動 MATLAB 的圖形界面,開始正常使用這款軟體。
2. Matlab安裝教程(Linux)
在虛擬機環境中,通常會將安裝包放置在/tmp/VMwareDnD目錄下,需將文件移動至/home/。首先,使用以下命令進行文件轉移:
然後,在包含安裝包的目錄中,執行解壓縮操作。如果虛擬機中未預裝解壓命令,可通過以下命令安裝:
在安裝過程中,建議先閱讀Readme文件,了解安裝步驟。為了掛載iso文件進行安裝,需要在相應位置創建掛載目錄:
將iso文件掛載到掛載目錄後,轉到該目錄並啟動安裝程序。若遇到安裝選項,選擇Advanced Options並選擇File installation Key方式安裝,鍵入在Readme中獲取的Key(例如:05322-36228-06991-12654-51812-34369-14072-44298-22786-36732-05503-35033-50900-29808-05166-12170-05630-02560-02687-62114-45079-42917-06281-13007-19512-18270)。
接著,需要提供license file,通常在安裝包內提供,輸入對應目錄。建議將license.lic保存在安裝目錄/opt/matlab/license/,便於後續使用。
為了安裝在/opt/matlab目錄下,需要先創建該目錄。將license文件放置在相應位置,安裝程序中輸入路徑為/opt/matlab/license/license.lic。
在安裝步驟中,填寫安裝地址,根據需求選擇安裝內容。安裝完成後,將補丁文件存放在指定位置,並卸載掛載的iso文件。
為了方便快捷啟動Matlab,推薦將命令添加到PATH。首先,在/home/創建bin目錄,然後創建啟動命令腳本,確保有執行許可權,接著在環境配置文件bashrc中添加環境變數設置,如需獨立管理,可以在/home/下創建獨立的環境文件並引入至bashrc中。
最後,在bashrc中輸入配置命令,完成環境配置。現在,只需在命令行輸入matlab,即可直接啟動Matlab。
3. linux c++ 怎麼 調用自己函數的
實驗平台:ubuntu 12.04 + g++4.6 + matlab2012a
問題描述:
有一個c++程序main.cpp,和一個matlab函數myFunc.m。現在要做這件事:
1)從main.cpp中傳遞2個double類型的數值a和b到myFunc.m中
2)myFunc.m中求和(sum = a+b)
3)main.cpp中接收myFunc.m返回的和並輸出。
思路:
1)設置matlab的編譯器,使用gcc編譯器。編譯m文件成.so。
2)編譯.cpp文件,編譯時調用.so庫(添加.so的路徑到編譯選項)。
步驟:
1)將自己的matlab函數(myFunc.m)編譯成動態鏈接庫
(1) 設定編譯器為gcc,在matlab 命令行依次執行命令mex -setup和mbuild -setup:
>> mex -setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the 'mex -setup' command selects an options file that is placed in ~/.matlab/R2012a and used by default for 'mex'. An options file in the current working directory or specified on the command line overrides the default options file in ~/.matlab/R2012a. To override the default options file, use the 'mex -f' command (see 'mex -help' for more information).The options files available for mex are: 1: /opt/MATLAB/R2012a/bin/mexopts.sh : Template Options file for building gcc MEX-files 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/mexopts.sh is being copied to ~/.matlab/R2012a/mexopts.shcp: cannot create regular file `~/.matlab/R2012a/mexopts.sh': Permission denied************************************************************************** Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html Building with the -largeArrayDims option enables the new API. **************************************************************************>> mbuild -setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the 'mbuild -setup' command selects an options file that is placed in ~/.matlab/R2012a and used by default for 'mbuild'. An options file in the current working directory or specified on the command line overrides the default options file in ~/.matlab/R2012a. To override the default options file, use the 'mbuild -f' command (see 'mbuild -help' for more information).The options files available for mbuild are: 1: /opt/MATLAB/R2012a/bin/mbuildopts.sh : Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/mbuildopts.sh is being copied to ~/.matlab/R2012a/mbuildopts.shcp: cannot create regular file `~/.matlab/R2012a/mbuildopts.sh': Permission denied>>
(2) 在matlab中,編寫myFunc.m文件內容如下:
function [ C ] = myFunc(A, B)C = A+B;end
(3) 生成myFunc.m的動態鏈接庫(.so)
>> mcc -W cpplib:libmyFunc -T link:lib myFunc.m -cWarning: MATLAB Toolbox Path Cache is out of date and is not being used.Type 'help toolbox_path_cache' for more info >>
等待數秒,完成。可以看到myFunc.m所在的目錄下生成了多個文件:
$ lslibmyFunc.cpp libmyFunc.ctf libmyFunc.exports libmyFunc.h libmyFunc.so main.cpp mccExcludedFiles.log myFunc.m readme.txt
命
令解釋:其中-W是控制編譯之後的封裝格式;cpplib是指編譯成C++的lib;cpplib冒號後面是指編譯的庫的名字;-T表示目
標,link:lib表示要連接到一個庫文件的目標,目標的名字即是.m函數的名字。-c表明需要生成.ctf文件,比如本例如果不加-c就不會生成
「libmyFunc.ctf」。
生成的文件中打開「libmyFunc.h」可以看到一行:
extern LIB_libmyFunc_CPP_API void MW_CALL_CONV myFunc(int nargout, mwArray& C, const mwArray& A, const mwArray& B);
這
個就是我們的myFunc.c函數待會兒在c++中調用時的介面。有4個參數,第一個是參數個數,第二個是用來接收函數返回值的,後面2個是從c++中傳
遞進來的變數。我們還會用到「libmyFunc.h」中的另外2個函數:libmyFuncInitialize()初始化,和注銷
libmyFuncTerminate()。
2)編寫main.cpp,代碼如下:
#include <iostream>#include "mclmcr.h"#include "matrix.h"#include "mclcppclass.h"#include "libmyFunc.h"#include "mclmcrrt.h"using namespace std;int main() { // initialize lib,這里必須做初始化! if( !libmyFuncInitialize()) { std::cout << "Could not initialize libmyFunc!" << std::endl; return -1; } // 用戶輸入2個數值 double a, b; cout<<"Please input 2 numbers <a b> and then press enter: "<<endl; cin >> a; cin >> b; double c; //used to receive the result // 為變數分配內存空間, maltab只有一種變數,就是矩陣,為了和c++變數接軌,設置成1*1的矩陣 mwArray mwA(1, 1, mxDOUBLE_CLASS); //1,1表示矩陣的大小, mxDOUBLE_CLASS表示變數的精度 mwArray mwB(1, 1, mxDOUBLE_CLASS); mwArray mwC(1, 1, mxDOUBLE_CLASS); // 調用類裡面的SetData函數給類賦值 mwA.SetData(&a, 1); mwB.SetData(&b, 1); // 調用自己的函數,求和。 myFunc(1, mwC, mwA, mwB); c = mwC.Get(1, 1); cout<<"The sum is: "<<c<<endl; // 後面是一些終止調用的程序 // terminate the lib libmyFuncTerminate(); // terminate MCR mclTerminateApplication(); return EXIT_SUCCESS;}
3)編譯main.cpp函數,調用libmyFunc.so
$ g++ -o main -I. -I/opt/MATLAB/R2012a/extern/include -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lm -lmwmclmcrrt -lmwmclmcr
開始編譯時也遇到一些問題,主要是鏈接庫路徑問題導致的編譯錯誤,詳細錯誤匯總在另篇筆記里(點此查看)。
編譯成功後,需要裝一下MCR Installer,步驟點此。
運行結果:
$ ./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 10 100The sum is: 110$ ./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 1 1The sum is: 2
源代碼及編譯過程中的所有文件已打包,點擊這里可以下載。