Conan Virtual Environments: Manage your C and C++ tools (2023)

The benefit of using a package manager like conan is, in addition to many other advantages, managing your project's dependencies. You can build your project even on different computers and with different operating systems just by runningconan installationcommand again.

But that's not entirely true, you usually need some tools like a compiler and maybe a separate build system to build your project. So if you change computers, you'll need to configure your build tools with the system package manager (apt, yum...) or download the installers, etc.

In the latest version 0.11 we made some minor changes to conan to try to help our users to easily reproduce their environments. We do not intend to replace current application managers; yum, apt, brew…etc are GREAT tools, but perhaps conan users could benefit from easy, custom installers for the applications they use to build software.

Conan has a new generator calledvirtual environment, inspired by the python virtual environments we love. This generator can be activated like any other in the [generators] section of your conanfile, and it will generate two files:activate.[sh|bat]ydisable.[sh|bat]. If we run the activation script, conan will set our current shell environment variables to the values ​​that declare the installed packages. The most common usage would be to set the PATH environment variable, so that the tools can be easily run.

Let's look at an example. Suppose we are working on a Windows machine and we want to build and test a project with different versions of MinGW and CMake. Then we can do:

Create a separate folder for your project. This folder will take care of our development environment.


now create aconanfile.txtfile:

[requires]mingw_installer/0.1@you are dripping/evidencecmake_installer/0.1@you are dripping/evidence[generators]virtual environment

In this file, we need two packages, one is MinGW installer and the other is CMake installer. Version "0.1" is the recipe version of the installer, not the MinGW or CMake tools. If you want to create your own recipes that match the tool version you can, but this way we can handle many versions of MinGW and CMake with just the same recipes. Later we will see how we can install these different versions.

Install the packages:


Once the tools are installed, you can activate the virtual environment in your shell:

PSactivate(my_cpp_environment) PS

Make sure everything works and the tools are in the way:

