Windows configuration vscode + cmake + Ninja+ Boost.Test C + + development environment of

Time:2020-11-23

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

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


#ifndef SOLUTION_HEADER
#define SOLUTION_HEADER
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
 vector<int> intersection(vector<int>& nums1, vector<int>& nums2);
};
#endif
solution.cpp
#include "solution.hpp"
static auto x = []() {
 // turn off sync
 std::ios::sync_with_stdio(false);
 // untie in/out streams
 cin.tie(NULL);
 return 0;
}();
vector<int> Solution::intersection(vector<int>& nums1, vector<int>& nums2) {
 if (nums1.size() > nums2.size())
 swap(nums1, nums2);
 unordered_set<int> A(nums1.begin(), nums1.end()), C;
 for (auto& i : nums2) {
 if (A.find(i) != A.end())
  C.insert(i);
 }
 return vector<int>(C.begin(), C.end());
}

solution.cpp


#include "solution.hpp"

static auto x = []() {
 // turn off sync
 std::ios::sync_with_stdio(false);
 // untie in/out streams
 cin.tie(NULL);
 return 0;
}();


vector<int> Solution::intersection(vector<int>& nums1, vector<int>& nums2) {
 if (nums1.size() > nums2.size())
 swap(nums1, nums2);
 unordered_set<int> A(nums1.begin(), nums1.end()), C;
 for (auto& i : nums2) {
 if (A.find(i) != A.end())
  C.insert(i);
 }
 return vector<int>(C.begin(), C.end());
}

solution_test.cpp


#define BOOST_TEST_MODULE SolutionTest

#include "solution.hpp"
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(SolutionSuite)

BOOST_AUTO_TEST_CASE(PlainTest1)
{
 vector<int> nums1{1,2,2,1};
 vector<int> nums2{2,2};
 vector<int> results = Solution().intersection(nums1, nums2);

 vector<int> expected{2};

 sort(results.begin(), results.end());
 sort(expected.begin(), expected.end());
 BOOST_CHECK_EQUAL_COLLECTIONS(results.begin(), results.end(), expected.begin(), expected.end());
}

BOOST_AUTO_TEST_CASE(PlainTest2)
{
 vector<int> nums1{4,9,5};
 vector<int> nums2{9,4,9,8,4};
 vector<int> results = Solution().intersection(nums1, nums2);

 vector<int> expected{9,4};

 sort(results.begin(), results.end());
 sort(expected.begin(), expected.end());
 BOOST_CHECK_EQUAL_COLLECTIONS(results.begin(), results.end(), expected.begin(), expected.end());
}

BOOST_AUTO_TEST_SUITE_END()

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.

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 Studio\2019\Community\VC\Auxiliary\Build\vcvars64.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 Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat”

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

Compile boost

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


> bootstrap.bat
> b2 --prefix=build install

After waiting for the compilation to complete, thebuild\libThere 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:


> b2 address-model=64 architecture=x86 
--with-test link=static --prefix=build install

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:


> mkdir build
> cd build
> cmake -G "Ninja" ..
> ninja test_main
> test_main.exe

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.

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:


{
 "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe",
 "terminal.integrated.env.windows": {
 "PATH" : "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64;E:\CMake\bin;E:\dev-lib\ninja",
 "INCLUDE": "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include;C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt",
 "LIB": "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\lib\x64;C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\lib\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\lib\10.0.18362.0\um\x64"
 },
 "cmake.cmakePath": "E:\CMake\bin\cmake.exe"
}

c_cpp_properties.json

The configuration of Linux and windows is given here.


{
 "configurations": [
 {
  "name": "Linux",
  "includePath": [
  "${workspaceFolder}/**"
  ],
  "defines": [],
  "compilerPath": "/usr/bin/clang++",
  "cStandard": "c11",
  "cppStandard": "c++17",
  "intelliSenseMode": "clang-x64"
 },
 {
  "name": "Win32",
  "includePath": [
  "${workspaceFolder}/**",
  "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include",
  "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include",
  "C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt",
  "C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared",
  "C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um",
  "C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt",
  "C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt",
  "E:\dev-lib\boost_1_73_0"
  ],
  "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
  "windowsSdkVersion": "10.0.18362.0",
  "compilerPath": "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe",
  "cStandard": "c11",
  "cppStandard": "c++17",
  "intelliSenseMode": "msvc-x64"
  }
 ],
 "version": 4
}

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.


