编译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;
}

I got this:

$ NVCC -lcudart hello。Cu hello.cu(11):错误:标识符"printf"是未定义的

编译时检测到的

1错误"/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii"。

为什么?这段代码应该如何编译?

您需要包括stdio.hcstdio而不是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文件详细说明了如何编译这些示例:

<>之前这些代码示例中的绝大多数都可以通过使用NVIDIA的CUDA编译器驱动程序nvcc。要编译一个典型的示例,请输入"的例子。那么,您只需要执行:> NVCC example.cu

问题是编译器不知道在哪里找到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!

最新更新