对 tbb::internal::concurrent_vector_base 的未定义引用

问题描述 投票:0回答:2

我正在尝试了解 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;

请告诉我如何遇到此错误。

tbb concurrent-vector
2个回答
1
投票

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

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

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

g++ main.cpp -ltbb

0
投票

当源文件在

g++
之后进入
-ltbb
命令行时,即经过大量简化之后:

# Linker errors
g++ -ltbb program.cpp
# Works
g++ program.cpp -ltbb
© www.soinside.com 2019 - 2024. All rights reserved.