【发布时间】:2015-06-25 18:54:38
【问题描述】:
我正在尝试在 Ubuntu 15.04 中测试以下代码。
#include <iostream>
#include <cilk/cilk.h>
using namespace std;
int main()
{
cilk_for(int x=0; x<10; x++)
{
cout << x << endl;
}
return 0;
}
我收到以下错误。似乎 g++ 命令可以检测到 cilk plus 但无法以某种方式编译。
anirban@anirban-XPS-8500:~$ g++ test_cilk.cpp -lcilkplus -lcilkrts
test_cilk.cpp: In function ‘int main()’:
test_cilk.cpp:8:11: error: expected primary-expression before ‘int’
cilk_for(int x=0; x<10; x++)
^
test_cilk.cpp:8:20: error: ‘x’ was not declared in this scope
cilk_for(int x=0; x<10; x++)
^
【问题讨论】:
-
我的猜测是
cilk_for是一个包含普通for循环的宏,这意味着您需要包含定义宏的头文件。我建议您阅读thecilk_forreference。 -
我尝试过#include
但不起作用。 -
如果包含头文件会出现什么问题?相同?还有什么?在列出源文件之后,您应该真正将库移动到命令行的末尾(参见例如this old answer of mine)。
-
@AnirbanGhosh Joachim 是对的。您需要包含
cilk.h文件。包含 cilk.h 文件时更新错误部分 -
例如,之前链接的
cilk_for引用表明关键字 是_Cilk_for,并且因为符号名称以下划线开头大写字母是为实现保留的,在我看来,它确实是是一个关键字,是英特尔编译器专用的基本 C 和 C++ 语言的扩展。