(my_cpp_environment) PSCCG--version>CCG(x86_64-posix-sjlj-rev1, built by the MinGW-W64 project)4.9.2 Copyright(C)2014 Free Software Foundation, Inc. This is free software;see thesourceforcopy conditions. there is no guarantee;not evenforMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
(my_cpp_environment) PSto do--versioncmake version 3.6.0 CMake Suite maintained and supported by Kitware(

You can now disable the virtual environment with thedeactivateroad map

(my_cpp_environment) PSdeactivate

The same can be done (with CMake, because MinGW is Windows only), on Linux/OSx. If you are a Conan user, you will know that each package depends on settings and options. So we can change the available options to easily get a different version of MinGW.

Let's take a look at the MinGW installer recipe. Go to ( and click on the "" tab.

We have these options available:

options= {"topics": ["posix","win32"],"exception": ["enano2","sjlj","seh"],"arco": ["x86","x86_64"],"version": ["4,8","4,9"]}default_options="exception=sjlj","threads=posix","arc=x86_64","version=4.9" 

By default we are installing MinGW 4.9 with support for posix threads and sjlj exceptions. But we can install MinGW with other options:

Edit your conanfile.txt:

[requires]mingw_installer/0.1@you are dripping/evidencecmake_installer/0.1@you are dripping/evidence[generators]virtual environment[options]mingw_installer:topics=ganar32mingw_installer:version=4.8

Remember to disable the previous virtual environment:

(my_cpp_environment) PSdeactivate

And install again:


You can also pass options on the command line instead of specifying them in the conanfile.txt file

Activate the virtual environment and verify that the tools have changed:

PSactivate(my_cpp_environment) PSCCG--version>CCG(rev0, built by the MinGW-W64 project)4.8.2 Copyright(C)2013 Free Software Foundation, Inc. This is free software;see thesourceforcopy conditions. there is no guarantee;not evenforMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

can you share thisconanfile.txtwith your team and thus share the development environment!

If you want to create conan packages for any tool, it's easy, especially if you are already familiar with creating conan packages.

let's see how or conanCMake recipe is ready:

of conanos matter Conan Archivematter operational systemclassroom CMakeInstallerConanName(Conan Archive): name = "cmake_installer" version = "0,1" license = "SWINDLER" URL = "" settings = {"os": ["windows", "linux", "Mac OS"], "arco": ["x86", "x86_64"]} options = {"version": ["3.6.0", "3.5.2", "3.4.3", "3.3.2", "3.2.3", "3.1.3", "3.0.2", "2.8.12"]} default_options = "version=3.6.0" definitely context(to be): e to be.settings.operational system == "Mac OS" y to be.settings.arco == "x86": increase exception("Not x86 compatible for OSx") e to be.settings.operational system == "linux" y to be.options.version == "2.8.12" y to be.settings.arco == "x86_64": increase exception("Not compatible with 2.8.12 for x86_64 binaries") definitely get_filename(to be): operational system = {"Mac OS": "Darwin", "windows": "win32"}.get(calle(to be.settings.operational system), calle(to be.settings.operational system)) arco = {"x86": "i386"}.get(calle(to be.settings.arco), calle(to be.settings.arco)) e to be.settings.operational system != "windows" the rest "x86" give back "cmake-%s-%s-%s" % (to be.options.version, operational system, arco) definitely build(to be): keychain = "%s_%s_%s" % (to be.settings.operational system, to be.settings.arco, calle(to be.options.version)) smaller = calle(to be.options.version)[0:3] extension = "tar.gz" e no to be.settings.operational system == "windows" the rest "zipper" URL = "" % (smaller, to be.get_filename(), extension) destination_file = "file.tgz" e to be.settings.operational system != "windows" the rest "" to warn("Downloading: %s" % URL) go down(URL, destination_file) tools.unzip(destination_file) definitely package(to be): to be.Copy("*", DST="", origin=to be.get_filename()) definitely package_info(to be): to be.env_info.path.attach(operational bring together(to be.package_folder, "paper box")) 

The configure method avoids some configuration/option combinations that throw an exception. The build method downloads the correct CMake file and unpacks it. The package method copies all the files in the zip to the package folder. Package information uses " self.env_info" to add the package's bin folder to the "path" environment variable.

This package has only 2 different things from a regular conan library package:

Source method is missing. This is because when you build a library the source code is always the same for all the packages generated, but in this case we are downloading the binaries so in the build method we download the different zip file for each configuration/option combination. Rather than actually creating the tools, we're just downloading them. Of course, if you want to compile it from source, you can do that too, create your own package recipe. The package_info method uses the newself.env_infoobject. With “self.env_info” the package can declare environment variables which will be set with thevirtual environmentgenerator.

Heself.env_infoThe variable can also be useful if a package tool depends on another tool. take a look atReceta MinGW

classroom MingwinstallerConanName(Conan Archive): name = "mingw_installer" version = "0,1" license = "SWINDLER" URL = "" settings = {"os": ["windows"]} options = {"topics": ["posix", "win32"], "exception": ["enano2", "sjlj", "seh"], "arco": ["x86", "x86_64"], "version": ["4,8", "4,9"]} default_options = "exception=sjlj", "threads=posix", "arc=x86_64", "version=4.9" definitely context(to be): to add("7z_installer/0.1@lasote/pruebas", private=TRUE)  definitely build(to be): ... go down(files[keychain], "file.7z") environment = ConfigureEntorno(to be) to"%s && 7z x file.7z" % environment.Command Line) definitely package(to be): to be.Copy("*", DST="", origin="mingw32") to be.Copy("*", DST="", origin="mingw64") definitely package_info(to be): to be.env_info.path.attach(operational bring together(to be.package_folder, "paper box")) to be.env_info.CXX = operational bring together(to be.package_folder, "paper box", "g++.exe") to be.env_info.CC = operational bring together(to be.package_folder, "paper box", "gcc.exe")

In the configure method we are adding a requirement to another package, the 7z_installer which will be used to unpack the mingw installers (with 7z compression).

In the build method, we download the correct MinGW installer and use the wizardConfigureEntorno. This helper will give us a string with a command to set the environment variables. This means that the 7z executable will be in the path, because the 7z_installer dependency declares thepaper boxfolder in yourpackage_info()method.

In the package_info method we are declaring CC and CXX variables, used by CMake, autotools, etc., to localize the compiler to C/C++ respectively. we are also addingpathvariable the bin folder so that we can invoke gcc, g++, make and other tools from the command line using the virtualenv generator when we run theactivateroad map.

The possibilities of these features are even greater. They are very useful, for example, in CI environments. Also, if you want to avoid copying shared libraries (.dlls, .dylib) into the project's binary directory (which can be done withit mattersfeature), you can create packages to add to the PATH environment variable so consumers can use it to easily find the right shared libraries for many different versions and configurations.

What do you think? Do you like this feature? What tools would you like to have as a conan package? use ourwhistleand tell us what your development environment is!

Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated: 03/30/2023

Views: 6473

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.