Conan


Conan 包管理器

本文简要介绍 Conan 的用法。

入门

// md5.cpp

#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

#include <iostream>

int main(int argc, char** argv){
	Poco::MD5Engine md5;
	Poco::DigestOutputStream ds(md5);
	ds << "abcdefghijklmnopqrstuvwxyz";
	ds.close();
	std::cout << Poco::DigestEngine::digestToHex(md5.digest()) << std::endl;
	return 0;
}

Conan 远程仓库

搜索 poco。

查看版本信息。conan inspect poco/1.10.1

# conanfile.txt

[requires]
poco/1.10.1

[generators]
cmake
conan profile new default --detect
conan profile update settings.compiler.libcxx=libstdc++11 default
mkdir build
cd build
conan install .. --build=missing
# CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
project(MD5Encrypter)

add_definitions("-std=c++11")

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

add_executable(md5 md5.cpp)
target_link_libraries(md5 ${CONAN_LIBS})
cd build
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release
ninja
cd bin
./md5
# the output is c3fcd3d76192e4007dfb496cca67e13b

添加编译设置。conan --settings compiler="gcc" --settings os="Linux"

conan 的本地缓存位置为~/.conan/data

使用conan search "*"搜索所有本地包。搜索特定包时为conan search apr/1.7.0@

使用conan info .检查当前项目依赖项,注意需要指向conanfile.txt文件所在目录。也可以生成依赖关系图conan info . --graph=file.html

可以在~/.conan/profiles中编写配置文件,如gcc_x86,然后通过conan install .. --profile=gcc_x86来使用。

使用conan remove命令删除包。

可以在conanfile.txt中指定使用动态链接(默认是静态)。

[options]
poco:shared=True
openssl:shared=True

还可以将动态链接库复制到程序所在的目录,使得它们可以被可执行文件找到。

[imports]
bin, *.dll -> ./bin
lib, *.dylib* -> ./bin

注意~/.conan/profiles下的配置文件中也有默认配置内容。比如以下配置。

# clang_3.5

[settings]
 os=Macos
 arch=x86_64
 compiler=clang
 compiler.version=3.5
 compiler.libcxx=libstdc++11
 build_type=Release

 [env]
 CC=/usr/bin/clang
 CXX=/usr/bin/clang++

使用时用命令conan create . demo/testing -pr=clang_3.5调用即可。

可以对不同的构建使用不同的配置并使用不同的构建目录。

conan install md5 -s build_type=Debug -if md5_build_debug
conan install md5 -s build_type=Release -if md5_build_release
cd md5_build_debug && cmake --build . --config Debug && cd ../..
cd md5_build_release && cmake --build . --config Release && cd ../..

如果要对依赖进行调试,则需要在本地编译这些依赖,而不是直接下载二进制包。可以使用conan install <reference> --build <name> --profile <debug_profile>

创建包

mkdir mypkg && cd mypkg
conan new hello/0.1 -t

目录结构如下。

conanfile.py
test_package
	  CMakeLists.txt
	  conanfile.py
	  example.cpp

其中conanfile.py用于定义包。比如。

# conanfile.py

from conans import ConanFile, CMake, tools

class HelloConan(ConanFile):
    name = "hello"
    version = "0.1"
    license = "<Put the package license here>"
    url = "<Package recipe repository url here, for issues about the package>"
    description = "<Description of hello here>"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake"

    def source(self):
        self.run("git clone https://github.com/conan-io/hello.git")
        # This small hack might be useful to guarantee proper /MT /MD linkage
        # in MSVC if the packaged project doesn't have variables to set it
        # properly
        tools.replace_in_file("hello/CMakeLists.txt", "PROJECT(MyHello)",
                              '''PROJECT(MyHello)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()''')

    def build(self):
        cmake = CMake(self)
        cmake.configure(source_folder="hello")
        cmake.build()

        # Explicit way:
        # self.run('cmake %s/hello %s'
        #          % (self.source_folder, cmake.command_line))
        # self.run("cmake --build . %s" % cmake.build_config)

    def package(self):
        self.copy("*.h", dst="include", src="hello")
        self.copy("*hello.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)
        self.copy("*.a", dst="lib", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["hello"]

test_package目录用于测试包。

test_package/conanfile.py是另一个配置。

# test_package/conanfile.py

from conans import ConanFile, CMake
import os

class HelloTestConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    generators = "cmake"

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def imports(self):
        self.copy("*.dll", dst="bin", src="bin")
        self.copy("*.dylib*", dst="bin", src="lib")

    def test(self):
        os.chdir("bin")
        self.run(".%sexample" % os.sep)

创建并测试包conan create . demo/testing。该命令相当于以下命令。

conan export . demo/testing
conan install hello/0.1@demo/testing --build=hello
# package is created now, use test to test it
conan test test_package hello/0.1@demo/testing

移除包与上传包。

conan remove packagename/1.0
conan upload packagename/1.0

文章作者: niuiic
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 niuiic !
评论
  目录