gpt4 book ai didi

c++ - 无法使用 TBB 和 lambda 编译基本的 C++ 程序

转载 作者:行者123 更新时间:2023-12-01 14:19:42 25 4
gpt4 key购买 nike

我尝试使用 TBB 和 lambda 表达式编写一个基本的 C++ 程序,但我无法编译它。



#include 
    
     
#include
#include
#include

使用命名空间标准;
使用命名空间 tbb;

void Foo(int number) {
cout<<数字< }

void ParallelApplyFoo(int* a, size_t n) {
parallel_for(blocked_range (0, n),
[ = ](const blocked_range & r){
对于 (size_t i = r.begin(); i != r.end();++i)
Foo(a[i]);
}
);
}

int main(int argc, char** argv) {
整数 = 10;
int* a = new int[num];
for(int i = 0; i < num; i++)
[我] = 我;
ParallelApplyFoo(a,num);
返回 0;
}

和编译器消息:

main.cpp: In function ‘void ParallelApplyFoo(int*, size_t)’:main.cpp:25:9: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]main.cpp:26:5: error: no matching function for call to ‘parallel_for(tbb::blocked_range, ParallelApplyFoo(int*, size_t)::&)>)’main.cpp:26:5: note: candidates are:In file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:161:6: note: template void tbb::parallel_for(const Range&, const Body&)main.cpp:26:5: error: template argument for ‘template void tbb::parallel_for(const Range&, const Body&)’ uses local type ‘ParallelApplyFoo(int*, size_t)::&)>’main.cpp:26:5: error:   trying to instantiate ‘template void tbb::parallel_for(const Range&, const Body&)’In file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:168:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::simple_partitioner&)/usr/include/tbb/parallel_for.h:168:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 3 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:175:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::auto_partitioner&)/usr/include/tbb/parallel_for.h:175:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 3 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:182:6: note: template void tbb::parallel_for(const Range&, const Body&, tbb::affinity_partitioner&)/usr/include/tbb/parallel_for.h:182:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 3 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:190:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::simple_partitioner&, tbb::task_group_context&)/usr/include/tbb/parallel_for.h:190:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 4 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:197:6: note: template void tbb::parallel_for(const Range&, const Body&, const tbb::auto_partitioner&, tbb::task_group_context&)/usr/include/tbb/parallel_for.h:197:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 4 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:204:6: note: template void tbb::parallel_for(const Range&, const Body&, tbb::affinity_partitioner&, tbb::task_group_context&)/usr/include/tbb/parallel_for.h:204:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   candidate expects 4 arguments, 2 providedIn file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:248:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, const Function&, tbb::task_group_context&)/usr/include/tbb/parallel_for.h:248:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)In file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:235:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, Index, const Function&, tbb::task_group_context&)/usr/include/tbb/parallel_for.h:235:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)In file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:228:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, const Function&)/usr/include/tbb/parallel_for.h:228:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)In file included from main.cpp:10:0:/usr/include/tbb/parallel_for.h:215:6: note: template void tbb::strict_ppl::parallel_for(Index, Index, Index, const Function&)/usr/include/tbb/parallel_for.h:215:6: note:   template argument deduction/substitution failed:main.cpp:26:5: note:   deduced conflicting types for parameter ‘Index’ (‘tbb::blocked_range’ and ‘ParallelApplyFoo(int*, size_t)::&)>’)gmake[2]: *** [build/Release/GNU-Linux-x86/main.o] Error 1

我尝试了编译器标志“-std=c++11”和“-std=gnu++11”然后输出:

