This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to override standard printf defined in gcc library


Hey Rahul,

I can't really prove my statement by quoting the gcc source code,
because I don't know where this kind of "transformation" happens, but it
seems that if you call printf and fprintf without variadic arguments
(though just a string), gcc transforms this call into a cheaper call
than (f)printf:

printf -> puts
fprintf -> fwrite (for the prototype, just check the man page of fwrite(3)

Alternatively, you could use the gcc compiler option -fno-builtin or
-fno-builtin-fprintf and -fno-builtin-printf. In this case, gcc doesn't
transfrom the calls. Then you would just have to overwrite printf and
fprintf.

As a further alternative, you might think about using a named pipe for
example to redirect the output to the GUI. Then you wouldn't have to
overwrite default libc functions ...

Hope that helps,
Andi


RahulJain83 wrote:
> Hi Kai,
>
> Thanks for the reply, actually you are right for printfs, and my issue for
> printfs has been resolved after defining puts too.
>
> But issue with fprintf still remains... can you please help.. I am pasting
> here a sample application and commands to build them -
>
> [code]
> /*
> * ======================================================================
> *        Filename:  printf.c
> * ====================================================================== */
> #include <stdio.h>
> #include <stdarg.h>
> extern "C" int fprintf (FILE *__restrict __stream,
>                 __const char *__restrict __format, ...)
> {
>
>     int ret_status = 0;
>     //printf ("Called my version of fprintf\n");
>
>     va_list args;
>     va_start(args,__format);
>     ret_status = vfprintf(__stream, __format, args);
>     va_end(args);
>     return ret_status;
> }
>
> extern "C" int fputs (__const char *__restrict __s, FILE *__restrict
> __stream)
> {
>
>     int ret_status = 0;
>     //printf ("Called my version of fputs\n");
>
>     return fprintf(__stream, "%s", __s);
> }
>
> extern "C" int printf (__const char *__restrict __format, ...)
> {
>
>     int ret_status = 0;
>     //printf ("Called my version of printf\n");
>
>     va_list args;
>     va_start(args,__format);
>     ret_status = vprintf(__format, args);
>     va_end(args);
>     return ret_status;
> }
>
> extern "C" int puts (__const char *__s)
> {
>
>     int ret_status = 0;
>     //printf ("Called my version of puts\n");
>
>     return printf("%s\n", __s);
> }
>
> /*
> * ======================================================================
> *        Filename:  main.c
> * ====================================================================== */
>
> #include <stdio.h>
> int main (int argc, char **argv)
> {
>     fprintf (stdout,"This is a variable argument message - %s\n", "Rahul
> Jain");
>     fprintf (stdout,"This is a static argument message - Rahul Jain\n");
>
>     printf ("This is a variable argument message - %s\n", "Rahul Jain");
>     printf ("This is a static argument message - Rahul Jain\n");
>     return 0;
> }
> [/code] 
>
> Build commands (with gcc 342) 
> /usr/local/soft/gcc/3.4.2/bin/g++ -ggdb -o printf.o -c printf.c
> /usr/local/soft/gcc/3.4.2/bin/g++ -ggdb -o main.o -c main.c
> /usr/local/soft/gcc/3.4.2/bin/g++ -ggdb -o a.out printf.o main.o
>
> Build commands (with gcc 424) 
> /usr/local/soft/gcc/4.2.4/bin/g++ -ggdb -o printf.o -c printf.c
> /usr/local/soft/gcc/4.2.4/bin/g++ -ggdb -o main.o -c main.c
> /usr/local/soft/gcc/4.2.4/bin/g++ -o a.out printf.o main.o
>
> Please see issue comes with gcc 424 and not with gcc 342. do you have any
> idea ? Really appreciate your help ..
>
> Thanks
> Rahul
>
>
> Kai Ruottu-3 wrote:
>   
>> RahulJain83 wrote:
>>     
>>> Above code works fine for following fprintf calls (that pass variable
>>> argument list to fprintf) -
>>>
>>> fprintf (stdout,"This is a variable argument message - %s\n", "Rahul
>>> Jain");
>>>
>>> However, for following fprintf call (that is not taking any variable
>>> arguments), it seems fprintf from libgcc is getting picked up as then
>>> print
>>> comes on the screen instead of our Graphical interface window -
>>>
>>> fprintf (stdout,"This is a variable argument message - Rahul Jain\n");
>>>
>>> I am not sure why this is happening. Is fprintf implemented as macro,
>>> such
>>> that it is working for case1 and not for case2. Can anybody help.
>>>   
>>>       
>> Please check that those 'printf()'s haven't been changed to 'puts()'s 
>> !   Sometimes I found out that
>> GCC produces a call to 'puts()'  instead of 'printf()' with the simple 
>> "Hello World" program... Why
>> it does this kind of optimization, is unclear but when this happens, 
>> doing something similar with 'puts()'
>> could be your workaround...
>>
>>
>>     
>
>   


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]