Anastasia Kazakova
Wondering how to add a package to your C++ project? In this guest blog post, Javier G. Sogo of JFrog shares how to get started using Conan's package manager in CLion.
![]() | Javier G. Sogo @jgsogo Software Engineer at JFrog After several years of developing in C++, I switched to Python to learn best practices and tools. Now I'm back building Conan, the C/C++ package manager. |
Writing a C++ project from scratch is always a delightful challenge. Starting from a blank page, writing your code and building your functionality, to build on top of robust, high-performance libraries... but wait! Sometimes it's not easy to have these libraries available for your project to compile and link, and you may not have experience with that build system, or the library may require some patches to be compiled for your platform.
The lack of a package manager in the C++ world has slowed down many projects, requiring dedicated efforts to deal with external libraries or rewrite functionality to avoid using third parties.
conanis an open source decentralized package manager that simplifies the process of integrating and reusing C and C++ libraries and packages. Automatically manages and reuses binaries for any possible configuration, increasing development speed. Conan is also capable of building and combining packages from source code, integrating with any build system, with very high customization and flexibility features.
In this blog post, we'll show you how to use Conan in your CLion workspace to get your favorite libraries ready to use in your project. We'll use the Conan-CLion plugin to seamlessly integrate the two applications, for example by creating an MD5 hasher using an already available implementation of the Poco libraries.
Installation of all parts
1. Conan-CLion add-on
To follow this example, you will need to installJFrog Conan Add-onfrom the JetBrains marketplace. It can be easily installed using the Plugins section in the Preferences/Settings window or from the Welcome screen using Configure > Plugins.
After restarting CLion, a new "Conan" tool window will appear at the bottom of the IDE, ready to use.
2. Conan Client
Next you will need to install Conan. Conan is a python application, you can install it using pip (pip install conan) inside a python virtual environment or system wide, or you can download a standalone application and unzip it on your system, choose the way that best suits you. adapts to your needs. needs ofdownload page.
We strongly recommend that you read theConan Documentation; While the default settings are sufficient for this example, it is important that you are aware of many other important features that are not covered in this article. Including, the distributed nature of Conan that allows you to mix different sources for your libraries,JFrog Artifactory Integrations, workspaces, etc. These resources will help you to develop high quality software.
3. Conan plugin settings
There are some configuration points we need to address before running our example, these configurations only need to be done once for all your projects. First, you'll need to provide the path to the Conan executable you want to use in the Preferences window (leave blank to use Conan's PATH).
Second, we need to define the correspondence betweenCLion Profiles CMakeNameand Conan profiles. CLion profiles are a convenient way to create different configurations to build your project, just like Conan profiles, so both must match.
[configuración]os=Macosos_build=Macosarch=x86_64arch_build=x86_64compiler=apple-clangcompiler.version=10.0compiler.libcxx=libc++compiler.cppstd=gnu17build_type=Release[opciones]zlib:shared=False[build_requires]cmake_installer/3.13.0 @conan/estable[entorno]
A Conan profile is a file in which the user defines a set of settings, options, environment variables and build requirements that can be reused to build any package. These files can be managed individually using the `conan profile` commands, or if you are working with others you will probably want to maintain a shared configuration with your colleagues using`conan setup install`commands Create as many profiles as you like using the command line or copy a URL with your shared Conan settings in the plugin settings. For more information related to this main feature of Conan, thedocumentation on using profilesIt's a good starting point.
When your profiles are ready, merge them with CLion's using theA window will appear for you to make these assignments.
Creating the C++ project
With the setup complete, we can move on to C++. the following exampledemonstrates theMD5hashutility in Little library, a few lines of code should get you started:
#includes "Poco/MD5Engine.h"#includes "Poco/DigestStream.h"#includes <iostream>int main(int argc, char** argv){ Poco::MD5Engine md5; Bit::DigestOutputStream ds(md5); ds << "abcdefghijklmnopqrstuvwxyz"; ds.close(); std::cout << Bit::DigestEngine::digestToHex(md5.digest()) << std::endl; retorna 0;}
If we try to build this project using CLion it will fail because the Poco libraries are missing. This is where our package manager comes in.
Getting your dependencies with Conan
Conan uses a separate file to store information related to a project. It could be a simple `conanfile.txt` file or a python `conanfile.py` file that handles more complex logic regarding your dependencies. The Conan plug-in can work with both types of files.
For this example, we just need a few lines in a `conanfile.txt` that you need to add to your repository:
[requer]Poco/1.9.0@pocoproject/stable[generators]cmake
[requires]
These lines notify Conan that our project requires the Poco library and provide the library version and source. Conan will look for the remotes and retrieve Poco's recipe from there, it will also grab the binaries if they are available for your setup, and if not it will trigger a source build. All this will be done using Conan's install button,, which can be found in Conan's tools window.
As you can see, Conan not only installed the Poco libraries, but also OpenSSL and ZLib. That's because they are transitive Poco dependencies. If binaries matching your configuration are available on the remote, Conan will simply download them; otherwise, Conan will build these packages from source, regardless of whether they use CMake or other build systems (OpenSSL uses Makefiles).
[generators]
Conan will create a file for each of the generators listed here, since CLion uses CMake we need that generator. These files contain all the requirements information, including: paths to includes, libraries or resources, and build flags. Basically everything that needs to be consumed by your project. You just need to include this file in your build system, nothing else needs to be changed and that's it.
Modify your `CMakeLists.txt` to include this automatically generated file called `conanbuildinfo.cmake`:
cmake_minimum_required(VERSIÓN 2.8.12)proyecto(MD5Hasher)include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)conan_basic_setup()add_executable(md5_hasher main.cpp)target_link_libraries(md5_hasher ${CONAN_LIBS})
Rebuild and run your project. Conan will take care of your dependencies, finding and downloading (or compiling) them, making them available to your project. If you feel that this implementation requires modifications to your project, you can try other CMake-related generators available in Conan, such as `cmake_find_package` or `cmake_paths`. Additional information on how to activate these generators can be found atConan Documentation.
Conclusion
Along with CMake and CLion, Conan has become one of the big names in the C++ language. Having a package manager encourages the community to share their libraries and build on existing ones, modernizing the C++ ecosystem into a more mature and useful one. It also allows companies to have a professional tool to build all their in-house and third-party libraries using the same process, preserving the consistency and reproducibility of their builds and keeping their developers focused on their work.
The plugin in its first version only implements some basic functionality of Conan, we recommend you to use it in your projects, but also to explore all the functionality that Conan provides. we are always happy toreceive feedbackrelated to the CLion plugin and Conan in general.
Happy coding!
conan guest post bulletin-cpp package and dependency manager
- To share
CLion 2019.1.3 updateCLion releases 2019.2 EAP: parameter hints, go to address in memory view, code support for ClangFormat config files, and more