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


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-tp26083189p26084284.html
Sent from the gcc - Help mailing list archive at Nabble.com.


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