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管理,加油!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM