

Migrating an existing C++ codebase to conan
source link: https://www.tuicool.com/articles/hit/jeE77nb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This is a bit of a battle report of migrating the dependencies in my C++ projects to use the conan package manager .
In the past weeks I have started to use conan in half a dozen both work and personal projects. Here’s my experiences so far.
Before
The first real project I started with was my personal game project . The “before” setup used a mixture if techniques to handle dependencies and uses CMake to do most of the heavy lifting.
Most dependencies reside in the “devenv”, which is a separate CMake project that I use to build and bundle the dependencies in a specific installation folder. It uses ExternalProject_Add
for most parts (e.g. Boost, SDL, Lua, curl and OpenSSL), add_subdirectory
for a few others (pugixml and lz4) and just install(FILES...)
for a few header only libs like JSON for Modern C++, Catch2 and spdlog. It should be noted that there are relatively few interdependencies between the projects in there.
Because it is more convenient to update, I keep a few dependencies that I control myself directly in the source tree, either as git externals or just copies of the source files.
I try to keep usage of system dependencies to a minimum so that the resulting binary is more portable to the average gamer who does not want to know about libraries and dependencies and such nonsense. This setup has been has been mostly painless and working for my three platforms Windows, Linux and Mac – at least as long as I did not try to change it significantly.
Baby steps
Since not all my dependencies are available on conan and small iterations are usually more successful, I decided to proceed by changing only a single dependency to conan. For this dependency, it’s a good idea to pick something that does not have many compile-time options and is more or less platform agnostic. So I opted for boost over, e.g. SDL or wxWidgets. Boost was also one of the most painful dependencies to build, if only for the insane amount of files it produces and the time it takes to copy those ten-thousands of files to the install location.
Getting started..
There are currently two popular variants of boost available through conan. The “normal” variant
on conan’s main repository/remote “conan-center” and a modular version that splits boost into its component libraries on the bincrafters remote, e.g. Boost.Filesystem
. The modular version is more appealing conceptually, and I also had a better time getting it to work in my first tests, so I picked that. I did a quick grep for #include <boost/
through my code for an initial guess which boost libraries I needed to get and created a corresponding conanfile.txt in my project root.
[requires] boost_filesystem/1.69.0@bincrafters/stable boost_math/1.69.0@bincrafters/stable boost_random/1.69.0@bincrafters/stable boost_property_tree/1.69.0@bincrafters/stable boost_assign/1.69.0@bincrafters/stable boost_heap/1.69.0@bincrafters/stable boost_optional/1.69.0@bincrafters/stable boost_program_options/1.69.0@bincrafters/stable boost_iostreams/1.69.0@bincrafters/stable boost_system/1.69.0@bincrafters/stable [options] boost:shared=False [generators] cmake
Now conan plays really nice with “single configuration generators” like the new CMake/Ninja support in VS2017 and onward. Basically, just cd into your build dir and call something like conan install -s build_type=Debug -s build_type=x86
whenever you want to update dependencies. More info can be found in the official documentation
. The workflow for CLion is essentially the same.
Using it in your build
After the last command, conan will download (or build) the dependencies and generate a file with all the corresponding paths.
To use it, include it from cmake like this:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) conan_basic_setup(TARGETS KEEP_RPATHS)
It will then provide targets for all the requested boost libraries that you can link to like this:
target_link_libraries(myTarget PUBLIC CONAN_PKG::boost_filesystem )
I wanted to make sure that the compiler build using the new boost files and not the old ones. Because I have a generic include into my devenv that was still going to be in my compilers include-paths for all the other dependencies, so I just renamed boost’s header include folder on disc. After my first successful compile I felt confident enough to delete them.
First problems
There was one major problem: some of my in-source dependencies had their own claim on using boost via passed CMake variables, Boost_LIBRARY_DIRS
and Boost_INCLUDE_DIR
. I adapted their CMakeLists.txt to allow for injecting appropriate targets instead. Not the cleanest solution, but it got my builds green again fast.
There’s a still a lot to cover on this: The other platforms had their own quirks and I migrated way more than just this first project. Also, there is still ways to go for a full migration with my game project. But more on that in my next blog post…
Recommend
-
18
Migrating Existing REST APIs to GraphQL Learn how to migrate existing REST APIs to GraphQL with Node.js and Express
-
61
We recently migrated the @sanity-packages written with Flow to TypeScript . It was an intere...
-
15
In July of this year, we announced “the launch of the Conan 2.0 Tribe” and the plan to move Conan 2.0 forward with the help and feedback of the tribe members. We’re...
-
11
Today we are very excited to announce a new milestone in the Conan project. We have now published our very own self-paced interactive training courses online, free to anyone who is interested in learning Conan. The courses are hosted on JFrog...
-
12
Jumpstart Rails Tutorial 01 - Bringing an Existing Codebase In Jan 2, 2020 Updated: 5/5/2020 It is th...
-
20
cmake-conan CMake wrapper for the Conan C and C++ package manager. This cmake module allows to launch conan install from cmake. The branches in this repo are: develop: PR are me...
-
7
March 29, 2021 #mta Migrating existing applications to Quarkus with Migration Toolkit for Applications ...
-
11
Migrating an existing Next.js project to Netlify May 5 Originally published at
-
4
Blog Post Migrating Our Web Codebase from Flow to TypeScript In summer 2021, we migrated our codebase from Flow to TypeScript. There were many reasons for doing this, but the biggest ones were TypeS...
-
8
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK