Ubuntu18.04安裝facebook folly庫

安裝步驟

安裝boost
ubuntu18.04安裝的boost有點陳舊,因此自行下載最新版本,執行安裝即可:https://www.boost.org/
具體步驟直接參考readme

安裝gtest

wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz && \
tar zxf release-1.8.0.tar.gz && \
rm -f release-1.8.0.tar.gz && \
cd googletest-release-1.8.0 && \
cmake . && \
make && \
make install

安裝一些必要的組件:

sudo apt-get install \
    g++ \
    cmake \
    libevent-dev \
    libdouble-conversion-dev \
    libgoogle-glog-dev \
    libgflags-dev \
    libiberty-dev \
    liblz4-dev \
    liblzma-dev \
    libsnappy-dev \
    make \
    zlib1g-dev \
    binutils-dev \
    libjemalloc-dev \
    libssl-dev \
    pkg-config \
    libunwind-dev

注意,如果之前通過apt方式安裝過其他的glog,請提前卸載,否則可能出現衝突。

安裝fmt庫:

git clone https://github.com/fmtlib/fmt.git && cd fmt

mkdir _build && cd _build
cmake ..

make -j$(nproc)
sudo make install

安裝一些高級功能調試用的庫,非必須:

sudo apt-get install \
    libunwind8-dev \
    libelf-dev \
    libdwarf-dev

安裝folly:

git clone [email protected]:facebook/folly.git
cd folly

mkdir _build && cd _build
cmake ..
make -j $(nproc)
sudo make install # 可以自定義安裝目錄,默認是/usr/local/下

編譯執行

因爲folly本身依賴項比較多,這裏以一個簡單的Future爲例子,說明具體的使用方式。

首先給出代碼1.cc

#include <folly/futures/Future.h>
#include <folly/executors/ThreadedExecutor.h>
#include <iostream>
using namespace folly;
using namespace std;

void foo(int x) {
    // do something with x
    cout << "foo(" << x << ")" << endl;
}

int main() {
    // ...
    folly::ThreadedExecutor executor;
    cout << "making Promise" << endl;
    Promise<int> p;
    Future<int> f = p.getSemiFuture().via(&executor);
    auto f2 = move(f).thenValue(foo);
    cout << "Future chain made" << endl;

    // ... now perhaps in another event callback

    cout << "fulfilling Promise" << endl;
    p.setValue(42);
    move(f2).get();
    cout << "Promise fulfilled" << endl;
    return 0;
}

給出編譯的CMake文件,具體的CMake的用法,可以參考博客:

  • https://blog.csdn.net/qq_35976351/article/details/104453888
  • https://blog.csdn.net/qq_35976351/article/details/106866808
  • http://www.yeolar.com/note/2014/12/16/cmake-how-to-find-libraries/
  • https://zhenshenglee.github.io/2016/10/05/161005-CMakeFind%E6%A8%A1%E5%9D%97/
cmake_minimum_required(VERSION 3.7)

find_package(folly REQUIRED)
find_package(Threads REQUIRED)
find_package(gflags REQUIRED)

set(CMAKE_CXX_STANDARD 14)

set_and_check(FOLLY_INCLUDE_DIR /usr/local/include/folly)
set_and_check(FOLLY_CMAKE_DIR /usr/local/lib/cmake/folly)
if (NOT TARGET Folly::folly)
    include("${FOLLY_CMAKE_DIR}/folly-targets.cmake")
endif()

set(FOLLY_LIBRARIES Folly::folly)

if (NOT folly_FIND_QUIETLY)
    message(STATUS "Found folly: ${PACKAGE_PREFIX_DIR}")
endif()

add_executable(main 1.cc)
target_link_libraries(main ${FOLLY_LIBRARIES})
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章