Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > redefining functions

Reply
Thread Tools

redefining functions

 
 
andreyvul
Guest
Posts: n/a
 
      11-24-2008
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?
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      11-24-2008
On Nov 24, 12:30*pm, andreyvul <andrey....@gmail.com> 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
 
Reply With Quote
 
 
 
 
andreyvul
Guest
Posts: n/a
 
      11-24-2008
On Nov 24, 12:35*pm, Owen Jacobson <angrybald...@gmail.com> wrote:
> On Nov 24, 12:30*pm, andreyvul <andrey....@gmail.com> 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
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      11-24-2008
On Nov 24, 12:42*pm, andreyvul <andrey....@gmail.com> wrote:
> On Nov 24, 12:35*pm, Owen Jacobson <angrybald...@gmail.com> wrote:
>
>
>
> > On Nov 24, 12:30*pm, andreyvul <andrey....@gmail.com> 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.
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-24-2008
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-24-2008
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) kst- <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"
 
Reply With Quote
 
andreyvul
Guest
Posts: n/a
 
      11-24-2008
On Nov 24, 12:42*pm, andreyvul <andrey....@gmail.com> wrote:
> On Nov 24, 12:35*pm, Owen Jacobson <angrybald...@gmail.com> wrote:
>
>
>
> > On Nov 24, 12:30*pm, andreyvul <andrey....@gmail.com> 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
 
Reply With Quote
 
WANG Cong
Guest
Posts: n/a
 
      11-24-2008
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.


 
Reply With Quote
 
Andrey Vul
Guest
Posts: n/a
 
      11-24-2008
On Nov 24, 12:46*pm, Keith Thompson <ks...@mib.org> wrote:
> andreyvul <andrey....@gmail.com> 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.
 
Reply With Quote
 
WANG Cong
Guest
Posts: n/a
 
      11-24-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
still confused about headers, inline functions, linking, and redefining pookiebearbottom@yahoo.com C++ 6 07-18-2006 05:55 PM
Redefining an enumerated attribute type Nick Bassiliades XML 1 12-12-2005 01:21 PM
Redefining operators in C++ bberu C++ 7 12-18-2004 08:32 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
redefining cout (for using printf/mexprintf) uli C++ 5 04-21-2004 04:04 AM



Advertisments