Visual Studio Code Cmake

  



The full path to the CMake program executable, including the file name and extension. It allows you to use a custom version of CMake with Visual Studio. For remote builds, specify the CMake location on the remote machine. For configurations such as Linux that use. CMake Tools provides the native developer a full-featured, convenient, and powerful workflow for CMake-based projects in Visual Studio Code. Important doc links. CMake Tools quick start; Configure and build a project with CMake Presets; Configure a project with kits and variants; Build a project with kits and variants; Debug a project.

I am used to writing C + + in Linux environment. Sometimes I switch to windows and want to continue working on the same project. It is always troublesome to reconfigure the environment. Although using visual studio to write c + + in windows only needs to double-click an icon, I still want to toss about the vs Code environment configuration. There are two main reasons: one is that various languages are written in vs code, so it is convenient to develop the same project on different platforms by using git synchronization code; the other is that graphical interface cannot be used in some cases, for example, visual studio, a graphical IDE, cannot be used to test windows environment when CI (continuous integration) is configured for GIT.

The environment and tool versions covered in this article are:

  • Windows 10
  • VS Code 1.45.0
  • C/C++(ms- vscode.cpptools )Plug in 0.28.0.insider3
  • CMake( twxs.cmake )Plug in 0.0.17
  • CMake Tools(ms- vscode.cmake -Tools) plug in 1.3.1
  • Visual Studio IntelliCode( visualstudioexptteam.vscodeintellicode )Plug in 1.2.7
  • Visual studio community 2019 (need to call the MSVC compiler provided by vs, as well as the corresponding header file and library file)
  • CMake 3.17.2
  • Ninja 1.10.0
  • Boost 1.73.0

primary coverage

1 create a C + + Project

2 install Visual Studio

3 install cmake and ninja

4 download and compile boost

4.1 use of command prompt

4.2 compile boost

5 command line compilation and testing

6 configuration vs Code

6.1 settings.json

6.2 c_cpp_properties.json

6.3 tasks.json

6.4 launch.json

6.5 CMakeLists.txt

6.6 compilation, testing and debugging

Create a C + + Project

Visual Studio Code Cmake Windows

The installation process of vscode and plug-ins will not be introduced in this paper, but the file structure and code of the project will be given directly.

The project structure is as follows..vscodeThe three JSON files in the folder are used to configure vs code. The second folder contains the answer to a question about leetcode(solution.hppandsolution.cpp ), solution_test.cppUsed to perform unit tests. BottomCMakeLists.txtThe file is used to configure cmake and give the Compilation Rules of the project.

The C + + part of the code is given here, and the contents of other files will be given later.

solution.hpp

solution.cpp

solution_test.cpp

Install Visual Studio

The installation process of VS is not described here, but the components that need to be installed are just prompted.

It should be noted that the visual studio community 2019 preview version cannot be correctly recognized when compiling boost, and the official version needs to be installed. I have tried both versions of visual studio community 2017 / 2019. Take the 2019 version as an example.

Just install the “desktop development using C + +” set of components.

Installing cmake and Ninja

Cmake can be installed by downloading an installation package named cmake-3.17.2-win64-x64.msi. After Ninja is downloaded, there is only one executable file, which can be placed in a directory at will.

The installation process will not be detailed for the moment, just pay attention to setting the environment variables after the installation.

After setting the environment variables, you can reopen the command-line tool or terminal and check the versions of cmake and Ninja to see if the settings are successful.

Download and compile boost

Boost can be downloaded from this link: https://dl.bintray.com/boostorg/release/1.73.0/source/ And then unzip it to a directory.

Boost itself is header only, that is, in most cases, it can be called directly by including its header file. But for the convenience of linking our own program to the unit test module of boost( Boost.Test )Here you need to compile boost to generate static library files.

Code

Use of command prompt

Since we have installed visual studio and the compiler tools and dependent libraries required for compiling C + + on Windows platform, we can directly use the environment provided by vs to compile boost.

Several command line tools can be found in the “Visual Studio 2019” directory of the start menu. We can open a command-line tool named “x64 native tools command prompt for vs 2019”. This icon corresponds to theC:Program Files (x86)Microsoft Visual Studio2019CommunityVCAuxiliaryBuildvcvars64.batThis script. The function of this script is to add the include path and library path of MSVC and windows SDK to environment variables, and then open a CMD command line. Therefore, all the dependencies needed to compile c + + can be detected directly during the running of this CMD.

We can try to type in this CMDSETTo view all the environment variables that are in effect.

We can also use normal information in PowerShell or C + +. The specific process will be introduced later.

The font of the default CMD is a little ugly. I’m used to opening a CMD terminal in Windows terminal, and then execute the following command:

> “C:Program Files (x86)Microsoft Visual Studio2019CommunityVCAuxiliaryBuildvcvars64.bat”

This allows our new terminals to detect the MSVC environment, as shown in the figure below.

Visual Studio Code Cmake

Compile boost

then,cdGo to the root directory of boost and execute the following command:

After waiting for the compilation to complete, thebuildlibThere will be a lot of them in the catalog.libDocuments. We’ll only use themlibboost_unit_test_framework-vc142-mt-gd-x64-1_73.libThis is a document.

Of course, if you only want to compile the unit test module, you can use the following command:

Command line compilation and testing

Here we first compile the C + + project on the command line and run the unit tests.cdGo to the project directory and execute the following command:

On Windows platform, the generation tool can choose nmake provided by vs or ninja. Microsoft nmake is similar to the make tool of Linux platform. According to this video, ninja compiles faster than nmake.

Visual Studio Code Cmake

As you can see, in thevcvars64.batIn the provided environment, cmake and Ninja installed by vs are used, and the version number is older than that of our own installation. Next, we describe how to configure the C + + compilation and test environment in vs code.

Configure vs Code

settings.json

Open the vs code settings, and clicksettings.jsonAdd the following lines to thevcvars64.batThe role of:

c_cpp_properties.json

The configuration of Linux and windows is given here.

tasks.json

The first twotaskThe first is to clear the build directory, the second is to configure cmake, and the third is to configure cmaketaskCmake is configured under windows.

launch.json

The first is on LinuxgdbDebugging, the second is under LinuxlldbDebugging, the third is in windows with MSVCcl.exeDebugging.

CMakeLists.txt

The cmake script is also cross platform, automatically identifies Linux or windows, and then executes the corresponding links.

Compile, test, and debug

Press the shortcut keyCtrl + Shift + PThen you can enter the different commands we defined earlier:

  • “Cmake: configure” – configure cmake
  • “Cmake: build” – compile project
  • “Cmake: run tests” – execute tests
  • “Tasks: run task → MSVC configure” – configure cmake by calling task

The effect of unit test is shown in the following figure:

Visual Studio Code Cmake Syntax Highlighting

The debugging effect is shown in the following figure:

Visual Studio Vb Examples

The complete project code is on my GitHub: https://github.com/johnhany/leetcode 。 For the configuration of C + + development environment under Linux platform, please refer to “Ubuntu computer vision development environment configuration (Python / C + +)”.

Visual Studio Code Cmake Settings

summary

Visual Studio Code Cmake Tutorial

This article is about windows configuration vscode + cmake + ninja+ Boost.Test C + + development environment (tutorial details) of the article introduced here, more related vscode configuration C / C + + environment content, please search the previous articles of developeppaer or continue to browse the related articles below, I hope you will support developeppaer more in the future!