gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:40:18 27 4
gpt4 key购买 nike

在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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com