redefining functions

A

andreyvul

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?
 
O

Owen Jacobson

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
 
A

andreyvul

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
 
A

andreyvul

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.
 
A

Andrey Tarasevich

andreyvul said:
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).
 
K

Keith Thompson

andreyvul said:
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;
}
 
A

andreyvul

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
 
W

WANG Cong

andreyvul said:
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.
 
A

Andrey Vul

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


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.
 
W

WANG Cong

Richard said:
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.
 
R

Richard Tobin

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
 
J

jameskuyper

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.
 
K

Keith Thompson

Andrey Vul said:
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.
 
K

Keith Thompson

andreyvul said:
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).
 
C

CBFalconer

andreyvul said:
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.
 
R

rand mair fheal

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
 
N

Nick Keighley

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top