当前位置: 首页 > 知识库问答 >
问题:

无法使用TBB和lambda编译basic C程序

董元徽
2023-03-14

我试图用TBB和lambda表达式编写一个基本的C程序,但我无法编译它<代码>

#include <iostream>
#include <cstdlib>
#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>

using namespace std;
using namespace tbb;

void Foo(int number) {
    cout<<number<<endl;
}

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

int main(int argc, char** argv) {
    int num = 10;
    int* a = new int[num];
    for(int i = 0; i < num; i++)
        a[i] = i;
    ParallelApplyFoo(a,num);
    return 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 provided
In 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 provided
In 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 provided
In 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 provided
In 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 provided
In 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 provided
In 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'

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

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

共有1个答案

仲孙献
2023-03-14

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

 类似资料:
  • 我有一个包含一些用户对象的列表,我正在尝试对列表进行排序,但只使用方法引用,使用lambda表达式编译器会给出一个错误: 错误:

  • 当我打f5我得到这个文本在调试窗口 javac版本展示 java版本 我试图编译的代码 路径、JAVA_HOME和JRE_HOME设置正确。调试已安装的加载项。我可以编译和运行相同的代码在InteliJ IDEA或使用java(文件),javac(文件),它会工作正常,但我不知道为什么它不工作在vscode.

  • 我试图开始一个项目与Angular,但我不能进一步因为错误: “在multi./src/styles.css./node_modules/bootstrap/dist/css/bootstrap.min.css模块中找不到错误:” 依赖关系如下: json(相关部分): devDependencies: 我已经尝试了stackoverflow的所有解决方案,但都没有效果。我试过: 我尝试过组合不同

  • 问题内容: 我想安装rJava,但没有用。当我在控制台中键入R CMD javareconf时,出现以下错误: 任何想法如何解决? 问题答案: 该文件是JDK安装的一部分。您可能没有正确安装Java JDK。从此处下载Oracle Java ,或使用软件包管理器。 如果您已经安装了Java JDK,则将JAVA_HOME环境变量设置为指向JDK目录。

  • 问题内容: 以下Java代码无法编译: 编译器报告: 奇怪的是,标记为“ OK”的行可以正常编译,但是标记为“ Error”的行失败。它们看起来基本相同。 问题答案: 您的lambda需要与保持一致。如果您参考JLS#15.27.3(Lambda的类型): 如果满足以下所有条件,则lambda表达式与函数类型一致: […] 如果函数类型的结果为void,则lambda主体为语句表达式(第14.8节

  • 问题内容: 我正在尝试按照本教程创建可执行文件 https://github.com/anthony- tuininga/cx_Freeze/tree/master/cx_Freeze/samples/Tkinter 进行一些调整后,我可以编译项目,但是当我单击.exe时,会触发加载鼠标的动画,但没有任何加载。之前曾问过这个问题,但从未解决过。 我的应用程式档案 我的setup.py 另外我一直在