Visual Studio 中的 CMake 项目CMake projects in Visual Studio

CMake 是一种跨平台开源工具,用于定义在多个平台上运行的生成过程。CMake is a cross-platform, open-source tool for defining build processes that run on multiple platforms. 本文假定你熟悉 CMake。This article assumes you're familiar with CMake. 可在使用 CMake 生成、测试和打包软件时了解更多关于它的信息。You can learn more about it at Build, Test and Package Your Software With CMake.

备注

在过去的几个版本中,CMake 已越来越多地与 Visual Studio 集成在一起。CMake has become more and more integrated with Visual Studio over the past few releases. 若要查看 Visual Studio 首选项的文档,请使用“版本”选择器控件。To see the documentation for your preferred version of Visual Studio, use the Version selector control. 它位于此页面上目录表的顶部。It's found at the top of the table of contents on this page.

“用于 Windows 的 C++ CMake 工具”组件使用“打开文件夹”功能,以直接将 CMake 项目文件(例如 CMakeLists.txt)用于 IntelliSense 和浏览。The C++ CMake tools for Windows component uses the Open Folder feature to consume CMake project files (such as CMakeLists.txt) directly for the purposes of IntelliSense and browsing. 支持 Ninja 和 Visual Studio 生成器。Both Ninja and Visual Studio generators are supported. 如果使用 Visual Studio 生成器,则它会生成临时项目文件,并将它传递给 msbuild.exe。If you use a Visual Studio generator, it generates a temporary project file and passes it to msbuild.exe. 但是,从不会加载项目来用于 IntelliSense 或浏览。However, the project is never loaded for IntelliSense or browsing purposes. 还可以导入现有 CMake 缓存。You can also import an existing CMake cache.

安装Installation

“用于 Windows 的 C++ CMake 工具”作为“使用 C++ 的桌面开发”和“使用 C++ 的 Linux 开发”工作负载的一部分进行安装。C++ CMake tools for Windows is installed as part of the Desktop development with C++ and Linux Development with C++ workloads. 有关详细信息,请参阅跨平台 CMake 项目For more information, see Cross-platform CMake projects.

已选中“C++”下拉列表且已调用“用于 Windows 的 C++ C Make 工具”选项的桌面开发的屏幕截图。

有关详细信息,请参阅在 Visual Studio 中安装 C++ Linux 工作负载For more information, see Install the C++ Linux workload in Visual Studio.

IDE 集成IDE Integration

选择“文件”>“打开”>“文件夹”以打开一个包含 CMakeLists.txt 文件的文件夹时,将发生以下情况:When you choose File > Open > Folder to open a folder containing a CMakeLists.txt file, the following things happen:

  • Visual Studio 将“CMake”项添加到“项目”菜单,其中包含用于查看和编辑 CMake 脚本的命令 。Visual Studio adds CMake items to the Project menu, with commands for viewing and editing CMake scripts.

  • 解决方案资源管理器显示文件夹结构和文件。Solution Explorer displays the folder structure and files.

  • Visual Studio 运行 cmake.exe 并为默认(x64 调试)配置生成 CMake 缓存文件 (CMakeCache.txt)。Visual Studio runs cmake.exe and generates the CMake cache file (CMakeCache.txt) for the default (x64 Debug) configuration. 输出窗口中显示 CMake 命令行以及 CMake 的其他输出。The CMake command line is displayed in the Output Window, along with additional output from CMake.

  • 在后台,Visual Studio 开始对源文件编制索引,以启用 IntelliSense、浏览信息和重构等等。In the background, Visual Studio starts to index the source files to enable IntelliSense, browsing information, refactoring, and so on. 随着工作进行,Visual Studio 监视器在编辑器和磁盘中随之发生变化,以保持其索引与源同步。As you work, Visual Studio monitors changes in the editor and also on disk to keep its index in sync with the sources.

可以打开包含任意数量 CMake 项目的文件夹。You can open folders containing any number of CMake projects. Visual Studio 检测并配置工作区中的所有“根”CMakeLists.txt 文件。Visual Studio detects and configures all the "root" CMakeLists.txt files in your workspace. CMake 操作(配置、生成、调试)、C++ IntelliSense 和浏览可用于工作区中的所有 CMake 项目。CMake operations (configure, build, debug), C++ IntelliSense, and browsing are available to all CMake projects in your workspace.

包含多个根的 CMake 项目

