安装步骤
安装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 git@github.com: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})