在Ubuntu20.04系统下使用源代码编译libtorrent,首先需要安装boost,这里使用boost 1.75.0版本。在boost官网下载好源码压缩包,解压后cd到boost_1_75_0目录下。网上查到的很多安装方法都不适用,直接根据官网的方法安装即可。
官网链接:https://www.boost.org/doc/libs/1_75_0/more/getting_started/unix-variants.html
在boost_1_75_0目录下执行下列命令完成boost安装:
$ ./bootstrap.sh
$ b2
$ ./b2 install --prefix=/usr/local
Boost安装完成后,即可进行libtorrent的编译和安装。首先在libtorrent的GitHub官网(https://github.com/arvidn/libtorrent)上下载源码libtorrent-RC_2_0。需要注意,在GitHub中下载会遗漏deps目录下两个文件夹中的内容,需要在GitHub中对这两个文件夹分别下载,再拷贝到相应目录下。
libtorrent源码准备完成后,首先进行依赖包的安装:
$ sudo apt install libboost-tools-dev libboost-dev libboost-system-dev
然后开始编译安装:
$ echo "using gcc ;" >>~/user-config.jam
$ b2 cxxstd=14 //注意一定要指定使用 C++ 14标准
$ sudo b2 install --prefix=/usr/local cxxstd=14
因脚本设置原因,需要将编译完成的静态库libtorrent.a拷贝到/usr/local/lib目录下。
Libtorrent的源码中包含有示例程序,位于源码文件夹中的example目录下。在安装好libtorrent后可编译该示例进行测试。编译方法如下:
首先更改example下的CMakeLists.txt文档中的内容,修改内容见下面代码的19和27行:
project(libtorrent-examples)
set(single_file_examples
simple_client
custom_storage
stats_counters
dump_torrent
dump_bdecode
make_torrent
connection_tester
upnp_test)
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
add_compile_options(-Wno-implicit-int-float-conversion)
endif()
foreach(example ${
single_file_examples})
add_executable(${
example} "${example}.cpp")
target_link_libraries(${
example} PRIVATE torrent-rasterbar torrent try_signal pthread) //增加 torrent try_signal pthread
endforeach(example)
add_executable(client_test
client_test.cpp
print.cpp
torrent_view.cpp
session_view.cpp)
target_link_libraries(client_test PRIVATE torrent-rasterbar pthread torrent try_signal) //增加 pthread torrent try_signal
然后编译示例程序
$ mkdir build // cd至 example目录下新建 build目录
$ cd build //进入 build目录下
$ cmake .. //通过 CMakeLists.txt文件生成 Makefile文件
$ make //执行编译
编译完成后会生成多个测试用例,使用下载好的种子文件进行测试。