redefining functions

Discussion in 'C Programming' started by andreyvul, Nov 24, 2008.

  1. andreyvul

    andreyvul Guest

    Suppose I have code like this:
    #include <stdio.h>
    int printf(const char *fmt, ...) {
    puts("Hello, world\n");
    }

    When compiling, I get an error that printf was previously defined in
    stdio.h (and it's correct).
    However, there are a lot of things that I need in stdio.h so I can't
    uninclude it.
    How do I tell the compiler to use the new printf without removing the
    #include?
     
    andreyvul, Nov 24, 2008
    #1
    1. Advertising

  2. On Nov 24, 12:30 pm, andreyvul <> wrote:
    > Suppose I have code like this:
    > #include <stdio.h>
    > int printf(const char *fmt, ...) {
    >  puts("Hello, world\n");
    >
    > }
    >
    > When compiling, I get an error that printf was previously defined in
    > stdio.h (and it's correct).
    > However, there are a lot of things that I need in stdio.h so I can't
    > uninclude it.
    > How do I tell the compiler to use the new printf without removing the
    > #include?


    Name your function something else. Redefining names from the standard
    library is an incredibly poor idea. Programmers reading your code
    (including you, if you leave it for a couple of weeks and come back to
    it) will expect standard names to do standard things. Furthermore,
    depending on your compiler and linker settings, the redefinition may
    or may not apply to the entire program, leading to baffling and
    inconsistent results between modules.

    Don't do it.

    -o
     
    Owen Jacobson, Nov 24, 2008
    #2
    1. Advertising

  3. andreyvul

    andreyvul Guest

    On Nov 24, 12:35 pm, Owen Jacobson <> wrote:
    > On Nov 24, 12:30 pm, andreyvul <> wrote:
    >
    > > Suppose I have code like this:
    > > #include <stdio.h>
    > > int printf(const char *fmt, ...) {
    > >  puts("Hello, world\n");

    >
    > > }

    >
    > > When compiling, I get an error that printf was previously defined in
    > > stdio.h (and it's correct).
    > > However, there are a lot of things that I need in stdio.h so I can't
    > > uninclude it.
    > > How do I tell the compiler to use the new printf without removing the
    > > #include?

    >
    > Name your function something else. Redefining names from the standard
    > library is an incredibly poor idea. Programmers reading your code
    > (including you, if you leave it for a couple of weeks and come back to
    > it) will expect standard names to do standard things. Furthermore,
    > depending on your compiler and linker settings, the redefinition may
    > or may not apply to the entire program, leading to baffling and
    > inconsistent results between modules.


    That's not possible without refactoring thousands of files of code.
    My intended effect is to use an #include for an old libc in order to
    partially implement a new libc.

    The real code is like this (<> #includes reference old libc):
    #include <stdio.h>
    extern int __new_printf(const char *, ...);
    #if LIBC_VER < 25
    /* reimplement printf */
    int printf(const char *fmt, ...) {
    return __new_printf(fmt, ...);
    }
    #endif
     
    andreyvul, Nov 24, 2008
    #3
  4. andreyvul

    andreyvul Guest

    On Nov 24, 12:42 pm, andreyvul <> wrote:
    > On Nov 24, 12:35 pm, Owen Jacobson <> wrote:
    >
    >
    >
    > > On Nov 24, 12:30 pm, andreyvul <> wrote:

    >
    > > > Suppose I have code like this:
    > > > #include <stdio.h>
    > > > int printf(const char *fmt, ...) {
    > > >  puts("Hello, world\n");

    >
    > > > }

    >
    > > > When compiling, I get an error that printf was previously defined in
    > > > stdio.h (and it's correct).
    > > > However, there are a lot of things that I need in stdio.h so I can't
    > > > uninclude it.
    > > > How do I tell the compiler to use the new printf without removing the
    > > > #include?

    >
    > > Name your function something else. Redefining names from the standard
    > > library is an incredibly poor idea. Programmers reading your code
    > > (including you, if you leave it for a couple of weeks and come back to
    > > it) will expect standard names to do standard things. Furthermore,
    > > depending on your compiler and linker settings, the redefinition may
    > > or may not apply to the entire program, leading to baffling and
    > > inconsistent results between modules.

    >
    > That's not possible without refactoring thousands of files of code.
    > My intended effect is to use an #include for an old libc in order to
    > partially implement a new libc.
    >
    > The real code is like this (<> #includes reference old libc):
    > #include <stdio.h>
    > extern int __new_printf(const char *, ...);
    > #if LIBC_VER < 25
    > /* reimplement printf */
    > int printf(const char *fmt, ...) {
    >    return __new_printf(fmt, ...);}
    >
    > #endif

    If the redefinition cannot happen using ANSI C90, feel free to use a
    gcc-specific method.
     
    andreyvul, Nov 24, 2008
    #4
  5. andreyvul wrote:
    > Suppose I have code like this:
    > #include <stdio.h>
    > int printf(const char *fmt, ...) {
    > puts("Hello, world\n");
    > }
    >
    > When compiling, I get an error that printf was previously defined in
    > stdio.h (and it's correct).
    > However, there are a lot of things that I need in stdio.h so I can't
    > uninclude it.
    > How do I tell the compiler to use the new printf without removing the
    > #include?


    The compiler will not "use the new printf" regardless of whether you
    remove the include or not. Standard 'printf' function is already defined
    in the standard library and an attempt to define another one (your own)
    will only result in a violation of definition rules of C language. This,
    once again, will happen regardless of whether you include 'stdio.h' or
    not (assuming that we are considering a hosted implementation).

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 24, 2008
    #5
  6. andreyvul <> writes:
    > Suppose I have code like this:
    > #include <stdio.h>
    > int printf(const char *fmt, ...) {
    > puts("Hello, world\n");
    > }
    >
    > When compiling, I get an error that printf was previously defined in
    > stdio.h (and it's correct).


    No, it was previously *declared* in stdio.h.

    > However, there are a lot of things that I need in stdio.h so I can't
    > uninclude it.
    > How do I tell the compiler to use the new printf without removing the
    > #include?


    As far as the C standard is concerned, you can't redeclare a standard
    function; any attempt to do so invokes undefined behavior. For
    example, the compiler is free to assume that any call to a function
    named "printf" is really a call to the standard one; one popular
    compiler can replace this:
    printf("Hello, world\n");
    with this:
    puts("Hello, world");

    Your implementation *might* let you do this somehow. Check your
    compiler's documentation.

    But there's another way: use a macro:

    #include <stdio.h>

    int my_printf(const char *fmt, ...) {
    puts("Hello, world\n");
    }

    #define printf my_printf

    int main(void) {
    printf("Good-bye, cruel world!\n");
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 24, 2008
    #6
  7. andreyvul

    andreyvul Guest

    On Nov 24, 12:42 pm, andreyvul <> wrote:
    > On Nov 24, 12:35 pm, Owen Jacobson <> wrote:
    >
    >
    >
    > > On Nov 24, 12:30 pm, andreyvul <> wrote:

    >
    > > > Suppose I have code like this:
    > > > #include <stdio.h>
    > > > int printf(const char *fmt, ...) {
    > > >  puts("Hello, world\n");

    >
    > > > }

    >
    > > > When compiling, I get an error that printf was previously defined in
    > > > stdio.h (and it's correct).
    > > > However, there are a lot of things that I need in stdio.h so I can't
    > > > uninclude it.
    > > > How do I tell the compiler to use the new printf without removing the
    > > > #include?

    >
    > > Name your function something else. Redefining names from the standard
    > > library is an incredibly poor idea. Programmers reading your code
    > > (including you, if you leave it for a couple of weeks and come back to
    > > it) will expect standard names to do standard things. Furthermore,
    > > depending on your compiler and linker settings, the redefinition may
    > > or may not apply to the entire program, leading to baffling and
    > > inconsistent results between modules.

    >
    > That's not possible without refactoring thousands of files of code.
    > My intended effect is to use an #include for an old libc in order to
    > partially implement a new libc.
    >
    > The real code is like this (<> #includes reference old libc):

    I'll reword the example to make it more sane:
    #include <stdio.h>
    extern int __c99_printf(const char *, ...);
    #if __STDC_VERSION__ < 199901L
    /* reimplement printf */
    int printf(const char *fmt, ...) {
    return __c99_printf(fmt, ...);
    }
    #endif
     
    andreyvul, Nov 24, 2008
    #7
  8. andreyvul

    WANG Cong Guest

    andreyvul wrote:

    > Suppose I have code like this:
    > #include <stdio.h>
    > int printf(const char *fmt, ...) {
    > puts("Hello, world\n");
    > }
    >
    > When compiling, I get an error that printf was previously defined in
    > stdio.h (and it's correct).
    > However, there are a lot of things that I need in stdio.h so I can't
    > uninclude it.
    > How do I tell the compiler to use the new printf without removing the
    > #include?


    If you are using gcc, you can try to put your own printf() into a
    separated library, and then use LD_PRELOAD to load that library.

    Thanks.
     
    WANG Cong, Nov 24, 2008
    #8
  9. andreyvul

    Andrey Vul Guest

    On Nov 24, 12:46 pm, Keith Thompson <> wrote:
    > andreyvul <> writes:
    > > Suppose I have code like this:
    > > #include <stdio.h>
    > > int printf(const char *fmt, ...) {
    > >  puts("Hello, world\n");
    > > }

    >
    > > When compiling, I get an error that printf was previously defined in
    > > stdio.h (and it's correct).

    >
    > No, it was previously *declared* in stdio.h.
    >
    > > However, there are a lot of things that I need in stdio.h so I can't
    > > uninclude it.
    > > How do I tell the compiler to use the new printf without removing the
    > > #include?

    >
    > As far as the C standard is concerned, you can't redeclare a standard
    > function; any attempt to do so invokes undefined behavior.  For
    > example, the compiler is free to assume that any call to a function
    > named "printf" is really a call to the standard one; one popular
    > compiler can replace this:
    >     printf("Hello, world\n");
    > with this:
    >     puts("Hello, world");
    >
    > Your implementation *might* let you do this somehow.  Check your
    > compiler's documentation.
    >
    > But there's another way: use a macro:
    >
    > #include <stdio.h>
    >
    > int my_printf(const char *fmt, ...) {
    >     puts("Hello, world\n");
    >
    > }
    >
    > #define printf my_printf
    >
    > int main(void) {
    >     printf("Good-bye, cruel world!\n");
    >     return 0;
    >
    > }

    I think trying to convert an existing function into a macro so that it
    can be overriden in some code could lead to headaches.
     
    Andrey Vul, Nov 24, 2008
    #9
  10. andreyvul

    WANG Cong Guest

    Richard Tobin wrote:

    > In article <ggepij$8f8$>,
    > WANG Cong <> wrote:
    >>If you are using gcc, you can try to put your own printf() into a
    >>separated library, and then use LD_PRELOAD to load that library.

    >
    > This is an operating system feature, not a gcc one.
    >


    Well, it's a feature of the dynamic linker, i can't say it
    is a part of an operating system.
     
    WANG Cong, Nov 24, 2008
    #10
  11. In article <ggepij$8f8$>,
    WANG Cong <> wrote:
    >If you are using gcc, you can try to put your own printf() into a
    >separated library, and then use LD_PRELOAD to load that library.


    This is an operating system feature, not a gcc one.

    -- Richard
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, Nov 24, 2008
    #11
  12. andreyvul

    jameskuyper Guest

    andreyvul wrote:
    ....
    > I'll reword the example to make it more sane:
    > #include <stdio.h>
    > extern int __c99_printf(const char *, ...);


    You're not allowed to define a function with that name; if the
    implementation has defined it, you code is highly non-portable.

    > #if __STDC_VERSION__ < 199901L
    > /* reimplement printf */
    > int printf(const char *fmt, ...) {
    > return __c99_printf(fmt, ...);


    I hope that '...' in the return statement was a typo. Otherwise you've
    got a lot of work to do before your code comes close to compiling. I'd
    recommend taking a look at vprintf() for a hint as to how this should
    be handled.

    > }
    > #endif
     
    jameskuyper, Nov 24, 2008
    #12
  13. Andrey Vul <> writes:
    > On Nov 24, 12:46 pm, Keith Thompson <> wrote:

    [...]
    >> But there's another way: use a macro:
    >>
    >> #include <stdio.h>
    >>
    >> int my_printf(const char *fmt, ...) {
    >>     puts("Hello, world\n");
    >>
    >> }
    >>
    >> #define printf my_printf
    >>
    >> int main(void) {
    >>     printf("Good-bye, cruel world!\n");
    >>     return 0;
    >>
    >> }

    > I think trying to convert an existing function into a macro so that it
    > can be overriden in some code could lead to headaches.


    I don't think there's any solution to your problem that doesn't lead
    to headaches. You can either redefine the problem or stock up on
    aspirin.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 24, 2008
    #13
  14. andreyvul <> writes:
    [...]
    > If the redefinition cannot happen using ANSI C90, feel free to use a
    > gcc-specific method.


    If you want a gcc-specific method, try a gcc-specific newsgroup such
    as gnu.gcc.help (assuming gcc's extensive documentation doesn't
    already answer your question).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 24, 2008
    #14
  15. andreyvul

    CBFalconer Guest

    andreyvul wrote:
    >
    > Suppose I have code like this:
    > #include <stdio.h>
    > int printf(const char *fmt, ...) {
    > puts("Hello, world\n");
    > }
    >
    > When compiling, I get an error that printf was previously defined
    > in stdio.h (and it's correct). However, there are a lot of things
    > that I need in stdio.h so I can't uninclude it. How do I tell the
    > compiler to use the new printf without removing the #include?


    You don't. You are not allowed to use the name 'printf'; that is
    reserved for the system. So name your routine something else.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Nov 24, 2008
    #15
  16. > How do I tell the compiler to use the new printf without removing the
    > #include?


    there are tricks to get around this
    but its better to not lie to the compiler
    and dont steal names reserved to the library

    arf meow arf - cats and dogs living together - who ya goin call
    its the end of the world as you know it - filler text goes here
    this is how the world ends - not with a whimper but with a bang
    this is how the world ends - not with a whimper but with a bang
     
    rand mair fheal, Nov 30, 2008
    #16
  17. On 30 Nov, 16:31, rand mair fheal <> wrote:

    > > How do I tell the compiler to use the new printf without removing the
    > > #include?

    >
    > there are tricks to get around this


    not in Standard C there aren't. There are compiler specific
    tricks to get around this. See other posts for details.

    > but its better to not lie to the compiler
    > and dont steal names reserved to the library


    true. But you may be experimenting with implementing
    your own version of the library.

    <snip>

    --
    Nick Keighley
     
    Nick Keighley, Dec 1, 2008
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Bassiliades

    Redefining an enumerated attribute type

    Nick Bassiliades, Dec 9, 2005, in forum: XML
    Replies:
    1
    Views:
    440
    Henry S. Thompson
    Dec 12, 2005
  2. uli
    Replies:
    5
    Views:
    4,845
    Dietmar Kuehl
    Apr 21, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,608
    Victor Bazarov
    Jun 21, 2004
  4. bberu
    Replies:
    7
    Views:
    640
    Ivan Vecerina
    Dec 18, 2004
  5. Replies:
    6
    Views:
    343
    Ron Natalie
    Jul 18, 2006
Loading...

Share This Page