Skip to main content

Bootstrapping a cmake project based on Hunter in Linux and Windows

This is the third post in my exploration of the package managers for C++ in Linux and Windows environments. This time I tested out the Hunter package manager with the same toy C++ program with header-only boost-core and boost-optional and boost-filesystem (linking necessary) dependencies.

The previous blog posts in this series were about 1) a simplistic use of vcpkg from cmake and 2) a little more sophisticated use of vcpkg with cmake. The examples work for both Linux and Windows environments.

Recap

The following is a barebones C++ cmake project hunter_test
hunter_test
├── CMakeLists.txt
├── include
│   └── driver.h
├── src
│   └── driver.cpp
└── test
    └── driver_test.cpp

3 directories, 4 files
The driver.cpp and driver_test.cpp files have just a main function that does nothing. driver.h is empty. The CMakeLists.txt looks as follows.
cmake_minimum_required (VERSION 3.12)

project (vcpkg_test CXX)
set(CMAKE_CXX_STANDARD 17)

add_executable(driver src/driver.cpp)
target_include_directories(driver PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(driver ${Boost_LIBRARIES})

enable_testing()
include(CTest)
add_executable(driver_test ${PROJECT_SOURCE_DIR}/test/driver_test.cpp)
add_test(NAME driver COMMAND driver_test)
The CMakeLists.txt so far is independent of any package managers. Now, we'll start using Hunter.

Adding Hunter Package Manager to Your Cmake Project

This step is ridiculously simple. Hunter documentation provides step-by-step instructions. I really appreciate that.
include("cmake/HunterGate.cmake")
HunterGate(URL "https://github.com/ruslo/hunter/archive/v0.23.165.tar.gz"
           SHA1 "5a73f91df5f6109c0bb1104d0c0ee423f7bece79")

project (hunter_test CXX)
hunter_add_package(Boost COMPONENTS filesystem)
find_package(Boost 1.67 CONFIG REQUIRED COMPONENTS filesystem)
Note that an additional cmake file HunterGate.cmake is added to the project. It lives under the cmake directory. It's the entry-point for Hunter. Including that file in our CMakeLists.txt allows us to call HunterGate with a tar-ball for downloading the Hunter release. You can find the latest release URL and SHA1 under ruslo/Hunter github releases.

This is all that's needed to jumpstart the project in Linux environment. Invoking cmake -B build triggers Hunter in action. It downloads the necessary dependency sources and compiles them for you under $HOME/.hunter. As long as the code's include directives are following the best practices, the code will build smoothly. Specifically, I'm talking about the following two lines. Note the boost prefix.
#include "boost/core/demangle.hpp"
#include "boost/filesystem.hpp"

Hunter in Visual Studio 2019

There are two ways you can use Visual Studio with Hunter.

First, open the CMakeLists.txt in hunter_test in VS via File-->Open-->CMake...-->Select CMakeList.txt. This works but there's a caveat. The output of cmake is not redirected to the VS Build Output window immediately. In fact, it could take several minutes (or even hours). Obviously, it appears like VS is stuck. In the hunter_test project above with boost-core, boost-optional, and boost-filesystem dependencies, took about 21 minutes on my VM to finish the cmake generation step. This issue is tracked on the VS Developer Community.
Recall that Hunter downloads dependency sources and builds them under the directory pointed by $HUNTER_ROOT. In my test runs, no output for this process was captured by Visual Studio. If Hunter fails to download and compile the sources for some dependencies, you might see some output.

I learned later that verbose cmake output can be enabled in Visual Studio. I found the output to be extra verbose due to %VSCMD_DEBUG% == 5.

Classic Visual Studio Solution Generator

An alternative to Visual Studio's built-in cmake support is to use the classic solution file generator in cmake: cmake -B build. The generated solution file has the classic layout of various targets including ALL_BUILD, RUN_TESTS, ZERO_CHECK, and the main targets driver and driver_test. It builds fine. The tests run file. Here's the output of generating the build files.

Conclusion

Visual Studio CMake integration works for Hunter-based projects. It has a few rough edges though. For now, you may be better off using cmake's native project generation for now rather than Visual Studio's "Open CMake Project" feature. You can still use the feature but remember that if one of your colleagues updates the HunterGate URL and SHA1 and if you open the project using the "Open CMake Project" feature, you might have to wait minutes to hours depending upon the number of dependencies rebuilt.

Comments

Popular Content

Unit Testing C++ Templates and Mock Injection Using Traits

Unit testing your template code comes up from time to time. (You test your templates, right?) Some templates are easy to test. No others. Sometimes it's not clear how to about injecting mock code into the template code that's under test. I've seen several reasons why code injection becomes challenging. Here I've outlined some examples below with roughly increasing code injection difficulty. Template accepts a type argument and an object of the same type by reference in constructor Template accepts a type argument. Makes a copy of the constructor argument or simply does not take one Template accepts a type argument and instantiates multiple interrelated templates without virtual functions Lets start with the easy ones. Template accepts a type argument and an object of the same type by reference in constructor This one appears straight-forward because the unit test simply instantiates the template under test with a mock type. Some assertion might be tested in...

Covariance and Contravariance in C++ Standard Library

Covariance and Contravariance are concepts that come up often as you go deeper into generic programming. While designing a language that supports parametric polymorphism (e.g., templates in C++, generics in Java, C#), the language designer has a choice between Invariance, Covariance, and Contravariance when dealing with generic types. C++'s choice is "invariance". Let's look at an example. struct Vehicle {}; struct Car : Vehicle {}; std::vector<Vehicle *> vehicles; std::vector<Car *> cars; vehicles = cars; // Does not compile The above program does not compile because C++ templates are invariant. Of course, each time a C++ template is instantiated, the compiler creates a brand new type that uniquely represents that instantiation. Any other type to the same template creates another unique type that has nothing to do with the earlier one. Any two unrelated user-defined types in C++ can't be assigned to each-other by default. You have to provide a...

Multi-dimensional arrays in C++11

What new can be said about multi-dimensional arrays in C++? As it turns out, quite a bit! With the advent of C++11, we get new standard library class std::array. We also get new language features, such as template aliases and variadic templates. So I'll talk about interesting ways in which they come together. It all started with a simple question of how to define a multi-dimensional std::array. It is a great example of deceptively simple things. Are the following the two arrays identical except that one is native and the other one is std::array? int native[3][4]; std::array<std::array<int, 3>, 4> arr; No! They are not. In fact, arr is more like an int[4][3]. Note the difference in the array subscripts. The native array is an array of 3 elements where every element is itself an array of 4 integers. 3 rows and 4 columns. If you want a std::array with the same layout, what you really need is: std::array<std::array<int, 4>, 3> arr; That's quite annoying for...