在Ubuntu 10.10中编译这个hello world示例时
这是来自CUDA by Example,第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”。
为什么呢?该代码应该如何编译?
答案 0 :(得分:11)
您需要为stdio.h
添加iostream
而不是std::cout
(适用于printf
个内容)(请参阅man 3 printf
)。我找到了本书here的源代码。
chapter03/hello_world.cu
实际上是:
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/
#include "../common/book.h"
int main( void ) {
printf( "Hello, World!\n" );
return 0;
}
../common/book.h
包括stdio.h
。
README.txt
文件详细说明了如何编译示例:
The vast majority of these code examples can be compiled quite easily by using NVIDIA's CUDA compiler driver, nvcc. To compile a typical example, say "example.cu," you will simply need to execute: > nvcc example.cu
答案 1 :(得分:0)
问题是编译器不知道在哪里可以找到printf
函数。它
需要知道在哪里找到它。 include
指令用于告诉编译器在哪里找到它。
#include "stdio.h"
int main(void) {
printf("Hello World!\n");
return 0;
}
修复后,它将起作用:
$ nvcc hello_world.cu
$ ls
a.out hello_world.cu
$ a.out
Hello World!