Package_managers
Package Managers
Section titled “Package Managers”Package managers simplify C++ dependency management by handling downloading, building, and linking external libraries. This chapter covers the most popular options.
Why Package Managers?
Section titled “Why Package Managers?”Manual dependency management is painful:
- Finding and downloading libraries
- Resolving transitive dependencies
- Building libraries for different platforms
- Managing versions and conflicts
Package managers solve all of these problems.
The most popular C++ package manager.
Installation
Section titled “Installation”# Using pip (recommended)pip install conan
# Verify installationconan --versionBasic Usage
Section titled “Basic Usage”conanfile.txt
Section titled “conanfile.txt”[requires]boost/1.80.0nlohmann_json/3.10.5poco/1.12.0
[generators]cmake_find_packagecmake_paths
[options]boost:shared=Falsepoco:shared=Falseconanfile.py (More Advanced)
Section titled “conanfile.py (More Advanced)”from conans import ConanFile, CMake, tools
class MyProject(ConanFile): name = "myproject" version = "1.0.0" settings = "os", "compiler", "build_type", "arch" exports_sources = "CMakeLists.txt", "src/*" generators = "cmake", "cmake_find_package"
def requirements(self): self.requires("boost/1.80.0") self.requires("nlohmann_json/3.10.5")
def build(self): cmake = CMake(self) cmake.configure() cmake.build()
def package(self): self.copy("*.h", dst="include", src="src") self.copy("*.lib", dst="lib", keep_path=False) self.copy("*.dll", dst="bin", keep_path=False) self.copy("*.so", dst="lib", keep_path=False) self.copy("*.dylib", dst="lib", keep_path=False)Workflow
Section titled “Workflow”# Install dependenciesconan install . -s build_type=Release
# Create packageconan create . user/channel
# Search for packagesconan search boost
# List installed packagesconan listUsing with CMake
Section titled “Using with CMake”cmake_minimum_required(VERSION 3.15)project(MyProject)
# Include Conan-generated filesinclude(${CMAKE_BINARY_DIR}/conan_paths.cmake)find_package(Boost REQUIRED)find_package(nlohmann_json REQUIRED)
add_executable(main main.cpp)target_link_libraries(main Boost::boost nlohmann_json::nlohmann_json)Microsoft’s C++ package manager, now officially supported.
Installation
Section titled “Installation”# Clone repositorygit clone https://github.com/microsoft/vcpkg.gitcd vcpkg
# Run bootstrap script./bootstrap-vcpkg.sh # Linux/macOSbootstrap-vcpkg.bat # Windows
# Integrate with Visual Studiovcpkg integrate installBasic Usage
Section titled “Basic Usage”# Search for packagesvcpkg search boost
# Install packagesvcpkg install boost:x64-linuxvcpkg install nlohmann-json:x64-linux
# Install specific versionvcpkg install boost:arm64-linux==1.80.0
# Remove packagevcpkg remove boostTriplet Files
Section titled “Triplet Files”vcpkg uses triplets to specify target platforms:
set(VCPKG_TARGET_ARCHITECTURE x64)set(VCPKG_CRT_LINKAGE dynamic)set(VCPKG_LIBRARY_LINKAGE static)set(VCPKG_CMAKE_CONFIGURATION_MODE "EXTERNAL")# Use custom tripletvcpkg install boost:custom-tripletUsing with CMake
Section titled “Using with CMake”# Configure with vcpkgcmake -B build -S . \ -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake
# For dynamic linkingcmake -B build -S . \ -DCMAKE_TOOLCHAIN_FILE=vcpkg/scripts/buildsystems/vcpkg.cmake \ -DVCPKG_TARGET_TRIPLET=x64-windows-dynamicCMakeLists.txt Integration
Section titled “CMakeLists.txt Integration”cmake_minimum_required(VERSION 3.15)project(MyProject CXX)
find_package(Boost REQUIRED)find_package(nlohmann_json REQUIRED)
add_executable(main main.cpp)target_link_libraries(main PRIVATE Boost::boost nlohmann_json::nlohmann_json)Hunter
Section titled “Hunter”A CMake-driven package manager.
Installation
Section titled “Installation”# In your CMakeLists.txtinclude(HunterGate)HunterGate( URL "https://github.com/cpp-pm/hunter/archive/v0.23.280.tar.gz" SHA1 "abc123...")
project(MyProject)
find_package(Glog REQUIRED)Configuration
Section titled “Configuration”# hunter_config(Glog VERSION 0.6.0)# hunter_config(Boost VERSION 1.80.0)Bazel - Build System with Package Management
Section titled “Bazel - Build System with Package Management”Bazel includes dependency management via repositories.
WORKSPACE File
Section titled “WORKSPACE File”workspace( name = "myproject", managed_directories = {"@": ["EXTERNAL"]},)
http_archive( name = "boost", url = "https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.gz", sha256 = "...", strip_prefix = "boost_1_80_0",)
new_http_archive( name = "nlohmann_json", url = "https://github.com/nlohmann/json/releases/download/v3.10.5/include.zip", build_file = "//third_party:nlohmann_json.BUILD",)CPM.cmake
Section titled “CPM.cmake”A CMake script for dependency management inspired by Conan.
Installation
Section titled “Installation”# Download CPM.cmakewget https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.38.3/cpm.cmakecmake_minimum_required(VERSION 3.14)project(MyProject)
# Include CPMinclude(cpm.cmake)
# Add packagesCPMAddPackage("gh:fmtlib/fmt#10.0.0")CPMAddPackage("gh:nlohmann/json@3.10.5")CPMAddPackage( NAME Boost VERSION 1.80.0 URL https://boostorg.jfrog.io/artifactory/main/release/1.80.0/source/boost_1_80_0.tar.gz SHA256 abc123...)
# Use packagesadd_executable(main main.cpp)target_link_libraries(main PRIVATE fmt::fmt nlohmann_json::nlohmann_json)Hunter vs Conan vs vcpkg Comparison
Section titled “Hunter vs Conan vs vcpkg Comparison”| Feature | Conan | vcpkg | Hunter |
|---|---|---|---|
| Package Count | ~2000 | ~1900 | ~500 |
| Cross-compilation | Excellent | Good | Good |
| Binary caching | Yes | Yes | Yes |
| CMake integration | Excellent | Excellent | Good |
| MSVC support | Excellent | Excellent | Good |
| Linux/macOS | Excellent | Excellent | Good |
| Build system | Python | CMake | CMake |
| Registration | Centered | Decentralized | Centralized |
Best Practices
Section titled “Best Practices”Version Management
Section titled “Version Management”# conanfile.txt - Use version ranges[requires]boost/[>=1.70.0 <2.0.0]nlohmann_json/3.10.5Lock Files
Section titled “Lock Files”# Generate lock fileconan lock create . --lockfile-out=conan.lock
# Use lock fileconan install . --lockfile=conan.lockPrivate Repositories
Section titled “Private Repositories”# Add private Conan serverconan remote add myserver https://myserver.com/artifactory/api/conan/myrepo
# Or use local cacheconan config set storage.path=/path/to/cacheCI/CD Integration
Section titled “CI/CD Integration”# GitHub Actions example- name: Install Conan run: pip install conan
- name: Configure run: conan install . -s build_type=Release
- name: Build run: cmake --build . --config ReleaseCreating Your Own Package
Section titled “Creating Your Own Package”Conan Recipe
Section titled “Conan Recipe”from conans import ConanFile, CMake, tools
class MyLibraryConan(ConanFile): name = "mylib" version = "1.0.0" license = "MIT" settings = "os", "compiler", "build_type", "arch" exports_sources = "CMakeLists.txt", "src/*"
def build(self): cmake = CMake(self) cmake.configure() cmake.build()
def package(self): self.copy("*.h", dst="include", src="include") self.copy("*.lib", dst="lib") self.copy("*.so", dst="lib") self.copy("*.dylib", dst="lib")
def package_info(self): self.cpp_info.libs = ["mylib"]Publish Package
Section titled “Publish Package”# Create packageconan create . user/channel
# Upload to remoteconan upload mylib/1.0.0@user/channel --allKey Takeaways
Section titled “Key Takeaways”- Package managers simplify dependency management
- Conan is most popular for C++ with Python-based recipes
- vcpkg is Microsoft’s official package manager with CMake integration
- CPM.cmake provides simple CMake-based dependency management
- Choose based on your project’s build system and needs
- Consider private registries for proprietary dependencies