Movatterモバイル変換


[0]ホーム

URL:


Tryagent mode in VS Code!

Dismiss this update

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 aCMakeLists.txt file in the root directory but no CMake presets, you can skip toCreate aCMakePresets.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 calledHelloWorld, navigate into it, and open VS Code in that folder by entering the following commands:

mkdir helloworldcd helloworldcode .

Thecode . 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 (⇧⌘P (Windows, LinuxCtrl+Shift+P)) and run theCMake: Quick Start command:

    Create CMake quickstart

  2. Enter a project name, and selectC++ as the project language.

    This information will be written toCMakeLists.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 theCmakeLists.txt, an option to add these would now be given. But for this tutorial, we will stick with just the hello world file.

  3. SelectCTest as an additional option to add support for testing. You can also selectCPack for CPack support.

    Additional Options

  4. Next, selectExecutable as the project type to create a basic source file (main.cpp) that includes a basicmain() function.

    Choose project type

    Note: If you had wanted to create a basic source and header file, you would have selectedLibrary instead. But for this tutorial,Executable will do. If you are prompted to configure IntelliSense for the folder, selectAllow.

This successfully creates theCMakeLists.txt file, which tells the CMake tools how to build your project.

Project contents

Create a CMakePresets.json file

Next, continue with the CMake Quick Start to create aCMakePresets.json file.

  1. SelectAdd a New Preset andCreate from Compilers.

    The extension automatically scans for kits on your computer and creates a list of compilers found on your system.

  2. 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

  3. Enter a name for this new preset.

    The name for the preset will be written toCMakePresets.json.

After completing these steps, you should now have a complete hello world CMake project that contains the following files:main.cpp,CMakeLists.txt, andCMakePresets.json.

Add a new preset

5/29/2024

[8]ページ先頭

©2009-2025 Movatter.jp