# in-kernel printf

**URL:** <https://forums.developer.nvidia.com/t/in-kernel-printf/20635>\
**Category:** CUDA Programming and Performance\
**Created:** [January 20, 2011, 7:35am UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635 "2011-01-20T07:35:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdew](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mdew](https://forums.developer.nvidia.com/u/mdew)\
**Post date:** [January 20, 2011, 7:35am UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/1 "2011-01-20T07:35:04Z")

</div>

Hey, I want to use in-kernel printf.  
I have cuda toolkit 3.2, VS 2008.  
I have GTX 460.  
I build my app for compute 2.0.

When I put printf(“helo”); in my kernel I get following compiler error:

: error: identifier “printf” is undefined

Why? Do I have to include some special header to use printf?

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [January 20, 2011, 7:53am UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/2 "2011-01-20T07:53:33Z")

</div>

include stdio.h and it should work.

---

<div class="post-metadata">

**Author:** ![mdew](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mdew](https://forums.developer.nvidia.com/u/mdew)\
**Post date:** [January 20, 2011, 8:16am UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/3 "2011-01-20T08:16:40Z")

</div>

> [@](#):
>
> include stdio.h and it should work.

I compiled the code but now when I launch my kernel program crashes :( - why?

I build it on Realease config… should I use debug for printf or what?

It crashes on \> program.exe!\_\_tmainCRTStartup() Line 501 + 0xf bytes C

below is some strange file crtexe.c

```auto
/*

            * do C++ constructors (initializers) specific to this EXE

            */

            if ( __native_startup_state ==__ initializing)

            {

                _initterm( __xc_a,__ xc_z ); // <<<<<<<<<<<<<<<<<<<<<<<<<<< this line unhandled exception

                __native_startup_state =__ initialized;

            }

```

Okay it works now, something strange happened and I had to recompile whole project…

Thanks a lot for help :)

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [January 20, 2011, 8:24am UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/4 "2011-01-20T08:24:45Z")

</div>

mainCRTStartup is buried in the CUDA runtime library. A failure like that would generally suggest something in your build system, toolchain or CUDA toolkit is badly broken, but I can’t help with Windows or Visual Studio troubleshooting, sorry.

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [January 25, 2011, 12:54pm UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/5 "2011-01-25T12:54:44Z")

</div>

> [@](#):
>
> Hey, I want to use in-kernel printf.
> 
> I have cuda toolkit 3.2, VS 2008.
> 
> I have GTX 460.
> 
> I build my app for compute 2.0.
> 
> When I put printf(“helo”); in my kernel I get following compiler error:
> 
> : error: identifier “printf” is undefined
> 
> Why? Do I have to include some special header to use printf?

you can not use printf inside a kernel but there is an implementation called cuprintf.

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [January 25, 2011, 12:59pm UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/6 "2011-01-25T12:59:42Z")

</div>

> [@](#):
>
> you can not use printf inside a kernel but there is an implementation called cuprintf.

yes you can. ignore my comment

[External Image](http://forums.nvidia.com/public/style_emoticons/<#EMO_DIR#>/teehee.gif "Image hosted on another site. Click to open in a new tab.")

---

<div class="post-metadata">

**Author:** ![StephanieTaylor](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@StephanieTaylor](https://forums.developer.nvidia.com/u/StephanieTaylor)\
**Post date:** [January 25, 2011, 9:05pm UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/7 "2011-01-25T21:05:23Z")

</div>

> [@](#):
>
> include stdio.h and it should work.

I am having the same problem, and must be doing some boneheaded thing. I have compute capability 2.1, so I should be able to use printf. Here is my code:

#include \<stdio.h\>

**global** void test\_kernel() {

printf(“Can I do this?\n”);

} // end test\_kernel

int main(int argc, char \*arvc) {

test\_kernel\<\<\<1,1\>\>\>();

}

I compile it with

nvcc -o hello\_world hello\_world.cu

And encounter the compilation error:

hello\_world.cu(4): error: calling a host function from a **device** / **global** function is not allowed

Any help would be appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [January 25, 2011, 9:24pm UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/8 "2011-01-25T21:24:51Z")

</div>

Pass an architecture selection option to nvcc to tell it you want to compile for a compute 2.1 device, something like

```auto
nvcc -arch=sm_21 -o hello_world hello_world.cu

```

---

<div class="post-metadata">

**Author:** ![StephanieTaylor](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@StephanieTaylor](https://forums.developer.nvidia.com/u/StephanieTaylor)\
**Post date:** [January 26, 2011, 2:35pm UTC](https://forums.developer.nvidia.com/t/in-kernel-printf/20635/9 "2011-01-26T14:35:12Z")

</div>

> [@](#):
>
> Pass an architecture selection option to nvcc to tell it you want to compile for a compute 2.1 device, something like
> 
> ```auto
> nvcc -arch=sm_21 -o hello_world hello_world.cu
> 
> ```

Yeah!

I had to add the line

cudaThreadSynchronize();

after the kernel call, but now it is working.

Thank you very much.
