This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: How to override standard printf defined in gcc library
Hi Andy,
Thanks a lot, it worked. On specifying -fno-builtin-fprintf and
-fno-builtin-printf, i got the results as expected. Ya, you are right , i
should think of using pipes as otherwise I would have to provide my own
definition of all the stream O/P functions that gcc provides (and that can
be error prone and might result in inconsistent behavior with change in gcc
version)
Thanks
Rahul
Andi Hellmund wrote:
>
> 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...
>>>
>>>
>>>
>>
>>
>
>
>
--
View this message in context: http://www.nabble.com/How-to-override-standard-printf-defined-in-gcc-library-tp26083189p26089615.html
Sent from the gcc - Help mailing list archive at Nabble.com.