BlogAtlas Vector Search voted most loved vector database in 2024 Retool State of AI reportLearn more >>
MongoDB Developer
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right

CMake + Conan + VS Code

Jorge D. Ortiz-Fuentes6 min read • Published Jun 28, 2024 • Updated Jun 28, 2024
Facebook Icontwitter iconlinkedin icon
Rate this tutorial

CMake + Conan + VS Code

Hey there, C and C++ developers! Today, I want to talk about a very sensitive topic for all of us. A thing that has caused pain and shame in the community. A thing that has affected our productivity and sometimes even our mental health. I am not referring to compiler errors, but to building complex projects and using third-party dependencies in them.
Traditionally, we used Makefiles to build our C and C++ projects and depended on having our third-party dependencies installed in our development systems or their sources included in our project in one way or another. The tools have evolved a lot through the last couple of decades, but not everybody is using them completely or even at all to build their projects.
Unless you live under a rock, you have probably seen what your fellow developers do when they use other powerful backend programming languages. Golang has support for third-party modules built-in. You just mention the URL of the third-party repository and it takes care of the rest. Jealous? Rust's cargo tool takes care of building your project and managing all its dependencies even when the project is distributed into several workspaces. Envious? Ashamed no more! Let me show you how to use CMake and Conan within VS Code.
This article is aimed at C or C++ developers who work on projects that produce binaries or libraries and want to be able to use third-party dependencies. In our case, we will be using the MongoDB driver for C++.
In this article, I will:
  • Create an example project from scratch with a directory structure that can be used to work with one or more libraries and a binary.
  • Write the instructions to build the different pieces of the project and link them together.
  • Create the configuration to use a third-party dependency using a package manager.
  • Put all these pieces together and make them work with VS Code.

Your project

We are going to focus on a project that produces a library, which uses a third-party package. The library is then used together with some other source code to produce a binary. The same structure can be extended to have more libraries or even more binaries. In this case, the third-party package is the MongoDB driver, which allows you to connect to a MongoDB Atlas cluster or a local MongoDB database.
  1. This is the basic directory structure:
  2. In the header libraryN.h, we add the traditional protection to avoid multiple inclusion:
  3. Inside of this preprocessor directives, we are going to define a class:
  4. And its constructor:
  5. Now, we are going to write the implementation code for this library in libraryN.cpp. We start by including the header that we have just declared:
  6. Then, we define the implementation of the constructor that we declared in the header:
  7. To be able to use the MongoDB driver here, we need to include the header for the desired part of the driver. In this case, we are going to create an instance:
  8. We can now use the instance in the constructor:
  9. And finally, we have to write the code that uses the library from the executable. We include the header of the library in main.cpp:
  10. We declare the main function here, which returns 0:
  11. And we finish by creating an instance of the class that we defined in the library:

Create the instructions to build

We are going to use CMake as the build system for our project. The instructions to CMake are provided in the CMakeLists.txt.
  1. We start by defining the minimum version of CMake that we allow to be used with our project.
  2. Then, we define the project using a name, a version and the language(s). This would also work for a C project, but in this case, we are going to use C++:
  3. The first actual instructions to build the project will be used to tell CMake to include our library. First, we include the sources –a single file, in this case– and then we provide the corresponding header files.
  4. Finally, we add the code for the executable and link the library that we defined in the previous step.

Configure the dependencies

The build instructions that we have just written would be suitable for projects with one library and an executable. You could add more libraries and other executables using the same CMake commands with the corresponding data. But if you want to bring in a third-party dependency, your options with CMake are more painful. This is where Conan comes to our rescue.
Conan is an open-source package manager for C and C++ that simplifies the process of integrating third-party dependencies in our project. It is an alternative to Microsoft's vcpkg, but they have slightly different approaches. They both have repositories of third-party dependencies and can be configured to include the ones that you need in your project. However, while vcpkg uses a series of tags that define the versions of the dependencies in the repository that are tested to work together, Conan allows you to choose independent versions for each dependency. You can choose individual versions with vcpkg, but it requires some extra effort.
  1. We must have Conan installed. In my case, I went with Homebrew.
  2. The first thing we need to do is to check if the package we want to use –MongoDB C++ driver– is available in Conan Center. So we visit that page and type "mongo" in the search box. Conan Center offers both the mongo-c-driver and the mongo-cxx-driver. We will use the second, so click on it. Conan center recipes
  3. On the mongo-cxx-driver page, it suggests using a simple conanfile.txt or a We will go with the file with "txt" extension, because although it is less versatile than the Python configuration, it is enough for our needs in this example. So, let's create a conanfile.txt with this content:

Project setup

Also, the page for the dependency suggests adding a couple of lines to our CMake configuration.
  1. We add a line to the CMakeLists.txt, right after the project declaration line, to find the package that Conan will install.
  2. Add a line to the bottom of the block for the library that will instruct CMake to link the dependency to it. We link the dependency to the library and not the executable because this is where we use the dependency.
  3. We use the command line to finish setting up the project. First, we create the profile that contains information about our toolchain and the type of build, which is going to be "Release." You can create a debug profile by copying the file that has been created and changing the build_type to "Debug" in the new file.
  4. Then, we can install the packages and tell Conan to produce the configuration. You can use the next command again with --profile=debug (or the filename of your debug profile), if you also want to also install the debug version of the packages.
  5. At this point, we should be able to build the project from the command line using the preset created by Conan. We can list CMake presets using the following command:
  6. There should be only one configuration preset available –or two if you also installed the debug one–, so let's use it to configure the project:
  7. And we can build the project using the corresponding build preset:
  8. Hug your partner, your friend, or your pet, because you have successfully built the project. Yippee ki-yay, my friend!

Use from VS Code

Let's clean the build to see how this works with VS Code.
  1. We need to specify the directory that Conan has used for the cache.
  2. And before we try to build from VS Code, we have to be aware that you will need to have two extensions installed: CMake and CMake Tools. VSCode extensions
  3. From the command palette (View -> Command Palette or Cmd+S+P), we invoke "Developer: window reload." VS Code will reload the directory, check the contents of the CMakeLists.txt, and offer us a choice of one of the available profiles. We should choose one of the presets provided by Conan.
  4. To build the project from VS Code, click on the CMake Tools icons on the sidebar. CMake-Tools usage
  5. We should be able to build, run, and even debug without any problems.


In this tutorial, we have explored some options to sharpen our tools. We can now use CMake for building mid-sized projects and Conan to take care of our dependencies. We can use these two to build from the command line, but we have gone a step further and used that configuration from our beloved VS Code.
Stay curious, hack your code, see you next time!
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this tutorial

Red Mosquitto: Implement a noise sensor with an MQTT client in an ESP32

Mar 18, 2024 | 25 min read
Industry Event

CppCon 2024

Sep 15, 2024 - Sep 21, 2024

Me and the Devil BlueZ: Reading BLE sensors from C++

Mar 18, 2024 | 16 min read

Storing Binary Data with MongoDB and C++

Sep 18, 2023 | 6 min read
Table of Contents
  • CMake + Conan + VS Code