编译 helloworld.cu 时遇到问题
在 Ubuntu 10.10 中编译这个 hello world 示例时,
这来自 CUDA 示例,章节3(没有提供编译指令>:@)
#include <iostream>
__global__ void kernel (void){
}
int main(void){
kernel <<<1,1>>>();
printf("Hellow World!\n");
return 0;
}
我得到了这个:
$ nvcc -lcudart hello.cu hello.cu(11):错误:标识符“printf”是 未定义
编译时检测到 1 个错误 “/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii”。
为什么?这段代码应该如何编译呢?
While compiling this hello world sample in Ubuntu 10.10
This is from CUDA by Example, chapter 3 (No compile instructions provided >:@)
#include <iostream>
__global__ void kernel (void){
}
int main(void){
kernel <<<1,1>>>();
printf("Hellow World!\n");
return 0;
}
I got this:
$ nvcc -lcudart hello.cu hello.cu(11): error: identifier "printf" is
undefined1 error detected in the compilation of
"/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii".
Why? How should this code be compiled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要包含
stdio.h
或cstdio
而不是iostream
(用于std::cout
内容)printf
(参见man 3 printf
)。我在这里找到了这本书的源代码。chapter03/hello_world.cu
实际上是:其中
../common/book.h
包含stdio.h
。README.txt
文件详细说明了如何编译示例:You need to include
stdio.h
orcstdio
notiostream
(which is forstd::cout
stuff) forprintf
(seeman 3 printf
). I found the source code for the book here.chapter03/hello_world.cu
is actually:Where
../common/book.h
includesstdio.h
.The
README.txt
file details how to compile the examples:问题是编译器不知道在哪里找到 printf 函数。它
需要知道在哪里可以找到它。
include
指令用于告诉编译器在哪里找到它。修复后,它可以工作:
The issue is that the compiler does not know where to find
printf
function. Itneeds to know where to find it. The
include
directive is used to tell the compiler where to find it.After the fix, it would work: