conan2 基础入门(03)-使用(msvc为例)

conan2 基础入门(03)-使用(msvc为例)

文章目录

⭐准备

在阅读和学习本文前,希望有一定的cmake基础。

cmake基础:

(CMake) 从下载到构建第一个CMake应用

(CMake) 库的生成和链接
Build a simple CMake project using Conan --- conan 2.3.0 documentation

在conan官网中有简单的示例教程。

生成profile文件

在使用conan前,需要先准备一个profile文件。下面指令会自动生成默认的。

shell 复制代码
# 生成默认profile文件,名字为`default`
# --force 表示强制生成,即若原来有`default`会被覆盖
# --name 表示指定生成名称
conan profile detect
conan profile detect --force
conan profile detect --name <指定名称>

# 查看名为`default`的profile文件的路径
conan profile path default
conan profile path <名称>

# 查看已经存在的eprofile
conan profile list

在笔者测试机(装有vs2019)上会出现如下显示。其实就是一个.ini格式的配置文件(但并非完全的ini,conan有自己的特殊处理)。

shell 复制代码
detect_api: Found msvc 16

Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=msvc
compiler.cppstd=14
compiler.runtime=dynamic
compiler.version=192
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\lotus\.conan2\profiles\default
shell 复制代码
C:.
│  global.conf
│  settings.yml
│  version.txt
│
├─extensions
│  └─plugins
│      │  profile.py
│      │
│      └─compatibility
│              compatibility.py
│              cppstd_compat.py
│
└─profiles
        default

预备文件和Code

文件名预览

shell 复制代码
:.
│  CMakeLists.txt
│  conanfile.txt
└─ main.cpp

main.cpp

cpp 复制代码
#include <iostream>

#include "zlib.h"

void test_env() {
    std::cout << ">>>" << __func__ << std::endl;
    std::cout << "sizeof(void*) = " << sizeof(void *) << std::endl;
#if defined(__VERSION__)
    std::cout << "__VERSION__ = " << __VERSION__ << std::endl;
#elif defined(_MSC_VER)
    std::cout << "_MSC_VER = " << _MSC_VER << std::endl;
#endif
}

