5

I am trying to use my own printf function so i don't want to include standard include files... so I am compiling my code with -nostdinc

I have created my program something like this:

extern int printf(const char*,...); printf("Value:%d",1234); //printf("\n"); 

It is working fine for this code, but when I use printf("\n") then it is showing undefined reference to 'putchar'.

If i comment printf("\n"); then nm command is showing

$ nm test1.o U exit 00000000 T main U printf 00000030 T _start 

but if I use printf("\n"); then nm command is showing

$nm test1.o U exit 00000000 T main U printf U putchar 0000003c T _start 

I am not getting how and from where putchar is getting included

gcc version 4.8.2 (GCC)

4
  • 1
    Why tagged C++? Why not just use a different name that printf? Commented Jun 29, 2014 at 7:50
  • It is replaced by the compiler thought good intentions. Commented Jun 29, 2014 at 7:50
  • I can use different name but I want to know the behavior of compiler.. why it is including putchar when i use -nostdinc Commented Jun 29, 2014 at 7:52
  • I am usng -nostdlib also at the time of linking then i am getting undefined reference to 'putchar'. Commented Jun 29, 2014 at 8:01

1 Answer 1

6

gcc optimizes printf in certain situations. You can look at the function fold_builtin_printf here for the complete details. IIRC, it optimizes calls with one argument followed by a newline to puts/putchar. You can turn it off by specifying -fno-builtin(gcc docs).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Pradhan, It is working if i use -fno-builting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.