g++ -std=gnu++11    -o dist/Release/GNU-Linux-x86/test build/Release/GNU-Linux-x86/main.o  build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::~start_for()':main.cpp:(.text+0x6): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::~start_for()':main.cpp:(.text+0x26): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::run(tbb::blocked_range const&, {lambda(tbb::blocked_range const&)#1} const&, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1} const&)':main.cpp:(.text+0x9d): undefined reference to `tbb::task_group_context::init()'main.cpp:(.text+0xb5): undefined reference to `tbb::internal::allocate_root_with_context_proxy::allocate(unsigned int) const'main.cpp:(.text+0xe1): undefined reference to `tbb::internal::get_initial_auto_partitioner_divisor()'main.cpp:(.text+0x107): undefined reference to `tbb::task_group_context::~task_group_context()'main.cpp:(.text+0x116): undefined reference to `tbb::task_group_context::~task_group_context()'main.cpp:(.text+0x12a): undefined reference to `vtable for tbb::task'main.cpp:(.text+0x138): undefined reference to `tbb::internal::allocate_root_with_context_proxy::free(tbb::task&) const'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::start_for, ParallelApplyFoo(int*, unsigned int)::{lambda(tbb::blocked_range const&)#1}, tbb::auto_partitioner>::execute()':main.cpp:(.text+0x251): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'main.cpp:(.text+0x27b): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'main.cpp:(.text+0x313): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'main.cpp:(.text+0x33d): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'main.cpp:(.text+0x4b6): undefined reference to `tbb::internal::allocate_continuation_proxy::allocate(unsigned int) const'main.cpp:(.text+0x4e8): undefined reference to `tbb::internal::allocate_child_proxy::allocate(unsigned int) const'main.cpp:(.text+0x585): undefined reference to `tbb::task_group_context::is_group_execution_cancelled() const'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::flag_task::~flag_task()':main.cpp:(.text._ZN3tbb10interface68internal9flag_taskD2Ev[_ZN3tbb10interface68internal9flag_taskD5Ev]+0x6): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::signal_task::~signal_task()':main.cpp:(.text._ZN3tbb10interface68internal11signal_taskD2Ev[_ZN3tbb10interface68internal11signal_taskD5Ev]+0x6): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::signal_task::~signal_task()':main.cpp:(.text._ZN3tbb10interface68internal11signal_taskD0Ev[_ZN3tbb10interface68internal11signal_taskD0Ev]+0x6): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o: In function `tbb::interface6::internal::flag_task::~flag_task()':main.cpp:(.text._ZN3tbb10interface68internal9flag_taskD0Ev[_ZN3tbb10interface68internal9flag_taskD0Ev]+0x6): undefined reference to `vtable for tbb::task'build/Release/GNU-Linux-x86/main.o:(.rodata+0x20): undefined reference to `typeinfo for tbb::task'build/Release/GNU-Linux-x86/main.o:(.rodata._ZTVN3tbb10interface68internal11signal_taskE[_ZTVN3tbb10interface68internal11signal_taskE]+0x14): undefined reference to `tbb::task::note_affinity(unsigned short)'build/Release/GNU-Linux-x86/main.o:(.rodata._ZTVN3tbb10interface68internal9flag_taskE[_ZTVN3tbb10interface68internal9flag_taskE]+0x14): undefined reference to `tbb::task::note_affinity(unsigned short)'build/Release/GNU-Linux-x86/main.o:(.rodata._ZTIN3tbb10interface68internal11signal_taskE[_ZTIN3tbb10interface68internal11signal_taskE]+0x8): undefined reference to `typeinfo for tbb::task'build/Release/GNU-Linux-x86/main.o:(.rodata._ZTIN3tbb10interface68internal9flag_taskE[_ZTIN3tbb10interface68internal9flag_taskE]+0x8): undefined reference to `typeinfo for tbb::task'

我使用的是 Fedora 18(当然安装了 tbb 和 tbb-devel 软件包)。

有人有什么想法吗?代码是从这里复制的:http://software.intel.com/en-us/blogs/2009/08/03/parallel_for-is-easier-with-lambdas-intel-threading-building-blocks

最佳答案

使用 -ltbb 编译器标志解决了它,可在此处找到:http://goparallel.sourceforge.net/compiling-tbb-programs-and-examples-on-linux-ubuntu/ .无论如何,感谢大家的帮助:)

关于c++ - 无法使用 TBB 和 lambda 编译基本的 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15467710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com