title: How can I keep gcc -O2 from optimizing putchar out?
body: <p>I have an application that uses a custom putchar(); which until now has been working fine.
  I bumped up the optimization level of the application to -O2, and now my putchar isn't used.
  I already use <em>-fno-builtin</em>, and based on some googling I added <em>-fno-builtin-putchar</em> to my CFLAGS, but that didn't matter.
  Is there a "correct" way to get around this or do I have to go into my code and add something like </p>
  
  <pre><code>#define putchar myputchar
  </code></pre>
  
  <p>to be able to use -O2 and still pull in my own putchar() function? </p>
  
  <p><strong>edit--</strong>
  Since my original post of this question, I stumbled on <em>-fno-builtin-functions=putchar</em>, as yet another gcc commandline option.  Both this and the one above are accepted by gcc, but don't seem to have any noticeable effect.</p>
  
  <p><strong>edit more--</strong>
  Experimenting further I see that gcc swallows <em>-fno-builtin-yadayada</em> also, so apparently the options parsing at the gcc front end is just passing the text after the second dash to some lower level which ignores it.</p>
  
  <p><strong>more detail:</strong>
  Three files try1.c, try2.c and makefile...</p>
  
  <p>try1.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int
  main(int argc, char *argv[])
  {
          putchar('a');
          printf("hello\n");
          return(0);
  }
  </code></pre>
  
  <p>try2.c:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int
  putchar(int c)
  {
          printf("PUTCHAR: %c\n",c);
          return(1);
  }
  </code></pre>
  
  <p>makefile:</p>
  
  <pre><code>OPT=
  
  try: try1.o try2.o
          gcc -o try try1.o try2.o
  
  try1.o: try1.c
          gcc -o try1.o $(OPT) -c try1.c
  
  try2.o: try2.c
          gcc -o try2.o $(OPT) -c try2.c
  
  clean:
          rm -f try1.o try2.o try
  </code></pre>
  
  <p>Here's the output:
  Notice that without optimization it uses the putchar I provided; but with -O2 it gets it from some other "magic" place...</p>
  
  <pre><code>els:make clean
  rm -f try1.o try2.o try
  els:make
  gcc -o try1.o  -c try1.c
  gcc -o try2.o  -c try2.c
  gcc -o try try1.o try2.o
  els:./try
  PUTCHAR: a
  hello
  els:
  els:
  els:
  els:make clean
  rm -f try1.o try2.o try
  els:make OPT=-O2
  gcc -o try1.o -O2 -c try1.c
  gcc -o try2.o -O2 -c try2.c
  gcc -o try try1.o try2.o
  els:./try
  ahello
  els:
  </code></pre>