还可按目标查看经过逻辑组织的项目。You can also view your projects organized logically by targets. 在“解决方案资源管理器”工具栏中,从下拉列表中选择“目标视图” :Choose Targets view from the dropdown in the Solution Explorer toolbar:

CMake“目标视图”按钮

单击“解决方案资源管理器”顶部的“显示所有文件”按钮,以查看 out/build/<config> 文件夹中的所有 CMake 生成的输出。Click the Show All Files button at the top of Solution Explorer to see all the CMake-generated output in the out/build/<config> folders.

Visual Studio 使用名为 CMakeSettings.json 的配置文件。Visual Studio uses a configuration file called CMakeSettings.json. 此文件使你可以定义和存储多个生成配置,并且可在 IDE 中方便地切换它们。This file lets you define and store multiple build configurations, and conveniently switch between them in the IDE. 配置是一种 Visual Studio 构造,用于封装特定于给定生成类型的设置。A configuration is a Visual Studio construct that encapsulates settings that are specific to a given build type. 这些设置用于配置 Visual Studio 传递给 cmake.exe 的默认命令行选项。The settings are used to configure the default command-line options that Visual Studio passes to cmake.exe. 还可以在此处指定其他 CMake 选项,并定义所需的任何其他变量。You can also specify additional CMake options here, and define any additional variables you like. 所有选项都作为内部或外部变量写入 CMake 缓存。All options are written to the CMake cache either as internal or external variables. 在 Visual Studio 2019 中,CMake 设置编辑器 提供一种简便方法来编辑设置。In Visual Studio 2019, the CMake Settings Editor provides a convenient way to edit your settings. 有关详细信息,请参阅自定义 CMake 设置For more information, see Customize CMake settings.

一个设置 (intelliSenseMode) 不会传递给 CMake,而是仅供 Visual Studio 使用。One setting, intelliSenseMode isn't passed to CMake, but is used only by Visual Studio.

如同在任何 CMake 项目中一样,使用每个项目文件夹中的 CMakeLists.txt 文件。Use the CMakeLists.txt file in each project folder just as you would in any CMake project. 可以指定源文件、查找库、设置编译器和链接器选项,以及指定其他与生成系统相关的信息。You can specify source files, find libraries, set compiler and linker options, and specify other build system-related information.

若要在调试时将参数传递给可执行文件,可以使用另一个名为 launch.vs.json 的文件。To pass arguments to an executable at debug time, you can use another file called launch.vs.json. 在某些情况中,Visual Studio 会自动生成这些文件。In some scenarios, Visual Studio automatically generates these files. 可以手动编辑它们,甚至可以自己创建文件。You can edit them manually, or even create the file yourself.

备注

对于其他类型的“打开文件夹”项目,将使用另外两个 JSON 文件:CppProperties.json 和 tasks.vs.json。For other kinds of Open Folder projects, two additional JSON files are used: CppProperties.json and tasks.vs.json. 这些都与 CMake 项目无关。Neither of these are relevant for CMake projects.

打开现有缓存Open an existing cache

打开现有 CMake 缓存文件 (CMakeCache.txt) 时,Visual Studio 不会尝试为你管理缓存和生成树。When you open an existing CMake cache file (CMakeCache.txt), Visual Studio doesn't try to manage your cache and build tree for you. 自定义或首选工具会完全控制 CMake 配置项目的方式。Your custom or preferred tools have complete control over how CMake configures your project. 若要在 Visual Studio 中打开现有缓存,请选择“文件”>“打开”>“CMake”。To open an existing cache in Visual Studio, choose File > Open > CMake. 然后,导航到现有 CMakeCache.txt 文件。Then, navigate to an existing CMakeCache.txt file.

可以将现有 CMake 缓存添加到打开的项目中。You can add an existing CMake cache to an open project. 执行该操作的方式与添加新配置相同。It's done the same way you'd add a new configuration. 有关详细信息,请参阅关于在 Visual Studio 中打开现有缓存的博客文章。For more information, see our blog post on opening an existing cache in Visual Studio.

生成 CMake 项目Building CMake projects

要生成 CMake 项目,可选择执行以下操作:To build a CMake project, you have these choices:

  1. 在“常规”工具栏中,找到“配置”下拉菜单。In the General toolbar, find the Configurations dropdown. 默认情况下,它可能显示“x64-Debug”。It probably shows "x64-Debug" by default. 选择首选配置,然后按 F5 或单击工具栏上的“运行”(绿色三角形)按钮 。Select the preferred configuration and press F5, or click the Run (green triangle) button on the toolbar. 项目首先自动生成,就像 Visual Studio 解决方案一样。The project automatically builds first, just like a Visual Studio solution.

  2. 右键单击 CMakeLists.txt,并从上下文菜单中选择“生成”。Right click on CMakeLists.txt and select Build from the context menu. 如果在文件夹结构中有多个目标,可以选择生成所有目标或仅生成某个特定目标。If you have multiple targets in your folder structure, you can choose to build all or only one specific target.

  3. 从主菜单中选择“生成”>“全部生成”(F7 或 Ctrl+Shift+B) 。From the main menu, select Build > Build All (F7 or Ctrl+Shift+B). 请确保已在“常规”工具栏的“启动项”下拉列表中选择了 CMake 目标 。Make sure that a CMake target is already selected in the Startup Item dropdown in the General toolbar.

CMake 生成菜单命令CMake build menu command

生成结果按预期显示在“输出窗口”和“错误列表”中 。As you would expect, build results are shown in the Output Window and Error List.

CMake 生成错误CMake build errors

在包含多个生成目标的文件夹中,可以指定要生成的 CMake 目标:在“CMake”菜单或“CMakeLists.txt”上下文菜单上选择“生成”项,以指定目标 。In a folder with multiple build targets, you can specify which CMake target to build: Choose the Build item on the CMake menu or the CMakeLists.txt context menu to specify the target. 如果在 CMake 项目中按 Ctrl+Shift+B,则会生成当前活动文档。If you enter Ctrl+Shift+B in a CMake project, it builds the current active document.

调试 CMake 项目Debugging CMake projects

若要调试 CMake 项目,请选择首选配置并按 F5,或按工具栏中的“运行”按钮 。To debug a CMake project, choose the preferred configuration and press F5, or press the Run button in the toolbar. 如果“运行”按钮提示“选择启动项”,请选择下拉箭头。If the Run button says "Select Startup Item", select the dropdown arrow. 选择要运行的目标。Choose the target that you want to run. (在 CMake 项目中,“当前文档”选项只对 .cpp 文件有效。)(In a CMake project, the "Current document" option is only valid for .cpp files.)

CMake 运行按钮CMake run button

如果在上次生成后进行了更改,则“运行”或“F5”命令会先生成项目 。The Run or F5 commands first build the project if changes have been made since the previous build. 对 CMakeSettings.json 进行更改 会导致重新生成 CMake 缓存。Changes to CMakeSettings.json cause the CMake cache to be regenerated.

可通过设置 launch.vs.json 文件中的属性来自定义 CMake 调试会话。You can customize a CMake debugging session by setting properties in the launch.vs.json file. 有关更多信息,请参阅配置 CMake 调试会话For more information, see Configure CMake debugging sessions.

CMake 项目的“仅我的代码”Just My Code for CMake projects

使用 MSVC 编译器为 Windows 生成时,CMake 项目支持“仅我的代码”调试。When you build for Windows using the MSVC compiler, CMake projects have support for Just My Code debugging. 若要更改“仅我的代码”设置,请转到“工具” > “选项” > “调试” > “常规”。To change the Just My Code setting, go to Tools > Options > Debugging > General.

Vcpkg 集成Vcpkg integration

如果已安装 vcpkg,则在 Visual Studio 中打开的 CMake 项目将自动集成 vcpkg 工具链文件。If you have installed vcpkg, CMake projects opened in Visual Studio automatically integrate the vcpkg toolchain file. 这意味着,将 vcpkg 用于 CMake 项目不需要其他配置。That means no additional configuration is required to use vcpkg with your CMake projects. 此支持适用于本地 vcpkg 安装和所面向的远程系统上的 vcpkg 安装。This support works for both local vcpkg installations and vcpkg installations on remote systems that you're targeting. 在 CMake 设置配置中指定任何其他工具链时,此行为会自动禁用。This behavior is disabled automatically when you specify any other toolchain in your CMake Settings configuration.

自定义配置反馈Customize configuration feedback

默认情况下,除非出现错误,否则将禁止显示大多数配置消息。By default, most configuration messages are suppressed unless there's an error. 可以通过在“工具” > “选项” > “CMake”中启用此功能来查看所有消息。You can see all messages by enabling this feature in Tools > Options > CMake.

