1

即使提到并发队列,似乎也无法编译程序。

代码包含这个

#include <tbb/concurrent_queue.h>

其次,我在代码中的任何地方添加

concurrent_queue<int> tbbqueue;

这是我在编译时遇到的错误。我可以使用任务等编译其他一些与 tbb 相关的代码,但由于某种原因这不起作用。

    g++ -O3 -Wall -pthread -std=c++11 -ltbb -o tbbqueue.o tbbqueue.cpp 
tbbqueue.cpp: In function ‘void* Agent(void*)’:
tbbqueue.cpp:46:10: warning: unused variable ‘elements’ [-Wunused-variable]
tbbqueue.cpp:47:9: warning: unused variable ‘elementsSize’ [-Wunused-variable]
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::internal::concurrent_queue_base_v3<int>::~concurrent_queue_base_v3()':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl8internal24concurrent_queue_base_v3IiED2Ev[_ZN3tbb10strict_ppl8internal24concurrent_queue_base_v3IiED5Ev]+0x10): undefined reference to `tbb::internal::NFS_Free(void*)'
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> >::deallocate_block(void*, unsigned long)':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE16deallocate_blockEPvm[_ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE16deallocate_blockEPvm]+0x4): undefined reference to `tbb::internal::NFS_Free(void*)'
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> >::allocate_block(unsigned long)':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE14allocate_blockEm[_ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE14allocate_blockEm]+0xf): undefined reference to `tbb::internal::NFS_Allocate(unsigned long, unsigned long, void*)'
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE14allocate_blockEm[_ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEE14allocate_blockEm]+0x2b): undefined reference to `tbb::internal::throw_exception_v4(tbb::internal::exception_id)'
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::internal::concurrent_queue_base_v3<int>::~concurrent_queue_base_v3()':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl8internal24concurrent_queue_base_v3IiED0Ev[_ZN3tbb10strict_ppl8internal24concurrent_queue_base_v3IiED0Ev]+0x10): undefined reference to `tbb::internal::NFS_Free(void*)'
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> >::~concurrent_queue()':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEED2Ev[_ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEED5Ev]+0x12d): undefined reference to `tbb::internal::NFS_Free(void*)'
/tmp/ccQg8OKZ.o: In function `tbb::strict_ppl::concurrent_queue<int, tbb::cache_aligned_allocator<int> >::~concurrent_queue()':
tbbqueue.cpp:(.text._ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEED0Ev[_ZN3tbb10strict_ppl16concurrent_queueIiNS_23cache_aligned_allocatorIiEEED0Ev]+0x12d): undefined reference to `tbb::internal::NFS_Free(void*)'
/tmp/ccQg8OKZ.o: In function `main':
tbbqueue.cpp:(.text.startup+0x32): undefined reference to `tbb::internal::NFS_Allocate(unsigned long, unsigned long, void*)'
collect2: error: ld returned 1 exit status
make: *** [tbbqueue.o] Error 1
4

1 回答 1

1

为链接指定库和源/目标文件的顺序很重要。在此处查看更多详细信息:为什么链接库的顺序有时会导致 GCC 中的错误?

在这种情况下,与 TBB ( -ltbb) 链接的标志是在程序源文件 ( ) 之前的命令行中指定的tbbqueue.cpp。因此,当链接器处理 TBB 库时,它没有看到任何使用它的代码,决定它是不必要的,并从考虑中删除了它的所有符号。然后,它在从 编译的目标文件中看到了外部符号tbbqueue.cpp,但没有看到可以找到这些符号的库。

如果选项的顺序改变如下:

g++ -O3 -Wall -pthread -std=c++11 -o tbbqueue.o tbbqueue.cpp -ltbb

编译应该成功。

于 2014-10-03T12:37:43.430 回答