编译 helloworld.cu 时遇到问题

发布于 2024-12-02 22:13:36 字数 586 浏览 5 评论 0原文

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

}

我得到了这个:

$ nvcc -lcudart hello.cu hello.cu(11):错误:标识符“printf”是 未定义

编译时检测到 1 个错误 “/tmp/tmpxft_00007812_00000000-4_hello.cpp1.ii”。

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

While compiling this hello world sample in Ubuntu 10.10

This is from CUDA by Example, chapter 3 (No compile instructions provided >:@)

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

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

Why? How should this code be compiled?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-12-09 22:13:36

您需要包含 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 文件详细说明了如何编译示例:

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

You need to include stdio.h or cstdionot iostream (which is for std::cout stuff) for printf (see man 3 printf). I found the source code for the book here.

chapter03/hello_world.cu is actually:


/*
 * 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;
}

Where ../common/book.h includes stdio.h.

The README.txt file details how to compile the examples:

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
心碎的声音 2024-12-09 22:13:36

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

The issue is that the compiler does not know where to find printf function. It
needs to know where to find it. The include directive is used to tell the compiler where to find it.

#include "stdio.h"

int main(void) {
  printf("Hello World!\n");
  return 0;
}

After the fix, it would work:

$ nvcc hello_world.cu
$ ls
a.out  hello_world.cu
$ a.out
Hello World!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文