Folly 官方地址:https://github.com/facebook/folly。
folly 依赖格式化库fmt
,需手工安装:
cd ~/3rd;
git clone https://github.com/fmtlib/fmt.git && cd fmt
rm _build -rf; mkdir _build -p && cd _build
cmake .. -DCMAKE_INSTALL_PREFIX=/opt/ -DBUILD_SHARED_LIBS=TRUE
make -j$(nproc)
sudo make install
下面脚本下载库文件到~/3rd
,编译安装到/opt/include
和/opt/lib
。
cd ~/3rd;
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 unzip -ytou
wget https://github.com/facebook/folly/archive/master.zip -O folly.zip
unzip folly.zip
cd folly-main
rm _build -rf
mkdir _build && cd _build
cmake .. "-DCMAKE_INCLUDE_PATH=/opt/include" "-DCMAKE_LIBRARY_PATH=/opt/lib" -DCMAKE_INSTALL_PREFIX=/opt/ -DFOLLY_SUPPORT_SHARED_LIBRARY=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON
make -j
make install # /opt
folly
编译后只有一个libfolly.a
库文件,该文件不能用于构建SO
文件,会报类似于下面的错误:
/usr/bin/ld: /opt/lib/libfolly.a(F14Table.cpp.o): relocation R_X86_64_TPOFF32 against `_ZZN5folly3f146detail21tlsPendingSafeInsertsElE5value' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /opt/lib/libfolly.a(dynamic.cpp.o): relocation R_X86_64_PC32 against symbol `_ZN5folly7dynamic8typeNameENS0_4TypeE' can not be used when making a shared object; recompile with -fPIC
下面方法可支持构建SO
文件:
cmake .. "-DCMAKE_INCLUDE_PATH=/opt/include" "-DCMAKE_LIBRARY_PATH=/opt/lib" -DCMAKE_INSTALL_PREFIX=/opt/ -DFOLLY_SUPPORT_SHARED_LIBRARY=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON
其它还可能碰到的错误为:
/usr/bin/ld: warning: libaio.so.1, needed by /opt/lib/libfolly.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libunwind.so.1, needed by /opt/lib/libfolly.so, not found (try using -rpath or -rpath-link)
/opt/lib/libfolly.so: undefined reference to `io_getevents@LIBAIO_0.4'
/opt/lib/libfolly.so: undefined reference to `io_queue_init@LIBAIO_0.1'
/opt/lib/libfolly.so: undefined reference to `io_submit@LIBAIO_0.1'
/opt/lib/libfolly.so: undefined reference to `io_queue_release@LIBAIO_0.1'
本质原因是libfolly.so
找不到libaio.so
和libunwind.so
库文件。这可以用命令ldd /your_path/libfolly.so
看出来。可以从其它机器将这两个文件复制过来,位置位于:
/lib/x86_64-linux-gnu/libaio.so.1.0.1
/lib/x86_64-linux-gnu/libaio.so.1
(只是上面文件的软链接)/usr/local/lib/libunwind.so.1
注意: ubuntu 20.04 unwind 版本为 libunwind.so.8。如果提示缺失文件,执行下面命令:
sudo ln -s /usr/lib/x86_64-linux-gnu/libunwind.so.8.0.1 /usr/local/lib/libunwind.so
Q. E. D.