void test_zlib(void) {
    std::cout << ">>>" << __func__ << std::endl;

    char buffer_in[256] = {
        "Conan is a MIT-licensed, Open Source package manager for C and C++ development"
        "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());
}

int main(void) {
    test_env();
    test_zlib();
}

CMakeLists.txt

cmake 复制代码
cmake_minimum_required(VERSION 3.15)
project(mydemo CXX)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

find_package(ZLIB REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)

conanfile.txt

ini 复制代码
[requires]
zlib/1.3.1

[generators]
CMakeDeps
CMakeToolchain

⭐使用

指令预览

流程化指令,run.bat

如果你和上文中的代码和配置文件编写一致,请无脑直接操作以下命令。

bat 复制代码
conan install . --output-folder=build --build=missing

cd build

cmake .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"

cmake --build . --config Release

cd ../bin/Release

mydemo.exe

正确执行结果

如果操作都没问题,则会出现下面的结果。

生成一个CMakeUserPresets.json文件,buildbin文件夹。并成功生成可执行文件mydemo.exe文件。

shell 复制代码
# mydemo.exe 执行结果
>>>test_env
sizeof(void*) = 8
_MSC_VER = 1929
>>>test_zlib
Uncompressed size is: 231
Compressed size is: 19
ZLIB VERSION: 1.3.1

可能出现的问题

下面讲一些可能出现的问题,因为笔者是比较顺利的完成的,这里将的都是一些基于经验的猜测。

  • 没有正确配置好conan环境
  • 没有生成profile文件
  • profile-default中的配置版本太低,无法正常支持本文示例中的库
  • profile文件中的配置与cmake指令中的配置不一致
    • 如指定编译器不同
    • 指定debug还是release模式不同
  • 相关路径错误
  • 你使用的环境与笔者不同,而你直接cv笔者的环境,指定代码等

关于cmake指定编译器,可以通过help指令查看,一般在Generators下,前面有*的是cmake下的默认编译器。

(CMake) 指定生成器 generator

shell 复制代码
cmake --help
...
...
Generators

The following generators are available on this platform (* marks default):
  Visual Studio 17 2022        = Generates Visual Studio 2022 project files.
                                 Use -A option to specify architecture.
* Visual Studio 16 2019        = Generates Visual Studio 2019 project files.
                                 Use -A option to specify architecture.
  Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
                                 Optional [arch] can be "Win64" or "ARM".
...
...

⭐具体讲解

关于上文中,文件和代码配置的相关信息可以参照官网的提示编写。

zlib - Conan 2.0: C and C++ Open Source Package Manager

conan

conanfile.txt

ini 复制代码
[requires]
zlib/1.3.1

[generators]
CMakeDeps
CMakeToolchain

[requires]

section is where we declare the libraries we want to use in the project

表示需要获取的库。

我们可以在官网查看是否有该包,和包的版本。也可以通过search指令来查看。

shell 复制代码
# conan search <库名>
conan search zlib

Found 6 pkg/version recipes matching zlib in conancenter
conancenter
  zlib
    zlib/1.2.8
    zlib/1.2.11
    zlib/1.2.12
    zlib/1.2.13
    zlib/1.3
    zlib/1.3.1

[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.

用于告诉conan编译或构建当前项目所需要的依赖。

CMakeDeps :生成关于Zlib库文件安装位置的信息

CMakeToolchain :传递构建信息到CMake 使用CMake工具链文件。

简单说这就是为了配合cmake使用。

执行 install

shell 复制代码
conan install . --output-folder=build --build=missing
  • --output-folder=build 表示conan的生成文件放置的目录。
  • --build=missing 表示conan在安装过程中构建任何缺失的依赖项。

该指令会自动根据profile文件安装下载conanfile.txt指定的库。并生成build文件夹。具体的还会生成CMakeUserPresets.json文件,不过这个不是重点。

具体的,执行完后会出现下列文件。

可见却是针对zlib库做了很多对应的操作,而其中最重要的是名为conan_toolchain.cmake的文件。

而实际的zlib库则是下载到.conan2/p文件夹中。(注意,在默认生成的profile中是下载的静态库)

如果你只是单纯的下载一个库,到这里其实就可以结束了。

cmake

而接下来都是cmake的操作了。

CMakeLists.txt

下面看两个cmake的重点语句。

cmake 复制代码
find_package(ZLIB REQUIRED)

target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)

其实在上文已经提到,在官网中正对ZILB库要求分别这样写查找方式和链接方式

生成项目

关于指定编译器问题,已经上面提到了。

这里的重点在于指定cmake_工具链_文件也就是上面提到的conan_toolchain.cmake

shell 复制代码
cmake .. -G "Visual Studio 16 2019" -DCMAKE_TOOLCHAIN_FILE="conan_toolchain.cmake"

conan_toolchain.cmake

我们查看以下该文件,可以看到下面的重要信息。

可见,这里自动帮我们找到了对应库的头文件和库文件。

cmake 复制代码
# ...

# Definition of CMAKE_PREFIX_PATH, CMAKE_XXXXX_PATH
# The Conan local "generators" folder, where this toolchain is saved.
list(PREPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_LIST_DIR} )
list(PREPEND CMAKE_LIBRARY_PATH "C:/Users/lotus/.conan2/p/zlibbe5a5af71d5cc/p/lib")
list(PREPEND CMAKE_INCLUDE_PATH "C:/Users/lotus/.conan2/p/zlibbe5a5af71d5cc/p/include")

# ...

对应的实际文件为:

构建

该操作很简单,就是注意下和profile文件中指定的是否是一致的release/debug。

shell 复制代码
cmake --build . --config Release

至此,就生成了我们的目标可执行文件。




END

辅助脚本

bat 复制代码
@REM 辅助清除脚本

del CMakeUserPresets.json

rmdir /s /q build

rmdir /s /q bin

关注我,学习更多C/C++,算法,计算机知识

B站:

👨‍💻主页:天赐细莲 bilibili

相关推荐
Fairy_sevenseven5 分钟前
【二十八】【QT开发应用】模拟WPS Tab
开发语言·qt·wps
_GR11 分钟前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
蜡笔小新星12 分钟前
Python Kivy库学习路线
开发语言·网络·经验分享·python·学习
凯子坚持 c13 分钟前
C语言复习概要(三)
c语言·开发语言
无限大.24 分钟前
c语言200例 067
java·c语言·开发语言
余炜yw26 分钟前
【Java序列化器】Java 中常用序列化器的探索与实践
java·开发语言
无限大.27 分钟前
c语言实例
c语言·数据结构·算法
篝火悟者27 分钟前
问题-python-运行报错-SyntaxError: Non-UTF-8 code starting with ‘\xd5‘ in file 汉字编码问题
开发语言·python
Death20030 分钟前
Qt 中的 QListWidget、QTreeWidget 和 QTableWidget:简化的数据展示控件
c语言·开发语言·c++·qt·c#
六点半88831 分钟前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法