Hello. Today we will talk about Conan - a modern dependency manager for C / C ++. If you are already actively working with him, you will hardly find anything new for yourself. Otherwise - I ask under the cat.
Why do I need a dependency manager
If you are a Linux-based user of the distributive or macOS, then most likely it’s not a problem for you to pull up some necessary dependency - the distributions most likely have the <library_name> -dev package you need. But if you are a Windows user, I think you have often encountered a problem, how can you add a dependency to the project? Well, if the third-party library header-only - just need to copy the header files in the right and start using. But usually libraries need to be assembled (and as in C ++ zoo, it is often not so easy to do), then place the compiled library in the right place for you. And only after that you can use it.
Besides, let's not forget that we should compile libraries, preferably with one compiler, do not forget about ABI compatibility, etc. And the compilation itself takes a lot of time (hi Boost). Too much for ordinary mortals, is not it? In the meantime, some people just use
pip ,
npm ,
cargo ,
maven , etc. and save yourself a lot of nerves.
Previous attempts
I would like to have an analogue of npm, but for C ++. That literally one team to pull up the dependence you need in the project and not know a headache.
')
There have already been attempts to create such a tool for C ++. Maybe some of you remember such a thing as
biicode . He, unfortunately, ordered to live long.
Here you can find out the cause of death of the patient. An interesting fact: the creators of Conan and biicode are about the same people. A reasonable question arises: will Conan not repeat the fate of biicode?