配置 CMake 诊断选项Configuring CMake diagnostic options

编辑 CMakeLists.txt 文件Editing CMakeLists.txt files

若要编辑 CMakeLists.txt 文件,请右键单击“解决方案资源管理器”中的文件,并选择“打开” 。To edit a CMakeLists.txt file, right-click on the file in Solution Explorer and choose Open. 如果对文件进行更改,会显示一个黄色的状态栏,通知用户 IntelliSense 将更新。If you make changes to the file, a yellow status bar appears and informs you that IntelliSense will update. 可以选择取消该更新操作。It gives you a chance to cancel the update operation. 有关 CMakeLists.txt 的详细信息,请参阅 CMake 文档For information about CMakeLists.txt, see the CMake documentation.

CMakeLists.txt 文件编辑CMakeLists.txt file editing

保存文件后,配置步骤立即自动再次运行,并在“输出”窗口中显示信息。As soon as you save the file, the configuration step automatically runs again and displays information in the Output window. 错误和警告显示在“错误列表”或“输出”窗口中 。Errors and warnings are shown in the Error List or Output window. 双击“错误列表”中的错误可导航到 CMakeLists.txt 中出现问题的行。Double-click on an error in the Error List to navigate to the offending line in CMakeLists.txt.

CMakeLists.txt 文件错误CMakeLists.txt file errors

CMake 配置步骤CMake configure step

如果对 CMakeSettings.json 或 CMakeLists.txt 文件进行了重大更改,Visual Studio 会自动重新运行 CMake 配置步骤 。When you make significant changes to the CMakeSettings.json or to CMakeLists.txt files, Visual Studio automatically reruns the CMake configure step. 如果配置步骤正确完成,则可在 C++ IntelliSense 和语言服务使用所收集的信息。If the configure step finishes without errors, the information that's collected is available in C++ IntelliSense and language services. 这些信息也可在生成和调试操作中使用。It's also used in build and debug operations.

排查 CMake 缓存错误Troubleshooting CMake cache errors

如果需要有关 CMake 缓存状态的详细信息来诊断问题,请打开“项目”主菜单或“解决方案资源管理器”中的“CMakeLists.txt”上下文菜单,运行下面的某个命令:If you need more information about the state of the CMake cache to diagnose a problem, open the Project main menu or the CMakeLists.txt context menu in Solution Explorer to run one of these commands:

  • 查看缓存:从编辑器中的生成根文件夹打开 CMakeCache.txt 文件。View Cache opens the CMakeCache.txt file from the build root folder in the editor. (如果清除缓存,则将擦除在此处对 CMakeCache.txt 进行的任何编辑。(Any edits you make here to CMakeCache.txt are wiped out if you clean the cache. 若要在清除缓存后依然保留更改,请参阅自定义 CMake 设置。)To make changes that persist after the cache is cleaned, see Customize CMake settings.)

  • 打开缓存文件夹:打开生成根文件夹的资源管理器窗口。Open Cache Folder opens an Explorer window to the build root folder.

  • 清理缓存:删除生成根文件夹,使下一个 CMake 配置步骤从清理缓存开始。Clean Cache deletes the build root folder so that the next CMake configure step starts from a clean cache.

  • 生成缓存:即使 Visual Studio 认为环境是最新的,也强制运行生成步骤。Generate Cache forces the generate step to run even if Visual Studio considers the environment up to date.

可在“工具”>“选项”>“CMake”>“常规”对话框中禁用自动缓存生成。Automatic cache generation can be disabled in the Tools > Options > CMake > General dialog.

从命令行运行 CMakeRun CMake from the command line

如果已从 Visual Studio 安装程序安装 CMake,可以通过执行以下步骤从命令行运行它:If you have installed CMake from the Visual Studio Installer, you can run it from the command line by following these steps:

  1. 运行适当的 vsdevcmd.bat (x86/x64)。Run the appropriate vsdevcmd.bat (x86/x64). 有关详细信息,请参阅在命令行上生成For more information, see Building on the Command Line.

  2. 切换到输出文件夹。Switch to your output folder.

  3. 运行 CMake 以生成/配置应用程序。Run CMake to build/configure your app.

Visual Studio 2017 具有对 CMake 的丰富支持,包括跨平台 CMake 项目Visual Studio 2017 has rich support for CMake, including cross-platform CMake projects. “Visual C++ Tools for CMake”组件使用“打开文件夹”功能,让 IDE 能够直接将 CMake 项目文件(例如 CMakeLists.txt)用于 IntelliSense 和浏览 。The Visual C++ Tools for CMake component uses the Open Folder feature to enable the IDE to consume CMake project files (such as CMakeLists.txt) directly for the purposes of IntelliSense and browsing. 支持 Ninja 和 Visual Studio 生成器。Both Ninja and Visual Studio generators are supported. 如果使用 Visual Studio 生成器,则它会生成临时项目文件,并将它传递给 msbuild.exe。If you use a Visual Studio generator, it generates a temporary project file and passes it to msbuild.exe. 但是,从不会加载项目来用于 IntelliSense 或浏览。However, the project is never loaded for IntelliSense or browsing purposes. 还可以导入现有 CMake 缓存。You also can import an existing CMake cache.

安装Installation

“Visual C++ Tools for CMake”作为“使用 C++ 的桌面开发”和“使用 C++ 的 Linux 开发”工作负载的一部分进行安装。Visual C++ Tools for CMake is installed as part of the Desktop development with C++ and Linux Development with C++ workloads.

已调用“适用于 C make 的 Visual C++ 工具”选项的“单个组件”选项卡的屏幕截图。

有关详细信息,请参阅在 Visual Studio 中安装 C++ Linux 工作负载For more information, see Install the C++ Linux workload in Visual Studio.

IDE 集成IDE integration

选择“文件”>“打开”>“文件夹”以打开一个包含 CMakeLists.txt 文件的文件夹时,将发生以下情况:When you choose File > Open > Folder to open a folder containing a CMakeLists.txt file, the following things happen:

  • Visual Studio 将“CMake”菜单项添加到主菜单,其中包含用于查看和编辑 CMake 脚本的命令。Visual Studio adds a CMake menu item to the main menu, with commands for viewing and editing CMake scripts.

  • 解决方案资源管理器显示文件夹结构和文件。Solution Explorer displays the folder structure and files.

  • Visual Studio 运行 CMake.exe 并根据需要生成默认配置(即 x86 Debug)的 CMake 缓存。Visual Studio runs CMake.exe and optionally generates the CMake cache for the default configuration, which is x86 Debug. 输出窗口中显示 CMake 命令行以及 CMake 的其他输出。The CMake command line is displayed in the Output Window, along with additional output from CMake.

  • 在后台,Visual Studio 开始对源文件编制索引,以启用 IntelliSense、浏览信息和重构等等。In the background, Visual Studio starts to index the source files to enable IntelliSense, browsing information, refactoring, and so on. 随着工作进行,Visual Studio 监视器在编辑器和磁盘中随之发生变化,以保持其索引与源同步。As you work, Visual Studio monitors changes in the editor and also on disk to keep its index in sync with the sources.

可以打开包含任意数量 CMake 项目的文件夹。You can open folders containing any number of CMake projects. Visual Studio 检测并配置工作区中的所有“根”CMakeLists.txt 文件。Visual Studio detects and configures all the "root" CMakeLists.txt files in your workspace. CMake 操作(配置、生成、调试)、C++ IntelliSense 和浏览可用于工作区中的所有 CMake 项目。CMake operations (configure, build, debug), C++ IntelliSense, and browsing are available to all CMake projects in your workspace.

包含多个根的 CMake 项目

还可按目标查看经过逻辑组织的项目。You can also view your projects organized logically by targets. 在“解决方案资源管理器”工具栏中,从下拉列表中选择“目标视图” :Choose Targets view from the dropdown in the Solution Explorer toolbar:

CMake“目标视图”按钮

Visual Studio 使用名为 CMakeSettings.json 的文件存储 Cmake.exe 的环境变量或命令行选项。Visual Studio uses a file called CMakeSettings.json to store environment variables or command-line options for Cmake.exe. CMakeSettings.json 还使你可以定义和存储多个 CMake 生成配置。CMakeSettings.json also enables you to define and store multiple CMake build configurations. 可以在 IDE 中方便地切换它们。You can conveniently switch between them in the IDE.

否则,请使用 CMakeLists.txt 来指定源文件、查找库、设置编译器和链接器选项(就像在任何 CMake 项目中进行操作一样),以及指定其他与生成系统相关的信息。Otherwise, use the CMakeLists.txt just as you would in any CMake project to specify source files, find libraries, set compiler and linker options, and specify other build system-related information.

如果需要在调试时将参数传递给可执行文件,则可以使用另一个名为 launch.vs.json 的文件。If you need to pass arguments to an executable at debug time, you can use another file called launch.vs.json. 在某些情况中,Visual Studio 会自动生成这些文件。In some scenarios, Visual Studio automatically generates these files. 可以手动编辑它们,甚至可以自己创建文件。You can edit them manually, or even create the file yourself.

备注

对于其他类型的“打开文件夹”项目,将使用另外两个 JSON 文件:CppProperties.json 和 tasks.vs.json。For other kinds of Open Folder projects, two additional JSON files are used: CppProperties.json and tasks.vs.json. 这些都与 CMake 项目无关。Neither of these are relevant for CMake projects.

导入现有缓存Import an existing cache

导入现有 CMakeCache.txt 文件时,Visual Studio 自动提取自定义的变量,并基于这些变量创建一个预填充的 CMakeSettings.json 文件 。When you import an existing CMakeCache.txt file, Visual Studio automatically extracts customized variables and creates a pre-populated CMakeSettings.json file based on them. 不会以任何方式修改原始缓存。The original cache isn't modified in any way. 仍可从命令行或者借助用于生成原始缓存的任何工具或 IDE 使用该原始缓存。It can still be used from the command line, or with whatever tool or IDE used to generate it. 新的 CMakeSettings.json 文件与项目的根 CMakeLists.txt 放在一起 。The new CMakeSettings.json file is placed alongside the project's root CMakeLists.txt. Visual Studio 基于设置文件生成新的缓存。Visual Studio generates a new cache based the settings file. 可在“工具”>“选项”>“CMake”>“常规”对话框中替代自动缓存生成。You can override automatic cache generation in the Tools > Options > CMake > General dialog.

并非缓存中的所有内容都会被导入。Not everything in the cache is imported. 生成器和编译器的位置等属性替换为已知适合用于 IDE 的默认值。Properties such as the generator and the location of the compilers are replaced with defaults that are known to work well with the IDE.

导入现有缓存To import an existing cache

  1. 从主菜单中选择“文件”>“打开”>“CMake”:From the main menu, choose File > Open > CMake:

    打开 CMakeOpen CMake

    此命令会显示“从缓存导入 CMake”向导。This command brings up the Import CMake from Cache wizard.

  2. 导航到要导入的 CMakeCache.txt 文件,然后单击“确定”。Navigate to the CMakeCache.txt file that you want to import, and then click OK. “从缓存导入 CMake 项目”向导随即显示:The Import CMake Project from Cache wizard appears:

    导入 CMake 缓存Import a CMake cache

    在向导完成时,可在“解决方案资源管理器”中看到新的 CMakeCache.txt 文件,位于项目中的根 CMakeLists.txt 文件旁边。When the wizard completes, you can see the new CMakeCache.txt file in Solution Explorer next to the root CMakeLists.txt file in your project.

生成 CMake 项目Building CMake projects

要生成 CMake 项目,可选择执行以下操作:To build a CMake project, you have these choices:

  1. 在“常规”工具栏中,找到“配置”下拉菜单。In the General toolbar, find the Configurations dropdown. 默认情况下,它可能显示“Linux-Debug”或“x64-Debug”。It's probably showing "Linux-Debug" or "x64-Debug" by default. 选择首选配置,然后按 F5 或单击工具栏上的“运行”(绿色三角形)按钮 。Select the preferred configuration and press F5, or click the Run (green triangle) button on the toolbar. 项目首先自动生成,就像 Visual Studio 解决方案一样。The project automatically builds first, just like a Visual Studio solution.

  2. 右键单击 CMakeLists.txt,并从上下文菜单中选择“生成”。Right click on the CMakeLists.txt and select Build from the context menu. 如果在文件夹结构中有多个目标,可以选择生成所有目标或仅生成某个特定目标。If you have multiple targets in your folder structure, you can choose to build all or only one specific target.

  3. 从主菜单中选择“生成”>“生成解决方案”(F7 或 Ctrl+Shift+B) 。From the main menu, select Build > Build Solution (F7 or Ctrl+Shift+B). 请确保已在“常规”工具栏的“启动项”下拉列表中选择了 CMake 目标 。Make sure that a CMake target is already selected in the Startup Item dropdown in the General toolbar.

