主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

macOS 编写应用程序

适用于 Apple Cocoa API 的 Objective-C/C++ 应用程序

Apple Xcode 以 Objective-C 语言实现,用于使用 Cocoa 框架(macOS 操作系统的原生面向对象 API)开发应用程序。

本文详细介绍了如何使用 Objective C 和 Cocoa 创建图形 MATLAB® 应用程序,然后使用 MATLAB Compiler SDK™ 进行部署。

示例代码的位置

您可以在 matlabroot/extern/examples/compilersdk/c_cpp/triangle/xcode 中找到示例 Apple Xcode、标头和工程文件。

准备您的 Apple Xcode 开发环境

要运行此示例,您应该具有使用 Apple Xcode 开发环境和 Cocoa 框架的经验。

本文中的示例已准备好构建和运行。但是,在构建和运行自己的应用程序之前,您必须执行以下操作(如我们的示例代码中所做的那样):

  1. 使用 C++ 共享库编译器、compiler.build.cppSharedLibrarymcc 编译 MATLAB Compiler SDK 构建共享库。

  2. 根据库的头文件编译应用程序代码,并将应用程序与组件库和 libmwmclmcrrt 链接。

  3. 在您的 Apple Xcode 工程中:

    • 在工程目标中指定 mcc示例代码中的构建组件库)。

    • HEADER_SEARCH_PATHS 中指定目标设置。

      • 指定包含库头的目录。

      • 指定路径 matlabroot/extern/include

      • 定义 MWINSTALL_ROOT,它使用相对路径建立安装路由。

    • LIBRARY_SEARCH_PATHS 设置为包含共享库的任何目录,以及基于 Intel® 处理器的 macOS 的路径 matlabroot/runtime/maci64 或基于 Apple 芯片macOS 的路径 matlabroot/runtime/maca64

构建并运行 Sierpinski 应用程序

在此示例中,部署图形 Sierpinski 函数 sierpinski.m,位于 matlabroot/extern/examples/compilersdk/c_cpp/triangle

function [x, y] = sierpinski(iterations, draw)
% SIERPINSKI Calculate (optionally draw) the points 
%  in Sierpinski's triangle

% Copyright 2004 The MathWorks, Inc.

    % Three points defining a nice wide triangle
    points = [0.5 0.9 ; 0.1 0.1 ; 0.9 0.1];

    % Select an initial point
    current = rand(1, 2);

    % Create a figure window
    if (draw == true)
        f = figure;
        hold on;
    end

    % Pre-allocate space for the results, to improve performance
    x = zeros(1,iterations);
    y = zeros(1,iterations);

    % Iterate
    for i = 1:iterations

        % Select point at random
        index = floor(rand * 3) + 1;

        % Calculate midpoint between current point and random point
        current(1) = (current(1) + points(index, 1)) / 2;
        current(2) = (current(2) + points(index, 2)) / 2;

        % Plot that point
        if draw, line(current(1),current(2));, end
	x(i) = current(1);
        y(i) = current(2);

    end

    if (draw)
        drawnow;
    end
    
  1. 使用 Mac Finder 定位 Apple Xcode 工程 (matlabroot/extern/examples/compilersdk/c_cpp/triangle/xcode)。如果需要,将文件复制到工作目录以运行此示例。

  2. 打开 sierpinski.xcodeproj。开发环境启动。

  3. Groups and Files 窗格中,选择 Targets

  4. 点击 Build and Run。运行 make 文件并启动 MATLAB Compiler™ (mcc)。

运行 Sierpinski 应用程序

从构建输出目录运行 Sierpinski 应用程序。出现以下 GUI:

在 Mac Cocoa 环境中实现 MATLAB Sierpinski 函数

  1. Iterations 字段中,输入一个整数,例如 10000

  2. 点击 Draw Triangle。出现下图: