主要内容

生成 C++ MATLAB Data API 共享库并编译 C++ 应用程序

支持的平台:Windows®、Linux®Mac

此示例说明如何从 MATLAB® 函数创建 C++ 共享库。您可以将生成的库集成到 C++ 应用程序中。此示例还说明如何从 C++ 应用程序调用 C++ 共享库。目标系统不需要 MATLAB 的许可副本即可运行该应用程序。

MATLAB 中创建函数

  1. 在 MATLAB 中,检查您想要打包的 MATLAB 代码。

    将 MATLAB 附带的 matrix 文件夹复制到您的工作文件夹。

    copyfile(fullfile(matlabroot,'extern','examples','compilersdk','c_cpp','matrix'),'matrix')

    导航到工作文件夹中的新 matrix 子文件夹。

  2. 检查并测试函数 addmatrix.mmultiplymatrix.meigmatrix.m

使用 compiler.build.cppSharedLibrary 创建 C++ 共享库

使用编程方法编译 C++ 共享库。或者,如果您想使用图形界面创建 C++ 共享库,请参阅使用 C++ 共享库编译器,通过 MATLAB 数据 API 调用 MATLAB 函数。

  1. 将函数文件列表保存在元胞数组中。

    functionfiles = {'addmatrix.m', 'multiplymatrix.m', 'eigmatrix.m'}
  2. 创建调用函数的 MATLAB 示例代码。示例文件用于生成目标语言的示例应用程序。有关详细信息和限制,请参阅创建示例代码来调用导出函数

    将以下代码保存在名为 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 对其进行编译。

  3. 使用 compiler.build.cppSharedLibrary 函数编译 C++ 共享库。使用名称-值参量指定库名称并添加示例文件。

    buildResults = compiler.build.cppSharedLibrary(functionfiles,...
    'LibraryName','libmatrix',...
    'SampleGenerationFiles','libmatrixSample.m');

    您可以使用名称-值参量在 compiler.build 命令中指定其他选项。有关详细信息,请参阅 compiler.build.cppSharedLibrary

    compiler.build.Results 对象 buildResults 包含有关编译类型、生成的文件、包含的支持包和编译选项的信息。

  4. 此语法会在当前工作目录中名为 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 Data API 共享库

注意

要使用调用所有三个函数并处理错误的更高级应用程序调用库,请使用位于以下文件夹中的 C++ 应用程序 matrix_mda.cpp

matlabroot\extern\examples\compilersdk\c_cpp\matrix
有关详细信息,请参阅将 C++ 共享库与 MATLAB Data API 集成

开始之前,请确保您下载并安装 MATLAB Runtime已安装 C++ 编译器

打包 C++ 共享库后,您可以从 C++ 应用程序调用这些共享库。在 samples 中生成的 C++ 代码基于您创建的 MATLAB 示例文件。

  1. 将生成的 libmatrix.ctf 文件从 v2\generic_interface 文件夹复制并粘贴到包含 libmatrixSample1_mda.cppsamples 文件夹中。

    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;
    }
  2. 在 MATLAB 命令提示符或系统命令提示符下,导航到您复制 libmatrix.ctfsamples 文件夹。

  3. 在系统命令提示符下使用 mbuild 编译并链接应用程序。

    mbuild libmatrixSample1_mda.cpp
  4. 从系统命令提示符运行应用程序。

    addmatrixSample1_mda.exe

    默认情况下,生成的 C++ 代码不会显示任何输出。

  5. (可选)使用 mbuild 编译并链接其他 C++ 示例应用程序。您还可以将生成的 C++ 代码用作指南来创建自己的应用程序。

    有关更多详细信息,请参阅将 C++ 共享库与 MATLAB Data API 集成

注意

有关如何从 feval 调用中检索和显示结构体数组、元胞数组或字符向量的示例,请参阅 matlabroot\extern\examples\compilersdk\c_cpp\matrix 中的文件 subtractmatrix.msubtractmatrix_mda.cpp

另请参阅

|

主题