Conan2: starting at a text book example

本文介绍了如何在C++项目中使用Conan包管理器来安装和依赖ZLib库,包括创建conanfile.txt、配置CMake和生成必要的构建文件。开发者可以从远程服务器获取预编译的二进制文件,简化开发过程。
摘要由CSDN通过智能技术生成

“From using libraries already packaged by Conan, to how to package your libraries and store them in a remote server alongside all the precompiled binaries.”

  1. Clone the sources of this example project from github, and open the simple_cmake_project directory:
> git clone https://github.com/conan-io/examples2.git
> cd examples2/tutorial/consuming_packages/simple_cmake_project

E:\XXXX\examples2\tutorial\consuming_packages\simple_cmake_project> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 EXXX-XXXX
E:.
|   CMakeLists.txt
|   conanfile.txt
|
+---build
\---src
        main.c

main.c

#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", strlen(buffer_out));

    printf("ZLIB VERSION: %s\n", zlibVersion());

    return EXIT_SUCCESS;
}

CMakeLists.txt

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)

  1. conanfile.txt

This application relies on the Zlib library. Conan, by default, tries to install libraries from a remote server called ConanCenter. One can search there for libraries and also check the available versions. In this case, after checking the available versions for Zlib one of the latest versions: zlib/1.2.11 is chosed.
The easiest way to install the Zlib library and find it from this project with Conan is using a conanfile.txt file:

[requires]
zlib/1.2.11

[generators]
CMakeDeps
CMakeToolchain

[requires] section is where we declare the libraries we want to use in the project, in this case, zlib/1.2.11.
[generators] section tells Conan to generate the files that the compilers or build systems will use to find the dependencies and build the project. In this case, as our project is based in CMake, we will use:
CMakeDeps to generate information about where the Zlib library files are installed and
CMakeToolchain to pass build information to CMake using a CMake toolchain file.

  1. Conan profile
    Besides the conanfile.txt, we need a Conan profile to build our project.
    Conan profiles allow users to define a configuration set for things like the compiler, build configuration, architecture, shared or static libraries, etc.
    Conan, by default, will not try to detect a profile automatically, so we need to create one.
    To let Conan try to guess the profile, based on the current operating system and installed tools, please run:
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>conan profile detect --force
detect_api: Found msvc 17

Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=193
os=Windows

WARN: This profile is a guess of your environment, please check it.
WARN: The output of this command is not guaranteed to be stable and can change in future Conan versions.
WARN: Use your own profile files for stability.
Saving detected profile to C:\Users\XXXXX\.conan2\profiles\default

This will detect the operating system, build architecture and compiler settings based on the environment.
It will also set the build configuration as Release by default.
The generated profile will be stored in the Conan home folder with name default and will be used by Conan in all commands by default unless another profile is specified via the command line.

  1. conan install
    Use Conan to install Zlib and generate the files that CMake needs to find this library and build project. Those files will be generated in the folder build:
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>conan install . --output-folder=build --build=missing

======== Input profiles ========
Profile host:
[settings]
arch=x86_64
...
...
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project> tree /a /f
卷 文档 的文件夹 PATH 列表
卷序列号为 XXXX-XXXX
E:.
|   CMakeLists.txt
|   CMakeUserPresets.json
|   conanfile.txt
|
+---build
|       cmakedeps_macros.cmake
|       CMakePresets.json
|       conanbuild.bat
|       conanbuildenv-release-x86_64.bat
|       conandeps_legacy.cmake
|       conanrun.bat
|       conanrunenv-release-x86_64.bat
|       conan_toolchain.cmake
|       deactivate_conanbuild.bat
|       deactivate_conanrun.bat
|       FindZLIB.cmake
|       module-ZLIB-release-x86_64-data.cmake
|       module-ZLIB-Target-release.cmake
|       module-ZLIBTargets.cmake
|       ZLIB-release-x86_64-data.cmake
|       ZLIB-Target-release.cmake
|       ZLIBConfig.cmake
|       ZLIBConfigVersion.cmake
|       ZLIBTargets.cmake
|
\---src
        main.c

• Conan installed the Zlib library from the remote server, which should be the Conan Center server by default if the library is available. This server stores both the Conan recipes, which are the files that define how libraries must be built, and the binaries that can be reused so we don’t have to build from sources every time.

• Conan generated several files under the build folder. Those files were generated by both the CMakeToolchain and CMakeDeps generators we set in the conanfile.txt. CMakeDeps generates files so that CMake finds the Zlib library we have just downloaded. On the other side, CMakeToolchain generates a toolchain file for CMake so that we can transparently build our project with CMake using the same settings that we detected for our default profile.

  1. build and run compressor app
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project>cd build

E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cmake .. -G "Visual Studio 17 2022" -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
-- Using Conan toolchain: E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/build/conan_toolchain.cmake
-- Conan toolchain: CMAKE_GENERATOR_TOOLSET=v143
-- Conan toolchain: C++ Standard 14 with extensions OFF
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.36.32532.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.36.32532/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Conan: Target declared 'ZLIB::ZLIB'
-- Configuring done (4.0s)
-- Generating done (0.0s)
-- Build files have been written to: E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/build

E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cmake --build . --config Release
MSBuild version 17.6.3+07e294721 for .NET Framework

  Checking Build System
  Building Custom Rule E:/XXXXX/examples2/tutorial/consuming_packages/simple_cmake_project/CMakeLists.txt
  main.c

E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build>cd Release
E:\XXXXX\examples2\tutorial\consuming_packages\simple_cmake_project\build\Release>compressor.exe
Uncompressed size is: 207
Compressed size is: 19
ZLIB VERSION: 1.2.11


References

  1. Conan Documentation --Release 2.0.17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值