CMake 生成菜单命令CMake build menu command

可以在 CMakeSettings.json 文件中自定义生成配置、环境变量、命令行参数和其他设置。You can customize build configurations, environment variables, command-line arguments, and other settings in the CMakeSettings.json file. 它使你可以进行更改,而无需修改 CMakeLists.txt 文件。It lets you make changes without modifying the CMakeLists.txt file. 有关详细信息,请参阅自定义 CMake 设置For more information, see Customize CMake settings.

生成结果按预期显示在“输出窗口”和“错误列表”中 。As you would expect, build results are shown in the Output Window and Error List.

CMake 生成错误CMake build errors

在包含多个生成目标的文件夹中,可以指定要生成的 CMake 目标:在“CMake”菜单或“CMakeLists.txt”上下文菜单上选择“生成”项,以指定目标 。In a folder with multiple build targets, you can specify which CMake target to build: Choose the Build item on the CMake menu or the CMakeLists.txt context menu to specify the target. 如果在 CMake 项目中按 Ctrl+Shift+B,则会生成当前活动文档。If you enter Ctrl+Shift+B in a CMake project, it builds the current active document.

调试 CMake 项目Debugging CMake projects

若要调试 CMake 项目,请选择首选配置并按 F5。To debug a CMake project, choose the preferred configuration and press F5. 或按工具栏中的“运行”按钮。Or, press the Run button in the toolbar. 如果“运行”按钮提示“选择启动项”,请选择向下箭头并选择要运行的目标。If the Run button says "Select Startup Item", select the dropdown arrow and choose the target that you want to run. (在 CMake 项目中,“当前文档”选项只对 .cpp 文件有效。)(In a CMake project, the "Current document" option is only valid for .cpp files.)

CMake 运行按钮CMake run button

如果在上次生成后进行了更改,则“运行”或“F5”命令会先生成项目 。The Run or F5 commands first build the project if changes have been made since the previous build.

可通过设置 launch.vs.json 文件中的属性来自定义 CMake 调试会话。You can customize a CMake debugging session by setting properties in the launch.vs.json file. 有关更多信息,请参阅配置 CMake 调试会话For more information, see Configure CMake debugging sessions.

编辑 CMakeLists.txt 文件Editing CMakeLists.txt files

若要编辑 CMakeLists.txt 文件,请右键单击“解决方案资源管理器”中的文件,并选择“打开” 。To edit a CMakeLists.txt file, right-click on the file in Solution Explorer and choose Open. 如果对文件进行更改,会显示一个黄色的状态栏,通知用户 IntelliSense 将更新。If you make changes to the file, a yellow status bar appears and informs you that IntelliSense will update. 可以选择取消该更新操作。It gives you a chance to cancel the update operation. 有关 CMakeLists.txt 的详细信息,请参阅 CMake 文档For information about CMakeLists.txt, see the CMake documentation.

CMakeLists.txt 文件编辑CMakeLists.txt file editing

保存文件后,配置步骤立即自动再次运行,并在“输出”窗口中显示信息。As soon as you save the file, the configuration step automatically runs again and displays information in the Output window. 错误和警告显示在“错误列表”或“输出”窗口中 。Errors and warnings are shown in the Error List or Output window. 双击“错误列表”中的错误可导航到 CMakeLists.txt 中出现问题的行。Double-click on an error in the Error List to navigate to the offending line in CMakeLists.txt.

CMakeLists.txt 文件错误CMakeLists.txt file errors

CMake 配置步骤CMake configure step

如果对 CMakeSettings.json 或 CMakeLists.txt 文件进行了重大更改,Visual Studio 会自动重新运行 CMake 配置步骤 。When significant changes are made to the CMakeSettings.json or to CMakeLists.txt files, Visual Studio automatically reruns the CMake configure step. 如果配置步骤正确完成,则可在 C++ IntelliSense 和语言服务使用所收集的信息。If the configure step finishes without errors, the information that's collected is available in C++ IntelliSense and language services. 这些信息也可在生成和调试操作中使用。It's also used in build and debug operations.

多个 CMake 项目可以使用相同的 CMake 配置名称(例如,x86-Debug)。Multiple CMake projects may use the same CMake configuration name (for example, x86-Debug). 在选择该配置时会配置并生成所有这些项目(在其自己的生成根文件夹中)。All of them are configured and built (in their own build root folder) when that configuration is selected. 可以调试参与该 CMake 配置的所有 CMake 项目中的目标。You can debug the targets from all of the CMake projects that participate in that CMake configuration.

