环境
ubuntu:bionic的docker image
docker run -it ubuntu:bionic
预装工具
apt-get install cmake
安装conan
# pip3 install conan
Collecting conan
# conan -v
Conan version 1.33.0
使用conan
官方文档:https://docs.conan.io/en/latest/getting_started.html
搜索包
# conan search poco --remote=conan-center
WARN: Remotes registry file missing, creating default one in /root/.conan/remotes.json
Existing package recipes:
poco/1.8.1 <<< 已有的版本
poco/1.9.3
poco/1.9.4
poco/1.10.0
poco/1.10.1
导入包
# conan inspect poco/1.9.4
poco/1.9.4: Not found in local cache, looking in remotes...
poco/1.9.4: Trying with 'conan-center'...
Downloading conanmanifest.txt completed [0.59k]
Downloading conanfile.py completed [12.59k]
Downloading conan_export.tgz completed [0.30k]
Decompressing conan_export.tgz completed [0.00k]
poco/1.9.4: Downloaded recipe revision 0
name: poco
version: 1.9.4
url: https://github.com/conan-io/conan-center-index
homepage: https://pocoproject.org
license: BSL-1.0
编译
# conan profile new default --detect
Found gcc 7
Found clang 6.0
gcc>=5, using the major as version
************************* WARNING: GCC OLD ABI COMPATIBILITY ***********************
Conan detected a GCC version > 5 but has adjusted the 'compiler.libcxx' setting to
'libstdc++' for backwards compatibility.
Your compiler is likely using the new CXX11 ABI by default (libstdc++11).
If you want Conan to use the new ABI for the default profile, run:
$ conan profile update settings.compiler.libcxx=libstdc++11 default
Or edit '/root/.conan/profiles/default' and set compiler.libcxx=libstdc++11
************************************************************************************
# conan profile update settings.compiler.libcxx=libstdc++11 default
准备三个文件放在同一个目录如:conan
中
conan$ ls
CMakeLists.txt conanfile.txt md5.cpp
文件内容:
conan$ cat conanfile.txt
[requires]
poco/1.9.4
[generators]
cmake
conan$ cat 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$ cat 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})
conan编译
conan# mkdir build && cd build
build# conan install ..
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=gcc
compiler.libcxx=libstdc++11
compiler.version=7
os=Linux
os_build=Linux
conanfile.txt: Generated conaninfo.txt
conanfile.txt: Generated graphinfo
build# cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Configuring done
-- Generating done
-- Build files have been written to: /root/build
build# cmake --build .
Scanning dependencies of target md5
[ 50%] Building CXX object CMakeFiles/md5.dir/md5.cpp.o
[100%] Linking CXX executable bin/md5
[100%] Built target md5
build# ls
CMakeCache.txt Makefile cmake_install.cmake conanbuildinfo.cmake conaninfo.txt
CMakeFiles bin conan.lock conanbuildinfo.txt graph_info.json
build# ./bin/md5
c3fcd3d76192e4007dfb496cca67e13b
环境完成!
打包项目
官网打包教程:https://docs.conan.io/en/latest/creating_packages/getting_started.html
下面要解决的问题是conanfile.txt中的conan包是怎么生成的。按照官网的打包步骤走了一遍,把源码打包成一个conan包。假设我们要将一个hello的源码打包成一个conan包
准备源码
# mkdir mypkg && cd mypkg
//这里会从github上下载hello的程序
/mypkg# conan new hello/0.1 -t
编译成conan包
方法1:
/mypkg# conan create . demo/testing
[100%] Built target example
hello/0.1@demo/testing (test package): Running test()
Hello World Release! <<< 执行hello程序
方法2:
上面的编译步骤等价于
/mypkg# conan export . demo/testing
/mypkg# conan install hello/0.1@demo/testing --build=hello
# package is created now, use test to test it
/mypkg# conan test test_package hello/0.1@demo/testing
hello/0.1@demo/testing (test package): Running test()
Hello World Release! <<< 执行hello程序
查看生成的包
# conan search hello
Existing package recipes:
hello/0.1@demo/testing
说明已经将hello的程序源码打包成了conan包。