zoukankan      html  css  js  c++  java
  • conan使用(三)--打包只有头文件的库

    参考:https://docs.conan.io/en/latest/howtos/header_only.html?highlight=header%20only

    对于只含头文件的库打包非常简单,以rapidjson为例。

    执行创建命令:

    conan new rapidjson/1.1.0
    

    然后修改生成的conanfile.py:

    # -*- coding: UTF-8 -*-
    
    import os
    
    from conans import ConanFile, tools
    
    
    class RapidjsonConan(ConanFile):
        name = "rapidjson"
        version = "1.1.0"
        license = "MIT"
        author = "Tencent"
        url = "https://github.com/Tencent/rapidjson.git"
        description = "A fast JSON parser/generator for C++ with both SAX/DOM style API "
        topics = ("JSON")
        no_copy_source = True
        # 如果是先准备好源码,可以直接使用exports_sources而不要用source方法
        # exports_sources = "include/*"
        _source_path = "rapidjson"
    
        def source(self):
            '''retrieval of the source code here. Remember you can also put the code
            in the folder and use exports instead of retrieving it with this
            source() method
            '''
            # self.run("git clone ...") or
            # tools.download("url", "file.zip")
            # tools.unzip("file.zip" )
            self.run("git clone -b version1.1.0 https://github.com/Tencent/rapidjson.git")
    
        def package(self):
            # 只需要include中的文件
            include_folder = os.path.join(self._source_path, "include")
            self.copy(pattern="license.txt", dst="license", src=self._source_path)
            self.copy(pattern="*", dst="include", src=include_folder)
    
        
        def package_id(self):
            self.info.header_only()
    

    如果是手动准备源码,请首先从GitHub上下载rapidjson源码:

    git clone -b version1.1.0 https://github.com/Tencent/rapidjson.git
    

    确保conanfile.py和rapidjson目录在同一级。

    然后执行:

    conan create . tencent/stable
    

    不报错误的话,就会生成成功,在系统conan缓存目录下就可以发现已经生成好了包:
    image.png
    最后将生成的包上传到服务器:

    conan upload rapidjson/1.1.0@tencent/stable -r develope --all
    

    查询服务器仓库上的包,表明已经上传成功:
    image.png
    接下来我们在Ubuntu下写个测试程序测试一下看是否能成功使用。

    首先在工程目录下写一个conanfile.txt:

    [requires]
    rapidjson/1.1.0@tencent/stable
    
    [imports]
    include, * -> ./include
    

    因为只是引入一个纯头文件库,所以配置写的很简单。imports是将库中include目录下的所有文件拷贝到编译目录的include目录下。
    然后创建源代码main.cpp,吧rapidjson官网的例子拷贝过来:

    // rapidjson/example/simpledom/simpledom.cpp`
    #include "rapidjson/document.h"
    #include "rapidjson/writer.h"
    #include "rapidjson/stringbuffer.h"
    #include <iostream>
     
    using namespace rapidjson;
     
    int main() {
        // 1. 把 JSON 解析至 DOM。
        const char* json = "{"project":"rapidjson","stars":10}";
        Document d;
        d.Parse(json);
     
        // 2. 利用 DOM 作出修改。
        Value& s = d["stars"];
        s.SetInt(s.GetInt() + 1);
     
        // 3. 把 DOM 转换(stringify)成 JSON。
        StringBuffer buffer;
        Writer<StringBuffer> writer(buffer);
        d.Accept(writer);
     
        // Output {"project":"rapidjson","stars":11}
        std::cout << buffer.GetString() << std::endl;
        return 0;
    }
    

    然后编写CMakeLists.txt:

    project(rapidjsonTest)
    cmake_minimum_required(VERSION 2.8.12)
    
    include_directories(${PROJECT_BINARY_DIR}/include)
    
    add_executable(${PROJECT_NAME} main.cpp)
    

    这样,我们创建一个build目录,来进行编译:
    image.png
    进入build目录执行:

    conan isntall ..
    cmake ..
    make
    
    ./rapidjsonTest
    

    image.png
    表明引入成功。

    这里补充一个conan使用技巧,就是默认搜索库的顺序是先搜索它先先加入的conan-cnter,没有的话才会搜索我们后来加入的develope库,但是实际应用中我们肯定期望优先搜索我们自己的库,因此应该改变搜索顺序。方法就是找到conan缓存目录(一般就是用户根目录下的.conan文件),下面有一个remotes.json,修改里面库的顺序即可。

    下一步计划是将chromium-base库打包实现conan管理,加油!

  • 相关阅读:
    工具类-ApplicationContextUtil
    银行联行号-全国地区码
    银行联行号-银行编码(联行号前3位)
    前端防止 JS 调试技巧
    Vue基础框架
    关于5G手机使用4G套餐扫盲
    nginx高级-前端必会
    懒人npm运行和打包命令
    关于虚拟专用网络的一些经验
    JS加密解密
  • 原文地址:https://www.cnblogs.com/xl2432/p/11901089.html
Copyright © 2011-2022 走看看