步骤 1 - 创建带有 C++ 应用程序的项目并打印到调试控制台。

在此步骤中,您首先使用 C++ 应用程序创建一个 Kanzi Studio 项目。然后,您将代码添加到 C++ 应用程序中,以在 Kanzi 调试控制台上打印 Hello world!。

获取教程

要获取本教程,请在 Kanzi Studio 的 Quick Start 窗口中点击 Examples。在 Hello world 教程旁点击 下载。

Kanzi Studio 会将教程下载到 <KanziWorkspace>/Tutorials 目录。您可以在 <KanziWorkspace>/Tutorials/Hello world/Completed 目录中找到已完成的教程。

创建含 C++ 应用程序的工程并打印到调试控制台

要创建含 C++ 应用程序的工程并打印到 Kanzi 调试控制台:

  1. 在 Kanzi Studio Quick Start 窗口中,点击 New Project:

    1. 选择工程名称和位置。

      例如,将工程命名为 Hello world。

    2. 将 Template 设置为 Application。

    单击 Create。

    ../../_images/create-new-project-with-cpp-application.png

    Kanzi 在 <ProjectName>/Tool_project 目录中创建一个 Kanzi Studio 项目,并在 <ProjectName>/Application 目录中创建项目的应用程序代码结构:

    • bin 目录包含项目的 kzb 和配置文件。

    • configs 目录包含不同平台的配置文件。默认情况下,Kanzi 会为多个不同的平台创建配置文件。开发 Kanzi 应用程序的推荐平台是 Microsoft Visual Studio。请参阅 部署 Kanzi 应用程序。

    • src 目录包含您的应用程序的源代码。

    • CMakeLists.txt 文件包含 CMake 为 Kanzi 应用程序生成 Visual Studio 解决方案的说明。

    • generate_cmake_vs<Version>_solution.bat 是一个脚本,您可以使用它为 Kanzi 应用程序生成一个 Visual Studio 解决方案。该脚本在 <ProjectName>/Application/build_vs<Version> 目录中生成 Visual Studio 解决方案。

  2. 在 Kanzi Studio 中,选择 File > Export > Export KZB。

    Kanzi Studio creates the kzb file and configuration files from your Kanzi Studio project. Kanzi Studio stores the exported files in the <ProjectName>/Application/bin directory or the location that you set in Project > Properties in the Binary Export Directory property. The kzb file contains all nodes and resources from your Kanzi Studio project, except the resources that you mark in a localization table as locale packs.

    当您从 Visual Studio 运行您的 Kanzi 应用程序时,您的应用程序会加载 kzb 文件和配置文件。

    ../../_images/export-kzb-binary.png
  3. 在 Kanzi Studio 中,选择 File > Open Kanzi Command Prompt。

    Kanzi Command Prompt 打开 Windows 命令提示符,并为您打开 Kanzi Command Prompt 的 Kanzi 版本设置 Kanzi 环境变量。

    使用 Kanzi Command Prompt 访问 Kanzi 实用程序和构建工具,例如 Gradle、SCons 和 CMake,而无需使用绝对路径或设置环境变量。

    ../../_images/open-kanzi-command-prompt.png

    小技巧

    您可以在 Windows 开始菜单的 Rightware 目录中找到 Kanzi Command Prompt。

    当您安装了多个版本的 Kanzi 时,确保将您要在该命令提示符下使用的 Kanzi 版本的 Kanzi Command Prompt 启动。

  4. 在 <ProjectName>/Application 目录中的 Kanzi Command Prompt 中,运行生成应用程序的 Visual Studio 解决方案的脚本。

    例如,如果您使用 Visual Studio 2019,运行

    generate_cmake_vs2019_solution.bat
    

    此脚本在目录 <ProjectName>/Application/build_vs2019 中为应用程序生成一个 Visual Studio 解决方案。

  5. 在 Visual Studio 中,打开 Application/build_vs<Version>/<ProjectName>.sln Visual Studio 解决方案。

    例如,如果您将 Kanzi Studio 项目命名为 Hello world,则 Visual Studio 解决方案将被称为 Hello_world.sln。

    ../../_images/hello-world-project-contents.png
  6. 在 Solution Explorer 中,右键点击 Hello_world 工程并选择 Set as StartUp Project。

    ../../_images/visual-studio-set-as-startup-project3.png
  7. 打开 hello_world.cpp 文件。

    默认情况下,模板会添加 kanzi.hpp 头文件,其中包括其他 Kanzi 头文件和 Code Behind 标识符。在本教程中,您不使用 Code Behind。

    此应用程序类使用 ExampleApplication 类,通过处理所需模块的注册来简化应用程序开发。

    #include <kanzi/kanzi.hpp>
    
    // [CodeBehind libs inclusion]. Do not remove this identifier.
    
    #if defined(HELLO_WORLD_CODE_BEHIND_API) && !defined(ANDROID) && !defined(KANZI_CORE_API_IMPORT)
    #include <Hello_world_code_behind_module.hpp>
    #endif
    
    using namespace kanzi;
    
    class HelloWorld : public ExampleApplication
    
  8. 要使用记录器打印到 Kanzi 调试控制台,请包含 log.hpp 头文件:

    #include <kanzi/core/log/log.hpp>
    
  9. 将 Application::onProjectLoaded 的内容替换为以下代码。

    当您将函数放置在 Application::onProjectLoaded 内时,Kanzi 在加载您的应用程序后调用该函数。

    void onProjectLoaded() override
    {
        // Prints Hello world! to the Kanzi debug console.
        kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("Hello world!"));
    }
    
  10. 选择您要使用的解决方案配置并运行您的应用程序。

    在开发期间,选择 Debug 配置。当您准备好创建生产版本时,选择 Release 配置。

    ../../_images/debug-configuration.png

    log.hpp::kzLogInfo 函数将 Hello world! 打印到 Kanzi 调试控制台。

    ../../_images/kanzi-debug-console.png

    当您在调试模式下运行应用程序时,Kanzi 显示调试控制台。默认情况下,Kanzi Studio 项目包含一个空场景。

当您完成此步骤时,这就是您的 hello_world.cpp 的样子。

#include <kanzi/kanzi.hpp>
#include <kanzi/core/log/log.hpp>

// [CodeBehind libs inclusion]. Do not remove this identifier.

#if defined(HELLO_WORLD_CODE_BEHIND_API) && !defined(ANDROID) && !defined(KANZI_CORE_API_IMPORT)
#include <Hello_world_code_behind_module.hpp>
#endif

using namespace kanzi;

class HelloWorld : public ExampleApplication
{
public:

   void onConfigure(ApplicationProperties& configuration) override
   {
      configuration.binaryName = "hello_world.kzb.cfg";
   }

   void onProjectLoaded() override
   {
      // Prints Hello world! to the Kanzi debug console.
      kzLogInfo(KZ_LOG_CATEGORY_GENERIC, ("Hello world!"));
   }

   void registerMetadataOverride(ObjectFactory& factory) override
   {
      ExampleApplication::registerMetadataOverride(factory);

#if defined(HELLO_WORLD_CODE_BEHIND_API) && !defined(ANDROID) && !defined(KANZI_CORE_API_IMPORT)
      HelloWorldCodeBehindModule::registerModule(getDomain());
#endif

      // [CodeBehind module inclusion]. Do not remove this identifier.
   }
};

Application* createApplication()
{
   return new HelloWorld;
}

另请参阅

Kanzi Engine C++ API 参考