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.
This is all that's needed to jumpstart the project in Linux environment. Invoking
First, open the
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.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.
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 projecthunter_test
hunter_test ├── CMakeLists.txt ├── include │ └── driver.h ├── src │ └── driver.cpp └── test └── driver_test.cpp 3 directories, 4 filesThe
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.
Comments