Take the 2-minute tour ×

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?

share|improve this question
@awoodland: Hmmmm, the second answer says it does, and indeed section B14 has "printf(“Hello thread %d, f=%f\n”, threadIdx.x, f);" –  Kheldar Sep 4 '11 at 19:06
how is this supposed to be compiled then? –  andandandand Sep 4 '11 at 19:09
the code stuff from that book are mere snippets if I recall correctly, and not always full examples. Not to mention they use alto of bad practise in those example.... –  NekoNova Sep 4 '11 at 19:19

1 Answer 1

up vote 7 down vote accepted

You need to include stdio.h not 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
share|improve this answer
I posted the second sample from the chapter verbatim, Section 3.2.2, and it does include <iostream> –  andandandand Sep 4 '11 at 19:16
@omgzor: That's a mistake. Check the errata for the book (from the page I linked to in my answer). ''p.23,25 - The #includes for this example are incorrectly shown as: #include <iostream> and #include "book.h." This has been corrected in the downloadable code package, but should read: #include <stdio.h> and #include "../common/book.h"''. (Also you're probably looking at the code for chapter03/simple_kernel.cu rather than chapter03/hello_world.cu) –  user786653 Sep 4 '11 at 19:21
Thanks, I didn't check the code for the book, as the pdf I have doesn't mention it as available. –  andandandand Sep 4 '11 at 19:25

Your Answer


