The use of clion combined with vcpkg and GTest

created at 09-15-2021 views: 24

1. Introduction, download and use of vcpkg

1.1 What is vcpkg

For our users, vcpkg is equivalent to a c++ package manager. Similar package managers in other languages include:

  1. Java: Maven
  2. JavaScript: npm, yarn
  3. python: pip

In fact, when I used python or Java before, I deeply felt the convenience of the package manager. At the time, I didn’t hear that C++ had a better package manager, and I only heard of vcpkg a year ago. This thing, but because I was not familiar with how cmake was written before, and the "Computer Graphics" course of this semester needs to follow the OpenGL-related library. At this time, I need to use vcpkg for the first time and feel its convenience. So also write a blog to record it.

1.2 vcpkg download

GitHub link:

clone url:

We need to download the project first:

git clone

then execute:


This instruction should go to GitHub to download the executable file corresponding to vcpkg.

After it is executed, the executable file of vcpkg.exe will appear in the project


Then vcpkg is downloaded

1.3 Use vcpkg to download third-party libraries

There are many third-party libraries in vcpkg, such as boost, GTest, jsoncpp, etc., we can use the following instructions to download the required libraries:

vcpkg install [packagename]

for example

vcpkg install package

When the third-party library is downloaded, it will prompt the script code that needs to be added when using cmake to build the project.

2 clion combined with vcpkg

Although vcpkg is a package manager for the Visual Studio compiler, it feels that compared to clion, vs has many places that are very uncomfortable (especially for students who have used IntelliJ IDEA), and clion can also choose to use vs as a compiler Environment, so I use clion here to use the third-party library in vcpkg.

1. Method 1: Use environment variables

First set two environment variables:

  • VCPKG_ROOT: vcpkg folder directory
  • VCPKG_DEFAULT_TRIPLET: x64-windows

environment variables

Then add it to CmakeLists.txt in the root directory of the project (located between the C++ standard declaration and the project declaration):

# c++11

# vcpkg <--
  set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
      CACHE STRING "")

# project

First use clion to open (or create a new) cmake project, and click the [cmake] tab below:

Add cmake command

Then fill in the required commands into the text box as shown below:

 fill in the required commands

The commands that need to be filled in are:

-DCMAKE_TOOLCHAIN_FILE=<your own vcpkg directory>/scripts/buildsystems/vcpkg.cmake

for example


Then you need to pay attention to switch the compiler to Visual Studio (both methods need to be switched), because vcpkg is actually a c++ package manager for Visual Studio, so many third-party libraries can only run successfully using the vs compilation environment (tested by GTest And Boost.test failed to compile and run using mingw)

Visual Studio

3 clion uses GTest through vcpkg

1. Introduction to GTest

The full name of GTest is Google Test, which is a set of unit test library for C++ developed by Google.

I have been known Junit when I was developing Java before, and I felt that unit testing was really convenient. For junior developers like me, unit testing is just a few more program execution entrances, but it is indeed very convenient to write little-endian code. Take notes and test smaller modules.

2. GTest configuration

First use vcpkg install gtest to download the gtest library

Then you will find a prompt like this in the command line:

The package gtest:x64-windows provides CMake targets:

    find_package(GTest CONFIG REQUIRED)
    target_link_libraries(main PRIVATE GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)

We can paste the next two lines of commands directly at the end of CMakeLists.txt:

add_executable(main ...)

find_package(GTest CONFIG REQUIRED)
target_link_libraries(main PRIVATE GTest::gtest GTest::gtest_main)

Note that in the target_link_libraries here, I removed the gmock-related configuration, because the two links at the same time will conflict, and generally we can use one of the two.

3. Simple use of GTest

Write in main.cpp:

#include <iostream>
#include <gtest/gtest.h>
using namespace std;

TEST(TestCase, t1) {
    cout << "hello world" << endl;

Then you will find that clion has the same user-friendly run button as IDEA:


Then we can run the test code block:

run the test code block

running result:

running result

Of course, there are many very powerful functions in GTest, but I don’t use them yet. At present, I only need a test entry log and some test information haha.

tips: Same as the use of the GTest library introduced here, other third-party libraries can be successfully configured according to this process, such as boost, jsoncpp, etc. I personally think that the only disadvantage of vcpkg is that it is bound to the vs compilation environment. The library cannot be compiled and run in the mingw environment.

created at:09-15-2021
edited at: 09-15-2021: