cuda - 编译helloworld.cu时遇到问题

标签 cuda

在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): error: identifier "printf" is undefined

1 error detected in the compilation of "/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii".



为什么?此代码应如何编译?

最佳答案

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

这些代码示例中的绝大多数都可以通过使用以下命令轻松编译
NVIDIA的CUDA编译器驱动程序nvcc。为了编译一个典型的例子,说
您只需执行“example.cu”:

> nvcc example.cu

关于cuda - 编译helloworld.cu时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301478/

相关文章:

c++ - 简单的 CUDA 应用程序,cudaMalloc 以错误 : unspecified driver error 结束

cuda - GPU 多处理器的内核 block 执行之间的共享内存会发生什么变化?

CMake v3.4.1 在 Ubuntu 16.04 上找到 CUDA 7.5 而不是 CUDA 8.0

c++ - 多 GPU 环境中单应用线程的 CUDA 5.0 上下文管理

cuda - 将 cudaStream_t 对象转换为 CUStream 对象

c++ - Cuda:访问冲突写入位置 0x0000000000000000

TensorFlow GPU : is cudnn optional?无法打开CUDA库libcudnn.so

linux - 无法使用 clang 在 linux 上构建 cuda 7.0 示例

optimization - 减少 CUDA 内核中使用的寄存器数量

c - 未处理的异常...读取位置访问冲突