Catégories

How to use vcpkg with VSCode and CMake

Temps de lecture : 5 minutes

April 2021. The following software are installed on my PC

  • Windows 10 20H2 19042.870 (WIN key, type winver)
  • VSCode 1.55.2 (WIN + R, type powershell, type code --version)
  • Microsoft compiler cl version 19.28.29913. You can either install Visual Studio or Build Tools for Visual Studio.
  • Git version 2.31.1.windows.1 (in the previous console type git --version)
  • CMake version 3.20.1 (in the previous console type cmake --version). If you want to learn more about CMake you can start here.

Install vcpkg

Open a PowerShell console (Win + x, then select Windows PowerShell) and move to a directory which will host the vcpkg directory.

cd C:\Users\phili\Documents
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat

Launch Windows PowerShell as Administrator (Win + x, then select Windows PowerShell (admin)) and reach the previous directory

.\vcpkg integrate install

Close the PowerShell Admin console and switch back to the non-admin console. Type the following lines to find and install sqlite package.

.\vcpkg search sqlite
.\vcpkg install sqlite3:x64-windows

On a non-English configuration you may have to install the English package for Visual Studio. To do so, launch Visual Studio Installer. Check the language tab and add English. Once the English Pack is installed, restart the .\vcpkg install sqlite3:x64-windows command

Click on the image to display it full screen

Note

If you want to

  • List which libs are installed: ./vcpkg list
  • Update vcpkg. Reach vcpkg directory and type git pull and run again the bootstrapper (.\bootstrap-vcpkg.bat)
  • Remove vcpkg. From vcpkg directory, execute ./vcpkg integrate remove and then delete vcpkg directory.
  • Learn more. Read the vcpkg documentation

Build a Debug version

Create a directory to host the application and launch VSCode from there.

cd C:\Users\phili\Documents\Temp
mkdir TestSQL-vcpkg
cd TestSQL-vcpkg
code .

In VSCode, make sure C/C++, CMake and CMake Tools extensions are installed. Open the Extensions pane (CTRL + SHIFT +x) and check what is missing. See below:

Copy 'n paste the code below in a main.cpp file and save it.

#include <sqlite3.h>
#include <iostream>

int main(void) {
  std::cout << sqlite3_libversion() << '\n';
  return 0;
}

Once this is done, modify the VSCode Settings.json. You will have to do it once. Indded this is how we let VS Code know about CMake.

Anyway, to edit, Settings.json, strike CTRL + comma

To switch in text mode, click the first of the 3 icons on the top right-hand side (the one looking like a file)

At the end of the last line add a "," then add a line like this one. Obviously, the path to the vcpkg.cmake file needs to be customized according to your configuration.

"cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "C:/Users/phili/Documents/vcpkg/scripts/buildsystems/vcpkg.cmake"
    }

Just to make sure, here is the content of my Settings.json as it is today.

{
    "editor.tabSize": 2,
    "explorer.confirmDelete": false,
    "explorer.confirmDragAndDrop": false,
    "editor.minimap.enabled": true,
    "editor.minimap.renderCharacters": true,
    "zenMode.centerLayout": false,
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "workbench.iconTheme": "material-icon-theme",
    "[typescript]": {
        "editor.defaultFormatter": "vscode.typescript-language-features"
    },
    "liveServer.settings.CustomBrowser": "chrome",
    "[cpp]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "editor.fontFamily": "'Cascadia Code PL', Consolas, 'Courier New', monospace",
    "editor.fontLigatures": true,
    "window.zoomLevel": 0,
    "editor.mouseWheelZoom": true,
    "debug.console.fontSize": 12,
    "editor.fontSize": 12,
    "cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "C:/Users/phili/Documents/vcpkg/scripts/buildsystems/vcpkg.cmake"
    },
    "cmake.configureOnOpen": true
}

CTRL + SHIFT + P and type CMake then select CMake Quick Start

When asked to select a target, pick Visual Studio Community 2019 Release - amd64 for example. Remember initially we installed sqlite3:x64-windows

Later, you will be asked for the name of the project ("TestSQL" in my case) and the kind of project (executable or lib). Here, select executable.

Here is the content of the generated CMakeLists.txt you will get

cmake_minimum_required(VERSION 3.0.0)
project(TestSQL VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(TestSQL main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

This should be simplified and modified as follow. Pay attention to the tabulation before the paths : c:/Users/phili/Documents/...

cmake_minimum_required(VERSION 3.0.0)
project(TestSQL VERSION 0.1.0)
INCLUDE_DIRECTORIES(
  c:/Users/phili/Documents/vcpkg/installed/x64-windows/include/
)
  
LINK_DIRECTORIES(
  c:/Users/phili/Documents/vcpkg/installed/x64-windows/lib/
)
add_executable(TestSQL main.cpp)
TARGET_LINK_LIBRARIES(TestSQL
  sqlite3
)

Later, if the project needs additional libraries, just add their names after sqlite3 and insert a space between them.

TARGET_LINK_LIBRARIES(TestSQL
  sqlite3 mylib blabla_lib
)

Everything seems to be configured. CTRL + SHIFT + P then type CMake and select CMake: Build. You can also strike F7 which is much faster.

If everything went well, you should have a Debug directory hosting a TestSQL.exe debug version.

It is time to Debug our code. Set a breakpoint on line 5 and then strike F5. When asked, select C++(Windows).

Later select cl.exe

Here is the content of the initial launch.json file you may get.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "cl.exe - Build and debug active file",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "console": "externalTerminal",
      "preLaunchTask": "C/C++: cl.exe build active file"
    }
  ]
}

Now, here is the modified version where preLaunchTask is commented and where ${fileBasenameNoExtension} is replaced by /build/Debug/TestSQL.exe. Indeed, this where the executable has been generated.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "cl.exe - Build and debug active file",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/Debug/TestSQL.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "console": "externalTerminal",
      //"preLaunchTask": "C/C++: cl.exe build active file"
    }
  ]
}

Press F5. If everything works as expected the program should stop on line 5. You should know what to do from this point 😁

Build a Release version

At the bottom of VSCode, click on CMake: [Debug]: Ready

And select Release in the popup list.

Note

If you want to change the kit (Visual Studio 2019 Release - amd64 was our initial choice), at the bottom of the VS Code window, click on the status bar, and select another compiler kit. However, if you select Visual Studio 2019 Release - x86 for example, make sure that you have installed the corresponding lib using vcpkg. In our case, so far, we only have the x64 version of the lib and not the x86 version. You may also have to update the content of the CMakeLists.txt. Remember, so far, we wrote something like:

INCLUDE_DIRECTORIES(
  c:/Users/phili/Documents/vcpkg/installed/x64-windows/include/
)
 
LINK_DIRECTORIES(
  c:/Users/phili/Documents/vcpkg/installed/x64-windows/lib/
)

Anyway, press F7 to build the release version. At the end of the build, a Release directory with a brand new TestSQL.exe should be available.

On a french keyboard, strike CTRL + ù to open a new terminal and type the line below to run your application.

cd .\build\Release\
./TestSQL.exe

Conclusion

vcpkg is great. The integration in Visual Studio is even smoother than the one with VSCode. Whatever, vcpkg elegantly solve one of the most annoying developer's tasks: getting libs and making sure they are quickly integrated with our compiling toolchain.

I hope this page will speed up your start with vcpkg and VSCode on Windows. Feel free to add comments to help me improve this article and help others.

2 comments to How to use vcpkg with VSCode and CMake

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.