我不使用 TBB,但我收到与 TBB 相关的链接器错误

I do not use TBB, but I get linker errors related to TBB

我有一些测试代码,其中包括:

#include <stddef.h>
#include <stdio.h>
#include <algorithm>
#include <chrono>
#include <execution>
#include <random>
#include <ratio>
#include <vector>

当我使用 Clang/Linux 构建它时,出现这些链接器错误,这似乎暗示我正在使用 TBB 库(我不是):

[1/2] /usr/bin/clang++ -IAlgosGPGPU.p -I. -I../AlgosGPGPU -fcolor-diagnostics -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=c++20 -g -MD -MQ AlgosGPGPU.p/main.cpp.o -MF AlgosGPGPU.p/main.cpp.o.d -o AlgosGPGPU.p/main.cpp.o -c ../AlgosGPGPU/main.cpp
[2/2] /usr/bin/clang++  -o AlgosGPGPU AlgosGPGPU.p/main.cpp.o -Wl,--as-needed -Wl,--no-undefined
FAILED: AlgosGPGPU 
/usr/bin/clang++  -o AlgosGPGPU AlgosGPGPU.p/main.cpp.o -Wl,--as-needed -Wl,--no-undefined
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `void tbb::interface7::internal::isolate_impl<void, __pstl::__tbb_backend::__parallel_stable_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, __pstl::__internal::__pattern_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true> >(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true>, std::integral_constant<bool, true>, std::integral_constant<bool, true>)::{lambda()#1}::operator()() const::{lambda(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>)#1}>(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, __pstl::__internal::__pattern_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true> >(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true>, std::integral_constant<bool, true>, std::integral_constant<bool, true>)::{lambda()#1}::operator()() const::{lambda(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>)#1}, unsigned long)::{lambda()#1} const>(__pstl::__tbb_backend::__parallel_stable_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, __pstl::__internal::__pattern_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true> >(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true>, std::integral_constant<bool, true>, std::integral_constant<bool, true>)::{lambda()#1}::operator()() const::{lambda(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>)#1}>(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, __pstl::__internal::__pattern_sort<__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true> >(__pstl::execution::v1::parallel_unsequenced_policy const&, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>, std::integral_constant<bool, true>, std::integral_constant<bool, true>, std::integral_constant<bool, true>)::{lambda()#1}::operator()() const::{lambda(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, std::less<double>)#1}, unsigned long)::{lambda()#1} const&)':
/usr/include/tbb/task_arena.h:216: undefined reference to `tbb::interface7::internal::isolate_within_arena(tbb::interface7::internal::delegate_base&, long)'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `tbb::tbb_allocator<double>::allocate(unsigned long, void const*)':
/usr/include/tbb/tbb_allocator.h:86: undefined reference to `tbb::internal::allocate_via_handler_v3(unsigned long)'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `operator new(unsigned long, tbb::internal::allocate_root_proxy const&)':
/usr/include/tbb/task.h:1145: undefined reference to `tbb::internal::allocate_root_proxy::allocate(unsigned long)'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `operator delete(void*, tbb::internal::allocate_root_proxy const&)':
/usr/include/tbb/task.h:1149: undefined reference to `tbb::internal::allocate_root_proxy::free(tbb::task&)'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `task':
/usr/include/tbb/task.h:625: undefined reference to `vtable for tbb::task'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `operator new(unsigned long, tbb::internal::allocate_continuation_proxy const&)':
/usr/include/tbb/task.h:1163: undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned long) const'
/usr/bin/ld: AlgosGPGPU.p/main.cpp.o: in function `operator delete(void*, tbb::internal::allocate_continuation_proxy const&)':
/usr/include/tbb/task.h:1167: undefined reference to `tbb::internal::allocate_continuation_proxy::free(tbb::task&) const'
...

如果我注释掉这一行代码:

sort(std::execution::par_unseq, sorted.begin(), sorted.end());

程序构建成功。

这是怎么回事? clang 是否在底层使用 TBB?

我相信大多数 Linux 发行版 link 都带有 GNU libstdc++,其 <execution> 支持确实需要 libtbb。这个埋在the documentation,搜索“注3”。所以你必须安装 libtbb 并将 -ltbb 添加到你的 link 命令中。