包管理器主要用于下载、管理、编译和链接第三方库,许多语言都原生集成包管理器(例如Rust、Python、C#等),各种三方库下载即用。C++语言的一大缺陷就是缺乏原生的包管理器,这导致在项目中引用第三方库十分繁琐,尤其是需要引用大量第三方库的时候。但哪里有问题,哪里就有造轮子的人!vcpkg、Conan等C++包管理器应运而生。
vcpkg是微软开发的C++包管理器,使用非常方便,非常容易和Visual Studio IDE集成;但是vcpkg在安装包的过程中经常需要访问Github资源,对于网络受限的用户来说非常不友好。
为此,笔者投入了Conan阵营,Conan与vcpkg差异较大,三方库提供了多个预编译版本,在安装的过程中往往只需要下载这些预编译的版本即可(vcpkg每次都会下载源码进行编译),所以安装包的过程很迅速。它也提供对CMake和Visual Studio IDE的集成支持。
本文简要介绍Conan包管理器的基本用法。
环境搭建
基于Python的pip包管理器进行安装:
conan命令分为三大类:①使用包命令;②创建包命令;③安全相关命令。我们的重点是如何使用包,这里的install、list、profile、remove、search命令使用较多,尤其是要熟练掌握install命令的用法。
在CMake项目中使用Conan
源文件
在源文件中使用zlib库,看怎么通过Conan引入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <zlib.h> int main (void ) { char buffer_in [256 ] = {"Conan is a MIT-licensed, Open Source package manager for C and C++ development, " "allowing development teams to easily and efficiently manage their packages and " "dependencies across platforms and build systems." }; char buffer_out [256 ] = {0 }; z_stream defstream; defstream.zalloc = Z_NULL; defstream.zfree = Z_NULL; defstream.opaque = Z_NULL; defstream.avail_in = (uInt) strlen (buffer_in); defstream.next_in = (Bytef *) buffer_in; defstream.avail_out = (uInt) sizeof (buffer_out); defstream.next_out = (Bytef *) buffer_out; deflateInit (&defstream, Z_BEST_COMPRESSION); deflate (&defstream, Z_FINISH); deflateEnd (&defstream); printf ("Uncompressed size is: %lu\n" , strlen (buffer_in)); printf ("Compressed size is: %lu\n" , defstream.total_out); printf ("ZLIB VERSION: %s\n" , zlibVersion ()); return EXIT_SUCCESS; }
CMakeLists.txt
CMakeLists.txt文件没有特殊写法,直接调用find_package找到包,然后为目标指定链接即可:
1 2 3 4 5 6 7 cmake_minimum_required(VERSION 3.15) project(compressor C) find_package(ZLIB REQUIRED) add_executable(${PROJECT_NAME} src/main.c) target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
conanfile.txt
conanfile.txt是使用Conan的关键,在这里指明了需要引入的三方库,以及需要与哪种构建工具进行集成。本项目中引入zlib库,与CMake构建工具集成。
1 2 3 4 5 6 [requires] zlib/1.3.1 [generators] CMakeDeps CMakeToolchain
CMakeDeps:表示为每个依赖库生成对应的cmake文件,以便在CMake中通过find_package()使用。
CMakeToolchain:表示生成CMake工具链文件(conan_toolchain.cmake)文件,用于控制编译选项、C++标准等,执行cmake命令时指定该文件即可。
执行命令
1 2 3 4 5 6 7 8 9 10 11 12 @ECHO ON set BASEDIR=%~dp0PUSHD %BASEDIR% RMDIR /Q /S build conan install . --output-folder=build --build=missing cd buildcmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake cmake --build . --config Release Release\compressor.exe
conan install . --output-folder=build:会根据conanfile.txt文件确定要下载的三方库,并且生成相应的cmake文件到--output-folder指定的文件夹下。
--build:指定哪些库从源码编译安装
--build=never:不允许从源码编译安装,只下载预编译版本的包。
--build=missing:如果指定的包没有预编译的版本,那么就从源码编译安装。
--build="*":表示所有包都从源码编译安装,可以通过正则表达式指定。
在Visual Studio中集成Conan包
源文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include <iostream> #include <boost/shared_ptr.hpp> #include "fmt/core.h" #include "Eigen/Core" class Test {public : Test () { std::cout << "Test constructed\n" ; } ~Test () { std::cout << "Test destructed\n" ; } }; int main () { Eigen::Matrix2d mat; mat << 1.0 , 2.0 , 3.0 , 4.0 ; Eigen::VectorXd vec (2 ) ; vec << 5.0 , 6.0 ; Eigen::VectorXd result = mat * vec; fmt::print ("Matrix:\n" ); for (int i = 0 ; i < mat.rows (); ++i) { for (int j = 0 ; j < mat.cols (); ++j) { fmt::print ("{:.2f} " , mat (i, j)); } fmt::print ("\n" ); } fmt::print ("\nVector:\n" ); for (int i = 0 ; i < vec.size (); ++i) { fmt::print ("{:.2f}\n" , vec (i)); } fmt::print ("\nResult of matrix-vector multiplication:\n" ); for (int i = 0 ; i < result.size (); ++i) { fmt::print ("{:.2f}\n" , result (i)); } boost::shared_ptr<Test> ptr1 (new Test()) ; { boost::shared_ptr<Test> ptr2 = ptr1; } return 0 ; }
conanfile.txt
1 2 3 4 5 6 7 8 9 [requires] folly/2024.08.12.00 boost/1.85.0 fmt/10.2.1 eigen/3.4.0 [generators] MSBuildDeps MSBuildToolchain
MSBuildDeps:为每个依赖库生成 .props文件,通过该文件可以将三方库引入项目。
MSBuildToolchain:生成 MSBuild 工具链文件,控制编译环境(如 C++ 标准、运行时库、平台等),通常是conantoolchain.props文件。
安装包
执行以下命令即可生成各种.props文件,为项目添加conandeps.props配置文件即可:
1 conan install . --build=never