自己的测试环境是 Ubuntu16.04.
由于ubuntu版本的的限制,使用 ubuntu 的二进制指令 sudo apt-get install libtbb-dev
进行安装 tbb 的版本较低。Ubuntu16.04 二进制安装的 tbb 版本是 4.4~20151115-0ubuntu3
,Ubuntu18.04 二进制安装的 tbb 版本是 2017~U7-8.
此教程同样适用于 ubuntu 18.
解压
tar -zxvf oneTBB-2020_U3.tar.gz
因为要使用 gcc-9 进行编译,所以需要编辑成 gcc-9 形式
cp build/linux.gcc.inc build/linux.gcc-9.inc
编辑 linux.gcc-9.inc 文件:
第15、16行原来是
CPLUS ?= g++
CONLY ?= gcc
修改为
CPLUS ?= g++-9
CONLY ?= gcc-9
然后在文件夹 oneTBB-2020_U3/
中编译
cd oneTBB-2020_U3
make compiler=gcc-9 stdver=c++17 tbb_build_prefix=my_tbb_build
编译完成后,在 builld/
文件夹下会看到编译生成的文件夹 my_tbb_build_release/
.
将 tbb 编译生成的库文件放到对应的 /usr/
文件夹下:
sudo mkdir /usr/local/tbb-2020_U3
sudo cp -r oneTBB-2020_U3/include /usr/local/tbb-2020_U3/include
# 建立新安装tbb版本的符号链接
sudo ln -s /usr/local/tbb-2020_U3/include/tbb /usr/local/include/tbb
sudo cp -r oneTBB-2020_U3/build/my_tbb_build_release /usr/local/tbb-2020_U3/lib
# 建立新安装tbb版本的符号链接
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbb.so.2 /usr/local/lib/libtbb.so
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbbmalloc.so.2 /usr/local/lib/libtbbmalloc.so
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbbmalloc_proxy.so.2 /usr/local/lib/libtbbmalloc_proxy.so
然后把 库文件的路径写入到 ~/.bashrc
:
echo 'export LD_LIBRARY_PATH=/usr/local/tbb-2020_U3/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc
然后再次编译程序。
如果出现以下报错,说明现在还是索引到了旧版本的 tbb .
In file included from /usr/include/c++/9/pstl/parallel_backend.h:14,
from /usr/include/c++/9/pstl/algorithm_impl.h:25,
from /usr/include/c++/9/pstl/glue_execution_defs.h:52,
from /usr/include/c++/9/execution:32,
from /home/wong/workspace/***/src/***/***/src/laser_mapping.cc:3:
/usr/include/c++/9/pstl/parallel_backend_tbb.h:28:2: error: #error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.
28 | #error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.
| ^~~~~
In file included from /usr/include/c++/9/pstl/parallel_backend.h:14,
from /usr/include/c++/9/pstl/algorithm_impl.h:25,
from /usr/include/c++/9/pstl/glue_execution_defs.h:52,
from /usr/include/c++/9/execution:32,
from /home/wong/workspace/***/src/***/***/src/pointcloud_preprocess.cc:4:
/usr/include/c++/9/pstl/parallel_backend_tbb.h:28:2: error: #error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.
28 | #error Intel(R) Threading Building Blocks 2018 is required; older versions are not supported.
需要卸载掉原来的 tbb,或者把原来的 tbb 库文件备份换个名字(这里把 tbb 库文件备份换个名字):
cd /usr/lib/x86_64-linux-gnu
# 把原来的 tbb 库文件备份换个名字
sudo mv libtbb.so libtbb.so.bk
sudo mv libtbbmalloc_proxy.so libtbbmalloc_proxy.so.bk
sudo mv libtbbmalloc.so libtbbmalloc.so.bk
# 建立新安装tbb版本的符号链接
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbb.so.2 /usr/lib/x86_64-linux-gnu/libtbb.so
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbbmalloc.so.2 /usr/lib/x86_64-linux-gnu/libtbbmalloc.so
sudo ln -s /usr/local/tbb-2020_U3/lib/libtbbmalloc_proxy.so.2 /usr/lib/x86_64-linux-gnu/libtbbmalloc_proxy.so
至此,安装tbb完成。
[1] 天街踏尽公卿骨. linux tbb 安装_Ubuntu18.04 GCC9 安装 [EB/OL]. https://blog.csdn.net/weixin_32207065/article/details/112270765, 2021-01-01/2022-08-11.
[2] 知行合一2018. Ubuntu 16.04系统中使用GCC 9.1及Intel TBB库运行C++17 STL并行算法库 [EB/OL]. https://blog.csdn.net/davidhopper/article/details/98309966, 2022-02-15 /2022-08-11.