0

我正在尝试了解 tbb::concurrent_vector 的工作原理。我正在使用 ubuntu 20.04 LTS。我已经通过 ubuntu 包安装了它。

sudo apt-get update -y
sudo apt-get install -y libtbb-dev

当我尝试编译这个简单的 C++ 程序时

#include <bits/stdc++.h>
#include <tbb/concurrent_vector.h>

using namespace std;

int main() {
    tbb::concurrent_vector<int> cv ={1,2,3};
    return 0;
}

我收到此错误:

mmk@mmk:~/my-computer/codes$ g++ main.cpp -o main
/usr/bin/ld: /tmp/ccPLqyKd.o: in function `tbb::concurrent_vector<int, tbb::cache_aligned_allocator<int> >::concurrent_vector(std::initializer_list<int>, tbb::cache_aligned_allocator<int> const&)':
main.cpp:(.text._ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEEC2ESt16initializer_listIiERKS2_[_ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEEC5ESt16initializer_listIiERKS2_]+0xd3): undefined reference to `tbb::internal::concurrent_vector_base_v3::internal_clear(void (*)(void*, unsigned long))'
/usr/bin/ld: main.cpp:(.text._ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEEC2ESt16initializer_listIiERKS2_[_ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEEC5ESt16initializer_listIiERKS2_]+0x10a): undefined reference to `tbb::internal::concurrent_vector_base_v3::~concurrent_vector_base_v3()'
/usr/bin/ld: /tmp/ccPLqyKd.o: in function `tbb::concurrent_vector<int, tbb::cache_aligned_allocator<int> >::~concurrent_vector()':
main.cpp:(.text._ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEED2Ev[_ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEED5Ev]+0x4b): undefined reference to `tbb::internal::concurrent_vector_base_v3::internal_clear(void (*)(void*, unsigned long))'
/usr/bin/ld: main.cpp:(.text._ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEED2Ev[_ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEED5Ev]+0x71): undefined reference to `tbb::internal::concurrent_vector_base_v3::~concurrent_vector_base_v3()'
/usr/bin/ld: /tmp/ccPLqyKd.o: in function `void tbb::concurrent_vector<int, tbb::cache_aligned_allocator<int> >::internal_assign_iterators<int const*>(int const*, int const*)':
main.cpp:(.text._ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEE25internal_assign_iteratorsIPKiEEvT_S7_[_ZN3tbb17concurrent_vectorIiNS_23cache_aligned_allocatorIiEEE25internal_assign_iteratorsIPKiEEvT_S7_]+0x74): undefined reference to `tbb::internal::concurrent_vector_base_v3::internal_reserve(unsigned long, unsigned long, unsigned long)'
/usr/bin/ld: /tmp/ccPLqyKd.o: in function `tbb::cache_aligned_allocator<int>::allocate(unsigned long, void const*)':
main.cpp:(.text._ZN3tbb23cache_aligned_allocatorIiE8allocateEmPKv[_ZN3tbb23cache_aligned_allocatorIiE8allocateEmPKv]+0x29): undefined reference to `tbb::internal::NFS_Allocate(unsigned long, unsigned long, void*)'
/usr/bin/ld: /tmp/ccPLqyKd.o: in function `tbb::cache_aligned_allocator<int>::deallocate(int*, unsigned long)':
main.cpp:(.text._ZN3tbb23cache_aligned_allocatorIiE10deallocateEPim[_ZN3tbb23cache_aligned_allocatorIiE10deallocateEPim]+0x20): undefined reference to `tbb::internal::NFS_Free(void*)'
collect2: error: ld returned 1 exit status

我也尝试过以这种方式声明它但失败了:

std::vector<int> v = {0, 1, 2};
tbb::concurrent_vector cv(v.begin(), v.end);
tbb::concurrent_vector<int, tbb::cache_aligned_allocator<int>> cv;

请告诉我我怎么会遇到这个错误。

4

1 回答 1

1

您收到错误是因为您的代码无法与 TBB 库链接。

“-ltbb” 此标志用于链接 TBB 库文件

请尝试使用以下命令编译您的代码

g++ main.cpp -ltbb
于 2022-01-10T08:25:13.700 回答