There are modern alternatives to Conan. Of the most famous:
vcpkg ,
hunter ,
buckaroo .
How to install?
First you need to install Conan itself. There are many ways to do this: Your favorite package manager, pip, brew. There are also binary builds for Ubuntu / Debian and Windows. The authors themselves recommend using the installation via pip. If you do not like these methods, then you can always build yourself from the
source code .
Special features
Conan tracks for you such things as: compiler, compiler version, operating system, OS width, build type (Release / Debug), standard library version. Using these parameters, Conan tries to find the assembled version for your configuration. If you’re lucky, there is an already compiled version in the repository and you don’t have to compile anything.
If you are not lucky and under the combination of your OS / compiler /, etc. No package was found, Conan will
automatically download the source code of the required library and compile it on your working machine. After that, you can safely use the library, as if it had already been collected by someone.
How to use
We have such a timer.cpp, which we must make compile and work:
#include "Poco/Timer.h" #include "Poco/Thread.h" #include "Poco/Stopwatch.h" #include <iostream> using Poco::Timer; using Poco::TimerCallback; using Poco::Thread; using Poco::Stopwatch; class TimerExample{ public: TimerExample(){ _sw.start();} void onTimer(Timer& timer){ std::cout << "Callback called after " << _sw.elapsed()/1000 << " milliseconds." << std::endl; } private: Stopwatch _sw; }; int main(int argc, char** argv){ TimerExample example; Timer timer(250, 500); timer.start(TimerCallback<TimerExample>(example, &TimerExample::onTimer)); Thread::sleep(5000); timer.stop(); return 0; }
Consider a standard example from the documentation. We want to connect the
POCO library to our project (suppose we use
CMake ). To do this, in the project folder create a file conanfile.txt with the following text:
[requires]
Poco/1.8.0.1@pocoproject/stable
[generators]
cmake
Let's analyze what is what in this file. In the Requires section, we describe the dependencies we need in the form Library Name / Library Version @ Name of Mailer / Package Stability. The name of the maintainer is the person or organization who supports the package. Stability is simply an indicator of how stable a package is. Usually use stable, testing, ci. I recommend using only stable packages.
In the generators section, we specify what we want to integrate with. In our case, we specify integration with CMake. Conan for integration with CMake will automatically create a file conanbuildinfo.cmake, in which variables with paths to header files and already compiled libraries will be defined, which we will use in our CMakeLists.txt:
project(FoundationTimer) cmake_minimum_required(VERSION 2.8.12) add_definitions("-std=c++11") include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup() add_executable(timer timer.cpp) target_link_libraries(timer ${CONAN_LIBS})
Pay attention to lines 5-6. In line 5 we connect the previously generated conanbuildinfo.cmake, and in the 6th line we simply define a macro that makes Conan magically work :-)
Now we are ready to build. And before the standard CMake run, we have to ask Conan to install dependencies. This is done by the following command:
conan install /path/to/conanfile.txt
Usually the team will look just like
conan install ..
The most interesting thing happens here - how Conan tightens dependenciesDependencies search order:
- We check the local cache.
- We are looking for in the connected repositories we need the recipe. I draw your attention that the search is conducted strictly in the order in which your repositories are connected. If the package is in two repositories, then the first one will be found.
- If the recipe was found and there is an already assembled library for your configuration, then simply download the package from the repository and save it to the local cache. It is worth noting that the package is automatically downloaded with all dependencies to it.
- If there is no assembled library for your configuration (or if there is no assembled library dependency for your configuration), Conan will report this and ask you to run it again with the - build flag to start building the package on your machine.
If you do not want to use someone else's library and do not trust other people's builds, you can always specify the - build flag so that the library and all its dependencies are forcibly reassembled on your machine.
Congratulations, all dependencies are pulled up with the help of Conan (if lucky)!
Conan and build system integration
It is important not just to download the necessary version of the library, but also to connect it correctly to the project. And it is desirable that this be done as easily as possible for the developer. Therefore, Conan can integrate with such things as CMake, Visual Studio, Xcode, etc. You can always find the full list
here .
Conan as a way to manage your own dependenciesConan is not only a client for installing dependencies from repositories already created by someone. The authors of this dependency manager also kindly provided us with the program
conan-server , which allows literally in a couple of actions to raise their own server with ready-made libraries. Very convenient for companies that want to build their own dependency management infrastructure.
Existing problems
The value of a package manager is determined by its usability and the number of packages. If there are no special problems with the first one (Conan is really very convenient to use), then there are certain problems with the number of packages. If you look at the main repositories, you will see two of them:
conan-center and
conan-transit . Agree, it is misleading - what to use? Here you need to tell a little story.
Initially, Conan had one repository into which any user had the right to ship packages. Because of this, the Conan repository very quickly turned into one big trash bin. The authors decided that this could not continue and decided to create a repository, in which the packages will get only after a thorough check. So it appeared conan-center. And the old, “garbage” repository decided not to delete, but simply to transfer it to read-only mode and renamed it to conan-transit.
Where packages for open-source libraries are stored
JFrog Bintray is responsible for storing all the packages. Not so long ago, Conan moved to the Bintray infrastructure, so we had access to a more capacious package repository, as well as a
CDN . One of the most interesting innovations after the move is that you can create your own
OSS repository absolutely free of charge. If you want privacy, you can use the trial version for 30 days, and then have to pay.
Bincrafters
There are many libraries, and I would like to have ready-made packages for everyone. The Conan team is now very small and is mainly engaged in the development of the package manager itself, so they have no time to package everything (although they are constantly looking for developers, including for packaging). But you need to package the library, otherwise there is no point in Conan. This is how the Bincrafters team appeared - a team of enthusiasts that actively packs the open-source library. They have now opened a program at
Patreon to raise funds for the rental of additional
CI facilities. If you want to join the team - welcome.
What if the X library is not in Conan?
At this stage of development, this happens very often. You can always leave a request to create a package
here (before this, make sure that no one has asked you to do this before). If the application already exists, just write in the comments that this library and you need it. And, of course, you yourself can package the library, everyone will just say thanks.
The most useful commands (in my opinion)conan search <library_name_pattern>
- search for a package in repositories by a given patternconan info
- view package information (license, creation date, package source code address, etc.)conan remote
- manage Conan repositories (add, delete, etc.)
Tips
- Use only stable packages. The use of testing and ci packages is not recommended, as they can break regularly.
- If you decide to write your own recipes, an example of a good recipe can be found here .
- When creating packages, it is highly recommended to have a customized CI for all target platforms. The problem always pops up where you do not expect them :-)
- Do not forget to update Conan. It may happen that Conan will simply break backward compatibility and the recipe that works with the new Conan will not work with the old one.
- Recommended package search order (at the moment): conan-center , public-conan (repository of Bincrafters team), conan-transit , other repositories.
Useful links: