【问题标题】:Can't run C++17 parallel algorithms with GCC on Linux无法在 Linux 上使用 GCC 运行 C++17 并行算法
【发布时间】:2021-04-07 18:54:31
【问题描述】:

我想使用 GCC9.3 的 C++17 执行策略运行标准算法。

我已经下载了https://github.com/oneapi-src/oneTBB/releases/download/v2021.1.1/oneapi-tbb-2021.1.1-lin.tgz

但我收到一个错误,即“任务”类未定义 即使是来自https://www.ibm.com/developerworks/aix/library/au-intelthreadbuilding/index.html的“hello world”示例

#include "tbb/tbb.h"
#include <iostream>
using namespace tbb;
using namespace std;
 
class first_task : public task { 
    public: 
    task* execute( ) { 
       cout << "Hello World!\n";
       return NULL;
    }
};
 
int main( )
{ 
    task_scheduler_init init(task_scheduler_init::automatic);
    first_task& f1 = *new(tbb::task::allocate_root()) first_task( );
    tbb::task::spawn_root_and_wait(f1);
}

我收到以下错误:

test.cpp:55:32: error: expected class-name before '{' token
   55 | class first_task : public task {
      |                                ^
test.cpp:57:5: error: 'task' does not name a type
   57 |     task* execute( ) {
      |     ^~~~
test.cpp: In function 'int main()':
test.cpp:65:5: error: 'task_scheduler_init' was not declared in this scope
   65 |     task_scheduler_init init(task_scheduler_init::automatic);
      |     ^~~~~~~~~~~~~~~~~~~
test.cpp:66:38: error: 'allocate_root' is not a member of 'tbb::v1::task'
   66 |     first_task& f1 = *new(tbb::task::allocate_root()) first_task( );
      |                                      ^~~~~~~~~~~~~
test.cpp:67:16: error: 'spawn_root_and_wait' is not a member of 'tbb::v1::task'
   67 |     tbb::task::spawn_root_and_wait(f1);
      |                ^~~~~~~~~~~~~~~~~~~

我正在编译:使用以下命令:

g++ -I&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/include/oneapi -I&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/include/oneapi/tbb -I&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/include -L&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/lib/intel64/gcc4.8/ test.cpp -o test

运行 source env/var.h 没有帮助 我做错了吗?

谢谢

【问题讨论】:

  • 前两个包含路径不是必需的。试试更短的命令g++ -I&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/include -L&lt;path_to_tbb&gt;/oneapi-tbb-2021.1.1/lib/intel64/gcc4.8/ test.cpp -o test
  • 不确定这里发生了什么,而且似乎没有多大关系,但gcc4.8 对我来说看起来很错误,因为 GCC >= 5 中的 libstdc++ 在设计上与以前的二进制文件不兼容(对于 C++ 11 一致性),除非它是专门为向后兼容而配置的。
  • 您需要将您的应用程序与 TBB 关联,即在命令行中添加 -ltbb,例如:solarianprogrammer.com/2019/05/09/… 但是,您也有编译器错误。尝试使用选项-E trhat 仅执行预处理器步骤并查看生成的文件。此外,错误消息上的行号与您向我们显示的来源不对应。 .
  • 如果您在 Linux 上,请检查您的发行版是否带有用于 tbb 的软件包,例如在基于 Debian 的发行版上,apt install libtbb-dev 可能有效。然后用g++ test.cpp -o test $(pkg-config --libs --cflags tbb)编译。
  • 您好,感谢您的 cmets。 @SM - 即使使用更短的命令,我仍然遇到同样的问题

标签: c++ gcc c++17 tbb


【解决方案1】:

从错误消息中,我得出结论,您使用的 gcc 或 TBB 实现都是实验性的。如您所见,gcc 确实找到了 TBB(#include tbb/tbb.h 上没有错误),这很好!然后,错误显示您使用的某些函数隐藏在 tbb::v1 命名空间内。因此,您可以使用using namespace tbb::v1 或在所有“未知”名称前添加tbb::v1::

然后,在 https://community.intel.com/t5/Intel-oneAPI-Threading-Building/Unable-to-compile-TBB-program/td-p/1226663
他们认为tbb::task 已贬值。请使用更现代的 TBB 教程 :-)

另见:https://github.com/oneapi-src/oneTBB/issues/243

【讨论】:

  • 非常感谢。您评论中链接的示例确实可以编译。但是原始的“任务”错误来自 GCC 执行策略标头。不是我直接使用“任务”。我应该在 GCC 中使用旧版本的 TBB 吗?哪个版本适用于 GCC 9.3?
  • 你不应该使用旧的例子!就是这样,我想。如今,受信任公司 www 页面上的旧示例非常普遍,因为行业变化非常快。一个示例是 cmake,它为初学者提供了大量教程,但我发现没有一个教“现代 cmake”。错误来自 gcc,这是可能的,但它的来源肯定是在 TBB 中。很可能任何被贬值的东西都被移到了tbb::v1 命名空间。不要使用这些功能,因为它们很快就会从 TBB 中删除。
  • 我没有使用这些功能。我添加了 包括。 GCC 中的代码显然使用了任务。我没有找到 GCC/TBB 兼容性矩阵。
  • 您向我们展示的代码明确地使用了任务。下次只展示你感兴趣的代码。另外,当 gcc 9.3 发布时,TBB 任务可能没有贬值。另见:software.intel.com/content/www/us/en/develop/articles/… 和其中的 pdf。我在上一个项目中广泛使用了 TBB,但我们有一个人专门处理所有这些兼容性问题并为它们编写包装器。
猜你喜欢
  • 2021-03-04
  • 2020-05-30
  • 1970-01-01
  • 2020-12-12
  • 2012-11-16
  • 2012-06-08
  • 2017-08-20
  • 2014-04-08
  • 2013-05-28
相关资源
最近更新 更多