C++ MATLAB データ API 共有ライブラリの生成と C++ アプリケーションのビルド
サポートされるプラットフォーム: Windows®、Linux®、Mac
この例では、MATLAB® 関数から C++ 共有ライブラリを作成する方法を説明します。生成されたライブラリは、C++ アプリケーションに統合できます。また、この例では、C++ アプリケーションから C++ 共有ライブラリを呼び出す方法も示します。このアプリケーションを実行するために、MATLAB のライセンス コピーがターゲット システムにインストールされている必要はありません。
MATLAB での関数の作成
MATLAB で、パッケージ化対象の MATLAB コードを調べます。
MATLAB に付属している
matrix
フォルダーを作業フォルダーにコピーします。copyfile(fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','matrix'),'matrix')
作業フォルダー内の新しい
matrix
サブフォルダーに移動します。関数
addmatrix.m
、multiplymatrix.m
、およびeigmatrix.m
を確認してテストします。
compiler.build.cppSharedLibrary
を使用した C++ 共有ライブラリの作成
プログラムによる方法を使用して C++ 共有ライブラリをビルドします。あるいは、グラフィカル インターフェイスを使用して C++ 共有ライブラリを作成する場合は、Package MATLAB Function Using C++ Shared Library Compiler App with MATLAB Data APIを参照してください。
関数ファイルのリストを cell 配列に保存します。
functionfiles = {'addmatrix.m', 'multiplymatrix.m', 'eigmatrix.m'}
関数を呼び出す MATLAB サンプル コードを作成します。サンプル ファイルを使用して、ターゲット言語のサンプル アプリケーションを生成します。詳細と制限については、Create Sample Code to Call Exported Functionを参照してください。
以下のコードを
libmatrixSample.m
という名前のサンプル ファイルに保存します。% Sample script to demonstrate execution of functions % addmatrix, eigmatrix, and multiplymatrix a1 = [1 4 7; 2 5 8; 3 6 9]; % Initialize a1 here a2 = a1; % Initialize a2 here a = addmatrix(a1, a2); e = eigmatrix(a1); m = multiplymatrix(a1, a2);
パッケージ化手順で、サンプル ドライバー ファイルをまったく含めないことも選択できます。独自の C++ アプリケーション コードを作成する場合、MATLAB 関数のパッケージ化が完了してから
mbuild
を使用して適切なディレクトリに移動し、コンパイルできます。関数
compiler.build.cppSharedLibrary
を使用して C++ 共有ライブラリをビルドします。名前と値の引数を使用してライブラリ名を指定し、サンプル ファイルを追加します。buildResults = compiler.build.cppSharedLibrary(functionfiles,... 'LibraryName','libmatrix',... 'SampleGenerationFiles','libmatrixSample.m');
compiler.build
コマンドで名前と値の引数を使用して、追加オプションを指定できます。詳細については、compiler.build.cppSharedLibrary
を参照してください。compiler.build.Results
オブジェクトbuildResults
には、ビルド タイプ、生成ファイル、含まれるサポート パッケージ、およびビルド オプションに関する情報が含まれています。この構文により、現在の作業ディレクトリの
libmatrixcppSharedLibrary
という名前のフォルダー内に以下のファイルが生成されます。samples\libmatrixSample1_mda.cpp
— 関数addmatrix
を呼び出す C++ サンプル アプリケーション。samples\libmatrixSample2_mda.cpp
— 関数eigmatrix
を呼び出す C++ サンプル アプリケーション。samples\libmatrixSample3_mda.cpp
— 関数multiplymatrix
を呼び出す C++ サンプル アプリケーション。v2\generic_interface\libmatrix.ctf
— デプロイ可能なアーカイブが含まれるコンポーネント テクノロジー ファイル。v2\generic_interface\readme.txt
— パッケージ化情報が含まれるテキスト ファイル。GettingStarted.html
— 共有ライブラリの統合に関する情報が含まれる HTML ファイル。includedSupportPackages.txt
— ライブラリに含まれるすべてのサポート ファイルをリストしたテキスト ファイル。mccExcludedFiles.log
— アプリケーションに含まれていないすべてのツールボックス関数のリストが含まれるログ ファイル。サポートされていない関数の詳細については、MATLAB Compiler の制限を参照してください。readme.txt
— パッケージ化およびインターフェイスの情報が含まれるテキスト ファイル。requiredMCRProducts.txt
— MATLAB Runtime がアプリケーションを実行するために必要な製品の製品 ID が含まれるテキスト ファイル。unresolvedSymbols.txt
— 未解決のシンボルに関する情報が含まれるテキスト ファイル。
メモ
生成されたライブラリに MATLAB Runtime やインストーラーは含まれていません。
buildResults
オブジェクトを使用してインストーラーを作成するには、compiler.package.installer
を参照してください。
C++ MATLAB データ API 共有ライブラリをサンプル アプリケーションとともに実装
メモ
3 つすべての関数を呼び出してエラーを処理する、より高度なアプリケーションを使用してライブラリを呼び出すには、次のフォルダーに配置されている C++ アプリケーション matrix_mda.cpp
を使用します。
matlabroot
\extern\examples\compilersdk\c_cpp\matrix
開始する前に、MATLAB Runtime のダウンロードとインストールが完了していて、C++ コンパイラがインストールされていることを確認してください。
C++ 共有ライブラリをパッケージ化した後、C++ アプリケーションから呼び出すことができます。samples
フォルダーで生成された C++ コードは、作成したサンプル MATLAB ファイルを基にしています。
生成されたファイル
libmatrix.ctf
をv2\generic_interface
フォルダーからコピーして、libmatrixSample1_mda.cpp
を含むsamples
フォルダーに貼り付けます。libmatrixSample1_mda.cpp
のプログラム コードを以下に示します。/*================================================================= * * ADDMATRIXSAMPLE1 * Sample driver code that uses the generic interface and * MATLAB Data API to call a C++ shared library created using * MATLAB Compiler SDK. * Refer to the MATLAB Compiler SDK documentation for more * information. * *=================================================================*/ // Include the header file required to use the generic // interface for the C++ shared library generated by the // MATLAB Compiler SDK. #include "MatlabCppSharedLib.hpp" #include <iostream> namespace mc = matlab::cpplib; namespace md = matlab::data; std::shared_ptr<mc::MATLABApplication> setup() { auto mode = mc::MATLABApplicationMode::IN_PROCESS; // Specify MATLAB startup options std::vector<std::u16string> options = {}; std::shared_ptr<mc::MATLABApplication> matlabApplication = mc::initMATLABApplication(mode, options); return matlabApplication; } int mainFunc(std::shared_ptr<mc::MATLABApplication> app, const int argc, const char * argv[]) { md::ArrayFactory factory; md::TypedArray<double> a1In = factory.createArray<double>({3, 3}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}); md::TypedArray<double> a2In = factory.createArray<double>({3, 3}, {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}); try { // The path to the CTF (library archive file) passed to // initMATLABLibrary or initMATLABLibraryAsync may be either absolute // or relative. If it is relative, the following will be prepended // to it, in turn, in order to find the CTF: // - the directory named by the environment variable // CPPSHARED_BASE_CTF_PATH, if defined // - the working directory // - the directory where the executable is located // - on Mac, the directory three levels above the directory // where the executable is located // If the CTF is not in one of these locations, do one of the following: // - copy the CTF // - move the CTF // - change the working directory ("cd") to the location of the CTF // - set the environment variable to the location of the CTF // - edit the code to change the path auto lib = mc::initMATLABLibrary(app, u"libmatrix.ctf"); std::vector<md::Array> inputs{a1In, a2In}; auto result = lib->feval(u"addmatrix", 1, inputs); } catch (const std::exception & exc) { std::cerr << exc.what() << std::endl; return -1; } return 0; } // The main routine. On the Mac, the main thread runs the system code, and // user code must be processed by a secondary thread. On other platforms, // the main thread runs both the system code and the user code. int main(const int argc, const char * argv[]) { int ret = 0; try { auto matlabApplication = setup(); ret = mc::runMain(mainFunc, std::move(matlabApplication), argc, argv); // Calling reset() on matlabApplication allows the user to control // when it is destroyed, which automatically cleans up its resources. // Here, the object would go out of scope and be destroyed at the end // of the block anyway, even if reset() were not called. // Whether the matlabApplication object is explicitly or implicitly // destroyed, initMATLABApplication() cannot be called again within // the same process. matlabApplication.reset(); } catch(const std::exception & exc) { std::cerr << exc.what() << std::endl; return -1; } return ret; }
MATLAB コマンド プロンプトまたはシステム コマンド プロンプトで、
libmatrix.ctf
のコピー先のsamples
フォルダーに移動します。システムのコマンド プロンプトで
mbuild
を使用して、アプリケーションをコンパイルしてリンクします。mbuild libmatrixSample1_mda.cpp
システムのコマンド プロンプトからアプリケーションを実行します。
addmatrixSample1_mda.exe
既定では、生成された C++ コードは出力を表示しません。
(オプション)
mbuild
を使用して、その他のサンプル C++ アプリケーションをコンパイルしてリンクします。生成された C++ コードは、独自のアプリケーションを作成するためのガイドとしても使用できます。詳細については、Integrate C++ Shared Libraries with MATLAB Data APIを参照してください。
メモ
struct 配列、cell 配列、または文字ベクトルを feval
の呼び出しから取得して表示する方法の例については、
の matlabroot
\extern\examples\compilersdk\c_cpp\matrixsubtractmatrix.m
ファイルおよび subtractmatrix_mda.cpp
ファイルを参照してください。
参考
compiler.build.cppSharedLibrary
| mcc