CMake“仅生成”菜单项CMake Build Only menu item

可以将生成和调试会话限制为工作区中的项目子集。You can limit builds and debug sessions to a subset of the projects in the workspace. 在 CMakeSettings.json 文件中创建具有唯一名称的新配置。Create a new configuration with a unique name in the CMakeSettings.json file. 然后,将该配置仅应用于这些项目。Then, apply the configuration to those projects only. 如果选择了该配置,则 IntelliSense 以及生成和调试命令仅应用于这些指定项目。When that configuration is selected, IntelliSense and the build and debug commands only apply to those specified projects.

排查 CMake 缓存错误Troubleshooting CMake cache errors

如果需要有关 CMake 缓存状态的详细信息来诊断问题,请打开“CMake”主菜单或“解决方案资源管理器”中的“CMakeLists.txt”上下文菜单,运行下面的某个命令:If you need more information about the state of the CMake cache to diagnose a problem, open the CMake main menu or the CMakeLists.txt context menu in Solution Explorer to run one of these commands:

  • 查看缓存:从编辑器中的生成根文件夹打开 CMakeCache.txt 文件。View Cache opens the CMakeCache.txt file from the build root folder in the editor. (如果清除缓存,则将擦除在此处对 CMakeCache.txt 进行的任何编辑。(Any edits you make here to CMakeCache.txt are wiped out if you clean the cache. 若要在清除缓存后依然保留更改,请参阅自定义 CMake 设置。)To make changes that persist after the cache is cleaned, see Customize CMake settings.)

  • 打开缓存文件夹:打开生成根文件夹的资源管理器窗口。Open Cache Folder opens an Explorer window to the build root folder.

  • 清理缓存:删除生成根文件夹,使下一个 CMake 配置步骤从清理缓存开始。Clean Cache deletes the build root folder so that the next CMake configure step starts from a clean cache.

  • 生成缓存:即使 Visual Studio 认为环境是最新的,也强制运行生成步骤。Generate Cache forces the generate step to run even if Visual Studio considers the environment up to date.

可在“工具”>“选项”>“CMake”>“常规”对话框中禁用自动缓存生成。Automatic cache generation can be disabled in the Tools > Options > CMake > General dialog.

单个文件编译Single file compilation

若要在 CMake 项目中编译单个文件,可在“解决方案资源管理器”中右键单击该文件。To build a single file in a CMake project, right-click on the file in Solution Explorer. 从弹出菜单中选择“编译”。Choose Compile from the pop-up menu. 通过使用 CMake 主菜单,还可生成当前在编辑器中打开的文件:You can also build the currently open file in the editor by using the main CMake menu:

CMake 单个文件编译

从命令行运行 CMakeRun CMake from the command line

如果已从 Visual Studio 安装程序安装 CMake,可以通过执行以下步骤从命令行运行它:If you have installed CMake from the Visual Studio Installer, you can run it from the command line by following these steps:

  1. 运行适当的 vsdevcmd.bat (x86/x64)。Run the appropriate vsdevcmd.bat (x86/x64). 有关详细信息,请参阅在命令行上生成For more information, see Building on the command line .

  2. 切换到输出文件夹。Switch to your output folder.

  3. 运行 CMake 以生成/配置应用程序。Run CMake to build/configure your app.

在 Visual Studio 2015 中,Visual Studio 用户可使用 CMake 生成器生成 MSBuild 项目文件,IDE 之后可将该项目文件用于 IntelliSense、浏览和编译。In Visual Studio 2015, Visual Studio users can use a CMake generator to generate MSBuild project files, which the IDE then consumes for IntelliSense, browsing, and compilation.

请参阅See also

教程:在 Visual Studio 中创建 C++ 跨平台项目Tutorial: Create C++ cross-platform projects in Visual Studio
配置 Linux CMake 项目Configure a Linux CMake project
连接到远程 Linux 计算机Connect to your remote Linux computer
自定义 CMake 生成设置Customize CMake build settings
CMakeSettings.json 架构参考CMakeSettings.json schema reference
配置 CMake 调试会话Configure CMake debugging sessions
部署、运行和调试 Linux 项目Deploy, run, and debug your Linux project
CMake 预定义配置引用CMake predefined configuration reference