在此页面

使用开发容器

使用开发容器(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 可将命令桥助手(一种用于与远程设备通信的服务)复制到开发容器中,而不是尝试挂载它。如果开发容器无法加载主机文件系统,这将非常有用。
mount-libexecboolean设置为true 可将libexec 目录挂载到 dev 容器中。该目录用于命令桥接助手。
libexec-mount-pointstring指定libexec 目录在 dev 容器中的挂载点。这将用于命令桥接助手。
kits对象数组指定要在开发容器中使用的自定义工具包。

典型本地开发配置

下面的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 二进制文件和生成器以及调试器。

{
    "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"
                    },
                    "debugger": "/usr/bin/lldb"
                }
            ]
        }
    }
}

另请参阅 启用和禁用插件以及如何为 Docker 开发:为 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.