Adding updates to the CMake: Quick Start to the existing docs (#7309)

This commit is contained in:
Sinem Akinci 2024-05-28 15:37:59 -07:00 коммит произвёл GitHub
Родитель 2dc62837c1
Коммит 8bd48df968
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
21 изменённых файлов: 135 добавлений и 42 удалений

Просмотреть файл

@ -78,57 +78,50 @@ sudo apt-get install build-essential gdb
## Create a CMake project ## Create a CMake project
If you have an existing CMake project that already has a `CMakeLists.txt` file in the root directory, you can skip to [Select a kit](#select-a-kit) to configure your existing project. If you do not have an existing CMake project, follow the steps in [Create a CMake project](cmake-quickstart.md).
Otherwise, create a folder for a new project. From the Terminal window, create an empty folder called `cmakeQuickStart`, navigate into it, and open VS Code in that folder by entering the following commands: If you already have an existing CMake project that has a `CMakeLists.txt` file in the root directory, continue to [Configure Hello World](#configure-hello-world) to configure your project.
```bash ### Configure Hello World
mkdir cmakeQuickStart
cd cmakeQuickStart
code .
```
The `code .` command opens VS Code in the current working folder, which becomes your "workspace". Before you can use the CMake Tools extension to build a project, you need to configure it to know about the compilers on your system. There are two ways to configure CMake in VS Code:
### Create a CMake hello world project - Use CMake Presets (recommended)
- Use CMake Kits/Variants
The CMake Tools extension can create the files for a basic CMake project for you. Open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Quick Start** command: #### Configure using CMake Presets
We recommend using CMake Presets for managing your CMake configurations. CMake Presets enable you to specify a common JSON file, where you store all the configurations for your project. You can then share this file with others, across different IDEs, and across different operating systems.
![Create CMake quickstart](images/cpp/cmake-quickstart-command-palette.png) If you created a project by following the step in [Create a CMake project](cmake-quickstart), your project is configured to use CMake Presets.
Enter a project name. This will be written to `CMakeLists.txt` and a few initial source files. If your project has a `CMakePresets.json` file, you can use the Configure and Build presets for specifying how to build your project on your machine.
Next, select **Executable** as the project type to create a basic source file (`main.cpp`) that includes a basic `main()` function. You can view the active configuration of presets in the Project Status in the CMake Tools view under the **Configure** and **Build** node. You can select these nodes at any time to set or change your Configure and Build presets.
![Choose project type](images/cpp/cmake-choose-type.png) ![CMake Side Panel with presets](images/cpp/cmake-side-panel-presets.png)
**Note:** If you had wanted to create a basic source and header file, you would have selected **Library** instead. But for this tutorial, **Executable** will do. If you are prompted to configure IntelliSense for the folder, select **Allow**. You can also set any of your presets by running the **CMake: Select Configure Preset** or **CMake: Select Build Preset** commands in the Command Palette (`kb(workbench.action.showCommands)`).
This creates a hello world CMake project containing `main.cpp`, `CMakeLists.txt` (which tells the CMake tools how to build your project), and a folder named `build` for your build files: ![CMake Select presets](images/cpp/select-cmake-preset.png)
![Project contents](images/cpp/cmake-project-contents.png) #### Configure using CMake Kits
If your project does not have a `CMakePresets.json` file, you need to use kits. A kit represents a toolchain, which is the compiler, linker, and other tools used to build your project.
### Select a kit To scan for kits:
Before you can use the CMake Tools extension to build a project, you need to configure it to know about the compilers on your system. Do that by scanning for 'kits'. A kit represents a toolchain, which is the compiler, linker, and other tools used to build your project. To scan for kits: 1. Open the Command Palette (`kb(workbench.action.showCommands)`) and run **CMake: Select a Kit**. The extension automatically scans for kits on your computer and creates a list of compilers found on your system.
1. Open the Command Palette (`kb(workbench.action.showCommands)`) and run **CMake: Select a Kit**. The extension will automatically scan for kits on your computer and create a list of compilers found on your system.
1. Select the compiler you want to use. For example, depending on the compilers you have installed, you might see something like: 1. Select the compiler you want to use. For example, depending on the compilers you have installed, you might see something like:
![Select the kit](images/cpp/cmake-selectkit.png) ![Select the kit](images/cpp/cmake-selectkit.png)
## Configure Hello World The kit you selected previously is now shown in the **Project Status** section in the CMake Tools view.
There are two things you must do to configure your CMake project: select a kit (which you just did) and select a variant. ![Selected kit in Sidebar](images/cpp/cmake-kit.png)
The kit you selected previously is shown in the **Project Status** view in the CMake Tools sidebar. For example: To change the kit, you can select the kit in the **Project Status** section in the CMake Tools view, or run the **CMake: Select a Kit** command again from the Command Palette. If you don't see the compiler you're looking for, you can edit the `cmake-tools-kits.json` file in your project. To edit the file, open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Edit User-Local CMake Kits** command.
![Selected kit in Sidebar](images/cpp/cmake-kit.png) Then, you'll need to select a variant.
To change the kit, you can click on the kit in the **Project Status** view in the CMake Tools sidebar, or run the **CMake: Select a Kit** command again from the Command Palette. If you don't see the compiler you're looking for, you can edit the `cmake-tools-kits.json` file in your project. To edit the file, open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Edit User-Local CMake Kits** command.
### Select a variant
A variant contains instructions for how to build your project. By default, the CMake Tools extension provides four variants, each corresponding to a default build type: `Debug`, `Release`, `MinRelSize`, and `RelWithDebInfo`. These options do the following: A variant contains instructions for how to build your project. By default, the CMake Tools extension provides four variants, each corresponding to a default build type: `Debug`, `Release`, `MinRelSize`, and `RelWithDebInfo`. These options do the following:
@ -149,7 +142,7 @@ The selected variant will appear in the Status bar next to the active kit.
### CMake: Configure ### CMake: Configure
Now that you've selected a kit and a variant, open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Configure** command to configure your project. This generates build files in the project's build folder using the kit and variant you selected. Now that you've selected your configuration settings via presets or kits/variants, open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Configure** command to configure your project. This generates build files in the project's build folder using the configurations you selected.
## Build hello world ## Build hello world

Просмотреть файл

@ -0,0 +1,73 @@
---
Order: 9
Area: cpp
TOCTitle: CMake Quick Start
ContentId: 55b5d15c-a020-4808-941f-e0255751a5f7
PageTitle: Create a CMake project with the CMake Quick Start
DateApproved: 5/29/2024
MetaDescription: Create a Hello World project by using the CMake Quick Start in the CMake Tools Visual Studio Code extension
---
# Create a CMake hello world project with CMake Quick Start
In this article, you'll learn how to create a CMake hello world project from scratch using the CMake Tools extension in VS Code.
If you have an existing CMake project that already has a `CMakeLists.txt` file in the root directory but no CMake presets, you can skip to [Create a `CMakePresets.json` file](#Create-a-CMakePresets.json-file) to configure your project with CMake presets.
Otherwise, create a folder for a new project. From the Terminal window, create an empty folder called `HelloWorld`, navigate into it, and open VS Code in that folder by entering the following commands:
```bash
mkdir helloworld
cd helloworld
code .
```
The `code .` command opens VS Code in the current working folder, which becomes your "workspace".
## Create a CMakeLists.txt file
The CMake Tools extension can create the files for a basic CMake project for you.
1. Open the Command Palette (`kb(workbench.action.showCommands)`) and run the **CMake: Quick Start** command:
![Create CMake quickstart](images/cpp/cmake-quickstart-command-palette.png)
1. Enter a project name, and select and **C++** as the project language.
This information will be written to `CMakeLists.txt` and a few initial source files.
> **Note:** If you had other source code files in this folder that you wanted to add as targets to the `CmakeLists.txt`, an option to add these would now be given. But for this tutorial, we will stick with just the hello world file.
1. Select **CTest** as an additional option to add support for testing. You can also select **CPack** for CPack support.
![Additional Options](images/cpp/cmake-quickstart-options.png)
1. Next, select **Executable** as the project type to create a basic source file (`main.cpp`) that includes a basic `main()` function.
![Choose project type](images/cpp/cmake-choose-type.png)
> **Note:** If you had wanted to create a basic source and header file, you would have selected **Library** instead. But for this tutorial, **Executable** will do. If you are prompted to configure IntelliSense for the folder, select **Allow**.
This successfully creates the `CMakeLists.txt` file, which tells the CMake tools how to build your project.
![Project contents](images/cpp/cmake-quickstart-cmakelists.png)
## Create a CMakePresets.json file
Next, continue with the CMake Quick Start to create a `CMakePresets.json` file.
1. Select **Add a New Preset** and **Create from Compilers**.
The extension automatically scans for kits on your computer and creates a list of compilers found on your system.
1. Select the compiler you want to use.
For example, depending on the compilers you have installed, you might see something like this:
![Add a new preset](images/cpp/cmake-quickstart-selectkit.png)
1. Enter a name for this new preset.
The name for the preset will be written to `CMakePresets.json`.
After completing these steps, you should now have a complete hello world CMake project that contains the following files: `main.cpp`, `CMakeLists.txt`, and `CMakePresets.json`.
![Add a new preset](images/cpp/cmake-quickstart-projcontents.png)

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 15 Order: 16
Area: cpp Area: cpp
TOCTitle: Configure IntelliSense for cross-compiling TOCTitle: Configure IntelliSense for cross-compiling
ContentId: 381b7ce1-5766-49b0-ad26-f9eedae70e63 ContentId: 381b7ce1-5766-49b0-ad26-f9eedae70e63

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 14 Order: 15
Area: cpp Area: cpp
TOCTitle: Configure IntelliSense TOCTitle: Configure IntelliSense
ContentId: bf494c65-12b4-4506-ab6c-1fad76d7ccf1 ContentId: bf494c65-12b4-4506-ab6c-1fad76d7ccf1

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 10 Order: 11
Area: cpp Area: cpp
TOCTitle: Debugging TOCTitle: Debugging
ContentId: 9150091A-6F3A-46B9-881B-B8FD788FA705 ContentId: 9150091A-6F3A-46B9-881B-B8FD788FA705

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 9 Order: 10
Area: cpp Area: cpp
TOCTitle: Editing and Navigating TOCTitle: Editing and Navigating
ContentId: 61D63E54-67E2-4743-B5CB-C6E7F582982A ContentId: 61D63E54-67E2-4743-B5CB-C6E7F582982A

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 11 Order: 12
Area: cpp Area: cpp
TOCTitle: Refactoring TOCTitle: Refactoring
ContentId: 5b5da770-b59f-4eca-94a3-78e824d16b52 ContentId: 5b5da770-b59f-4eca-94a3-78e824d16b52

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 13 Order: 14
Area: cpp Area: cpp
TOCTitle: Settings TOCTitle: Settings
ContentId: 4E34F6AF-BFC6-4BBB-8464-2E50C85AE826 ContentId: 4E34F6AF-BFC6-4BBB-8464-2E50C85AE826

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 15 Order: 16
Area: cpp Area: cpp
TOCTitle: FAQ TOCTitle: FAQ
ContentId: 652c9cec-b8fa-4597-a894-f2ea9a095c31 ContentId: 652c9cec-b8fa-4597-a894-f2ea9a095c31

Просмотреть файл

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:7ca0515e91b1b2293d822cfbed099598e591543e4ad4db76463d6a10ace97c47 oid sha256:9f4dfd100c080619ac34cc76418a309c0be242ad502e606bc652de7f906ae79d
size 14418 size 4524

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ec10f1699b40dd8e5796525eb900d724459a3e88dbbaac4fb278b71a096d515d
size 18999

Просмотреть файл

@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1 version https://git-lfs.github.com/spec/v1
oid sha256:c1dc23df090235fd41b913aea9e7d28b245657dc43dfd4f0eaac4966b7de670e oid sha256:f5e40bd294a71f62546d5687b1cee0dd5f5f12375cf6ab7c8abfe8bf3d15f622
size 2324 size 4698

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1b2b4886f7e57c71022344250d77f7cb2e4eef713ae46bf4c03022b0b08c1458
size 3909

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c6b13b4ac4897c18d77e5f4a1577061c885868bf4f1969fcc981b94151b6ff98
size 4694

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8b3a8dec6b6c26a903d25b2acfbc4acb719efa3b9271f8f067294507199cda9f
size 10528

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:885a7b8930cf1e1362e7776a841a60e339067593a5a46b4c547573711d0d460a
size 6640

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1e95e5f4b7b1d3888d76f2b2fbb65fffd4238d14a977bc689fbed0339689b851
size 52016

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c742faa45e102745d9f198c63901d3d8e9aa9f36f6a6e91a8c5bb0bc896e2068
size 27778

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ff6863e4cd263e8d074832740c99ae0c351a7a8f601d4e9887bec89ec2c28d28
size 18843

Просмотреть файл

@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:85a0721e7300a3d5c8a421498ba1f7c7d8eef8b861652fb78fc4c5e9085e1666
size 15369

Просмотреть файл

@ -1,5 +1,5 @@
--- ---
Order: 12 Order: 13
Area: cpp Area: cpp
TOCTitle: Configure debugging TOCTitle: Configure debugging
ContentId: 8cb0c932-d5f2-41e7-b297-5fd100ce4e0c ContentId: 8cb0c932-d5f2-41e7-b297-5fd100ce4e0c