{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
 {
  "label": "clean",
  "type": "shell",
  "command": "rm -r build/*"
 },
 {
  "label": "configure",
  "type": "shell",
  "command": "cmake",
  "args": [
  "--no-warn-unused-cli",
  "-DCMAKE_C_COMPILER=/usr/bin/clang",
  "-DCMAKE_CXX_COMPILER=/usr/bin/clang++",
  "-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE",
  "-DCMAKE_BUILD_TYPE=Debug",
  "-H${workspaceFolder}",
  "-B${workspaceFolder}/build",
  "-G'Unix Makefiles'"
  ]
 },
 {
  "label": "MSVC configure",
  "type": "shell",
  "command": "cmake",
  "args": [
  "-H${workspaceFolder}",
  "-B${workspaceFolder}/build",
  "-GNinja"
  ]
 }
 ]
}

launch.json

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


{
 "version": "0.2.0",
 "configurations": [
 {
  "name": "(gdb) Launch",
  "type": "cppdbg",
  "request": "launch",
  "program": "${workspaceFolder}/build/test_main",
  "args": [],
  "stopAtEntry": false,
  "cwd": "${workspaceFolder}",
  "environment": [],
  "externalConsole": true,
  "MIMode": "gdb",
  "setupCommands": [
  {
   "description": "Enable pretty-printing for gdb",
   "text": "-enable-pretty-printing",
   "ignoreFailures": true
  }
  ]
 },
 {
  "name": "(lldb) Launch",
  "type": "lldb",
  "request": "launch",
  "program": "${workspaceFolder}/build/test_main",
  "args": [],
 },
 {
  "name": "(cl) Launch",
  "type": "cppvsdbg",
  "request": "launch",
  "program": "${workspaceFolder}\build\test_main.exe",
  "args": [],
  "stopAtEntry": false,
  "cwd": "${workspaceFolder}",
  "environment": [],
  "externalConsole": false,
 }
 ]
}

CMakeLists.txt

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


cmake_minimum_required (VERSION 3.5)
project(leetcode)

set(PROBLEM_NAME "349-Intersection-of-Two-Arrays-set")

set(CMAKE_CXX_STANDARD 14)

set(SOLUTION_SOURCES ${PROJECT_SOURCE_DIR}/${PROBLEM_NAME}/solution.cpp)
add_library(solution STATIC ${SOLUTION_SOURCES})

enable_testing()

set(TEST_SOURCES ${PROJECT_SOURCE_DIR}/${PROBLEM_NAME}/solution_test.cpp)
set(TEST_LIBS solution)

add_executable(test_main ${TEST_SOURCES})

if(WIN32)
 message(STATUS "Detected Windows platform")
 set(BOOST_ROOT E:\dev-lib\boost_1_73_0)
 set(BOOST_LIBRARYDIR E:\dev-lib\boost_1_73_0\build\lib)
 set(Boost_USE_STATIC_LIBS ON)
 find_package(Boost REQUIRED COMPONENTS unit_test_framework)

 target_link_libraries(test_main PRIVATE ${TEST_LIBS} Boost::boost Boost::unit_test_framework)
elseif(UNIX)
 message(STATUS "Detected UNIX platform")
 find_package(Boost REQUIRED COMPONENTS unit_test_framework)
 add_library(boost_unit_test_framework STATIC IMPORTED)
 set_target_properties(boost_unit_test_framework PROPERTIES
 IMPORTED_LOCATION /usr/lib/libboost_unit_test_framework.a)

 target_link_libraries(test_main ${TEST_LIBS} boost_unit_test_framework)
else()
 message(FATAL_ERROR "Unsupported platform")
endif()

add_test(solution_test test_main COMMAND test_main)

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:

The debugging effect is shown in the following figure:

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 + +)”.

summary

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!

Recommended Today

The course of using Chinese software of poedit Pro

Poedit pro (formerly known as poedit) is a free (professional version charge), open source and cross platform gettext class (. Po format, gettext is used in the application program for program internationalization) International Translation editor. It is also one of the most widely used software of the same type. At present, it is available in […]