主要内容

生成 C++ mwArray 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

  3. 创建调用函数的 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 对其进行编译。

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

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

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

    functionfiles = {'addmatrix.m', 'multiplymatrix.m', 'eigmatrix.m'}
  2. 使用 compiler.build.cppSharedLibrary 函数编译 C++ 共享库。使用名称-值参量添加示例文件并指定库名称和接口 API。

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

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

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

    该函数会在当前工作目录中名为 libmatrixcppSharedLibrary 的文件夹中生成以下文件:

    • samples\libmatrixSample1_mwarray.cpp - 调用 addmatrix 函数的 C++ 示例应用程序。

    • samples\libmatrixSample2_mwarray.cpp - 调用 eigmatrix 函数的 C++ 示例应用程序。

    • samples\libmatrixSample3_mwarray.cpp - 调用 multiplymatrix 函数的 C++ 示例应用程序。

    • GettingStarted.html - 包含有关集成您共享库的信息的 HTML 文件。

    • includedSupportPackages.txt - 列出库中包含的所有支持文件的文本文件。

    • libmatrix.cpp - C++ 源代码文件。

    • libmatrix.def - 为链接器提供模块信息的模块定义文件。

    • libmatrix.dll - 动态链接库文件。

    • libmatrix.exports - 包含所有非静态函数名称的导出文件。

    • libmatrix.h - C++ 头文件。

    • libmatrix.lib - 导入库文件。

    • mccExcludedFiles.log - 包含应用程序中未包含的任何工具箱函数列表的日志文件。有关不支持的函数的信息,请参阅 MATLAB Compiler 限制

    • readme.txt - 包含打包信息的文本文件。

    • requiredMCRProducts.txt - 包含 MATLAB Runtime 运行应用程序所需产品的产品 ID 的文本文件。

    • unresolvedSymbols.txt - 包含有关未解析符号的信息的文本文件。

    注意

    生成的库不包括 MATLAB Runtime 或安装程序。要使用 buildResults 对象创建安装程序,请参阅 compiler.package.installer

使用 C++ 示例应用程序实现 C++ mwArray API 共享库

注意

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

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

开始之前,请确保您已安装 C++ 编译器

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

  1. 将生成的 C++ 代码文件 libmatrixSample1_mwarray.cppsamples 文件夹复制并粘贴到包含 libmatrix.lib 的文件夹中。

    libmatrixSample1_mwarray.cpp 的程序列表如下所示。

    /*=================================================================
     *
     * LIBMATRIXSAMPLE1
     * CPP Sample driver code for libmatrix that calls a shared library
     * created using MATLAB Compiler SDK.
     * Refer to the MATLAB Compiler SDK documentation for more information.
     *
     *=================================================================*/
    // Include the library specific header file as generated by the 
    // MATLAB Compiler
    #include <iostream>
    #include "libmatrix.h"
    
    void addmatrixSample() {
        try {
            mxDouble a1InData[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
            mwArray a1In(3, 3, mxDOUBLE_CLASS);
            a1In.SetData(a1InData, 9);
            mxDouble a2InData[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
            mwArray a2In(3, 3, mxDOUBLE_CLASS);
            a2In.SetData(a2InData, 9);
            mwArray aOut;
            addmatrix(1, aOut, a1In, a2In);
            std::cout << aOut << '\n';
        } catch (const mwException& e) {
            std::cerr << e.what() << std::endl;
        } catch (...) {
            std::cerr << "Unexpected error thrown" << std::endl;
        }
    }
    
    int run_main(int argc, const char** argv) {
        if (!libmatrixInitialize()) {
            std::cerr << "Could not initialize the library properly" << std::endl;
            return 2;
        } else {
            addmatrixSample();
            // Call the application and library termination routine
            libmatrixTerminate();
        }
        // Note that you should call mclTerminateApplication at the end of
        // your application to shut down all MATLAB Runtime instances.
        mclTerminateApplication();
        return 0;
    }
    
    // The main routine. On macOS, 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(int argc, const char** argv) {
        /* Call the mclInitializeApplication routine. Make sure that the application
         * was initialized properly by checking the return status. This initialization
         * has to be done before calling any MATLAB APIs or MATLAB Compiler SDK
         * generated shared library functions.
         */
        if (!mclInitializeApplication(nullptr, 0)) {
            std::cerr << "Could not initialize the application." << std::endl;
            return 1;
        }
        return mclRunMain(static_cast<mclMainFcnType>(run_main), argc, argv);
    }
    
  2. 在系统命令提示符下,导航到您复制 libmatrixSample1_mwarray.cpp 的文件夹。

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

    mbuild libmatrixSample1_mwarray.cpp libmatrix.lib

    注意

    .lib 是 Windows 中使用的扩展名。在 macOS 上,文件扩展名为 .dylib,在 Linux 上为 .so

  4. 从系统命令提示符运行应用程序。如果您在打包步骤中使用了 MATLAB 示例代码,则示例 C++ 应用程序会返回与 MATLAB 代码相同的输出。

    libmatrixSample1_mwarray.exe
    2 8 14
    4 10 16
    6 12 18
    
  5. (可选)使用 mbuild 编译并链接其他 C++ 示例应用程序。您还可以将生成的 C++ 代码用作指南来创建自己的应用程序。

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

另请参阅

|

主题