在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;
}
I got this:
$ NVCC -lcudart hello。Cu hello.cu(11):错误:标识符"printf"是未定义的
编译时检测到的1错误"/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii"。
为什么?这段代码应该如何编译?
您需要包括stdio.h
或cstdio
而不是iostream
(这是std::cout
的东西)为printf
(见man 3 printf
)。我在这里找到了这本书的源代码。
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
文件详细说明了如何编译这些示例:
问题是编译器不知道在哪里找到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!