问题
Can't seem to compile program with even a mention of concurrent_queue.
code contains this
#include <tbb/concurrent_queue.h>
And second I add anywhere in the code
concurrent_queue<int> tbbqueue;
This is the error I get on compile. I am able to compile some other tbb related code using tasks etc, but this for some reason is not working.
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
回答1:
The order, in which you specify libraries and source/object files for linking, matters. See more details here: Why does the order in which libraries are linked sometimes cause errors in GCC?
In this case, the flag to link with TBB (-ltbb
) was specified in the command line prior to the program source file (tbbqueue.cpp
). So when the linker processed the TBB library it had seen no code that uses it, decided it is unnecessary, and removed all its symbols from consideration. Then, it saw external symbols in the object file compiled from tbbqueue.cpp
, but not libraries where these symbols could be found.
If the order of options is changed like this:
g++ -O3 -Wall -pthread -std=c++11 -o tbbqueue.o tbbqueue.cpp -ltbb
the compilation should succeed.
来源:https://stackoverflow.com/questions/16115022/tbb-concurrent-queue-errors