How to Auto Generate CMakeList file and Build C++ project in VS Code?

Introduction

 VS Code is a lightweight fast and easy to use editor for developing software in any language. Traditionally most of us use Visual Studio for our development purposes especially if we are developing in C++. One of the Key advantage while using VS Code to develop applications in C++ is that developer can solely focus on creating source file and these can be bundled in a project or solution file using CMake with any compiler and build configuration.

Here we are going to see step by step how to Auto generate CMakelist file in VS Code which specifies the source files that needs to be bundled together and the build configuration etc.

Installing CMake Tool Extension

Before we get started we need to install extension for CMake Tools in VS Code. I Have installed extension from Microsoft for this purpose as shown in the figure below:

 

CMake Extension for VS Code

  • Create a sample C++ file with some code as shown in the figure below in the folder which is loaded in VS Code

Sample C++ code for CMake

  • Next step is to use Command Pallet in VS Code(Ctrl + Shift + P) to auto generate CMakeList.txt file. After invoking the Command Pallet navigate to the option CMake : Quick Start as shown in the figure below:

CMake tools to Auto Generate CMakelist file

  • After pressing enter in this option it will prompt you to select the Compiler and the Build configuration that needs to be used for the project as shown below:

CMake Build and Compiler settings

  • Once the suitable compiler is selected it will prompt for the name of the project as shown in the figure below. Enter a suitable name for the project and solution file.

CMake project name

  • Once its entered it will ask for the type of output that needs to be generated when the project file is build, either the Library or the exe as shown in the figure below:

CMake project output type

  • Once a suitable type is selected CMakeList.txt file is generated with the appropriate content and build directory is created. Once Save on the CMakeList.txt file is triggered it will generate the project and solution file and place it in the build folder as shown below:

Generated files in CMake
CMakeList.txt file content

Comments

Popular posts from this blog

Creating RESTful Minimal WebAPI in .Net 6 in an Easy Manner! | FastEndpoints

Mastering Concurrency with Latches and Barriers in C++20: A Practical Guide for Students

Graph Visualization using MSAGL with Examples