本页内容

使用开发容器

使用开发容器(dev container)为您的项目定义并配置一个一致的开发环境,该环境可在开发人员以及自动化构建或测试系统之间保持一致。

您可以本地或远程在云端运行开发容器,用于运行应用程序、隔离处理代码库时所需的工具、库或运行时,或用于持续集成和测试。

开发容器环境可能与部署环境不同,因为它们通常包含开发过程中所需的工具和实用程序,而这些在最终部署时并不需要。

有关完整规范和devcontainer.json 文件格式,请参阅开发容器文档。

注意:需启用 “开发容器支持”插件才能使用此功能。

配置开发容器

要为Qt Creator 配置开发容器:

  1. 在项目目录中创建一个devcontainer.json 文件。
  2. devcontainer.json 文件中的customizations 下添加一个qt-creator 部分。
  3. 使用本主题中描述的自定义选项来定制开发容器。

开发容器的自定义选项

下表列出了可在devcontainer.json 文件中用于自定义Qt Creator 的可用选项。

属性类型描述
auto-detect-kitsboolean设置为true 以尝试在开发容器中自动检测套件。
run-processes-in-terminalboolean设置为true 以在终端窗口中运行部分开发容器的设置过程。目前仅用于docker build
copy-cmd-bridgeboolean设置为true ,将用于与远程设备通信的Command Bridge Helper服务复制到开发容器中,而非尝试挂载该服务。当开发容器无法挂载主机文件系统时,此设置非常有用。
mount-libexecboolean设置为true ,将libexec 目录挂载到开发容器中。此选项用于 Command Bridge Helper。
libexec-mount-pointstring指定 dev 容器中libexec 目录的挂载点。此设置用于 Command Bridge Helper。
kits一个对象数组指定要在 dev 容器中使用的自定义套件。

典型本地开发配置

以下示例devcontainer.json 文件展示了如何为典型的本地开发工作流配置 dev 容器。它启用了套件的自动检测,将libexec 目录挂载到容器中,并指定了挂载点。

当您希望Qt Creator 自动查找可用的构建套件,并确保容器内可访问libexec 目录中的所需辅助工具时,此设置非常有用。

{
    "customizations": {
        "qt-creator": {
            "device": {
                "auto-detect-kits": true,
                "run-processes-in-terminal": false,
                "copy-cmd-bridge": false,
                "mount-libexec": true,
                "libexec-mount-point": "/devcontainer/libexec"
            }
        }
    }
}

自定义套件和工具链的配置

以下示例devcontainer.json 文件演示了当您需要在容器内使用特定的 Qt 版本、编译器或工具链时,如何配置开发容器。这对于交叉编译、支持多种构建配置,或确保跨团队开发环境的一致性非常有用。

此配置文件禁用了自动套件检测,转而通过kits 属性定义了一个自定义套件。该套件指定了Qt Creator 在开发容器中将使用的 Qt 版本、编译器路径、CMake 二进制文件和生成器,以及调试器。

该配置文件还使用可选的cmake.variables 属性,在Qt Creator 配置项目时传递任意的CMake变量。每个键都是一个CMake变量名,其值可以是:

  • 一个普通字符串
  • 字符串数组(通过; 连接成单个CMake列表值)
  • 包含value 字段以及可选type 字段(BOOLSTRINGFILEPATHPATHSTATICINTERNAL )的对象。该类型将作为-DVAR_NAME:TYPE=value 附加到 CMake 命令行中。
{
    "customizations": {
        "qt-creator": {
            "auto-detect-kits": false,
            "kits": [
                {
                    "name": "My DevContainer Kit",
                    "qt": "/6.7.0/gcc_64/bin/qmake6",
                    "compiler": {
                        "Cxx": "/usr/bin/c++",
                        "C": "/usr/bin/gcc"
                    },
                    "cmake": {
                        "binary": "/usr/bin/cmake",
                        "generator": "Unix Makefiles",
                        "variables": {
                            "CMAKE_BUILD_TYPE": "Release",
                            "CMAKE_PREFIX_PATH": ["/opt/qt", "/usr/local"],
                            "BUILD_TESTING": {
                                "type": "BOOL",
                                "value": "OFF"
                            }
                        }
                    },
                    "debugger": "/usr/bin/lldb"
                }
            ]
        }
    }
}

另请参阅 “启用和禁用插件”以及“如何:针对 Docker 进行开发”

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.