Conan在Linux的使用教程(一)

一、使用pip安装

pip install conan
source ~/.profile

注意事项:

  • 确保您的pip安装与您的Python(>= 3.6)版本相匹配
  • Linux中,您可能需要sudo权限才能全局安装 Conan
  • 强烈建议使用虚拟环境(virtualenvwrapper 效果很好)来处理与 Python 相关的所有内容。使用 Python 3,也可以使用内置模块(检查venv — Creation of virtual environments — Python 3.12.4 documentation)。如果不使用虚拟环境,conan 依赖项可能会与先前存在的依赖项发生冲突,特别是当您将 Python 用于其他目的时。

更新

pip install conan --upgrade

二、使用 Conan 构建一个简单的 CMake 项目

结构目录
.
├── CMakeLists.txt
└── src
    └── main.c

该项目包含一个基本的CMakeLists.txt,其中包括zlib依赖项和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 "
                            "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)

本程序依赖Zlib库,默认情况,Conan会从ConanCenter的远程服务器安装库。可以在ConanCenter找到可以的版本,这里使用zlib/1.2.11

然后创建conanfile.txt,语法类似于INI文件

[requires]
zlib/1.2.11

[generators]
CMakeDeps
CMakeToolchain
  • requires: 在项目中使用的库的地方
  • generators:告诉 Conan 生成编译器或构建系统将用来查找依赖项和构建项目的文件

除了conanfile.txt,还需要Conan Config文件,ConanConfig的作用是允许用户为编译器、构建配置、体系结构、共享或静态库等定义一组配置。如何创建呢,

conan profile detect --force  //将根据环境检测操作系统、构建架构和编译器设置

在Linux Ubuntu20.04会输出

detect_api: Found cc=gcc- 8.4.0
detect_api: gcc>=5, using the major as version
detect_api: gcc C++ standard library: libstdc++11

Detected profile:
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu14
compiler.libcxx=libstdc++11
compiler.version=8
os=Linux

获取配置文件的路径,然后修改 compiler.cppstd 的值

conan profile path default
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值