printf and puts problem...

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

printf and puts problem...

Post by AlfaOmega08 »

In the main function of my os, I've writed:

Code: Select all

printf("printf test\n");
printf("String: %s", "Hello World!");
printf("Signed: %d & %d", 20, -20);
printf("Unsigned: %u", -20);
printf("Hex: %x", 255);
When linking, ld exit with the error:
undefined reference to 'puts'

if i write

Code: Select all

pritnf("\n");
there is an undefined reference to 'putchar'

Maybe GCC excange printf with putchar or puts?[/code]
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post by Wave »

Where do you define printf()?
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

Maybe GCC excange printf with putchar or puts?
No,
undefined reference to 'puts'
means GCC can't find puts,so you should include the file which you've defined puts in it.
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Post by AlfaOmega08 »

I've defined it in stdio.h as
int printf(const char *format, ...);

then in stdio.c there is the source

but the error is from another source file where the call is
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Post by AlfaOmega08 »

abuashraf wrote:
AlfaOmega08 wrote: undefined reference to 'puts'
means GCC can't find puts,so you should include the file which you've defined puts in it.
I know. But first I've a video::puts function, not a puts function, and second, there isn't any reference to puts in the source file
User avatar
proxy
Member
Member
Posts: 108
Joined: Wed Jan 19, 2005 12:00 am
Contact:

Post by proxy »

GCC does do basic function substitution when certain optimizations are enabled. For example.

printf("a"); -> putchar('a');
printf("hello\n") -> puts("hello");

basically if there is no format character in the string then it may be reduced to a simpler function.

proxy
User avatar
AlfaOmega08
Member
Member
Posts: 226
Joined: Wed Nov 07, 2007 12:15 pm
Location: Italy

Post by AlfaOmega08 »

proxy wrote: GCC does do basic function substitution when certain optimizations are enabled
how can I disable that optimizations?
User avatar
lukem95
Member
Member
Posts: 536
Joined: Fri Aug 03, 2007 6:03 am
Location: Cambridge, UK

Post by lukem95 »

using the -O argument you can control the optimisation level.

-O0 is none.
~ Lukem95 [ Cake ]
Release: 0.08b
Image
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

Also, make sure you are compiling as "free standing", so it's not trying to use the actual printf made for something other than your operating system.
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Post by skyking »

Actually it's -fno-builtin that does the trick...

And -nostdlib to avoid linking with the libraries used on the OS that gcc is on (or -nodefaultlibs to avoid gcc provided library functions).
Ready4Dis
Member
Member
Posts: 571
Joined: Sat Nov 18, 2006 9:11 am

Post by Ready4Dis »

skyking wrote:Actually it's -fno-builtin that does the trick...

And -nostdlib to avoid linking with the libraries used on the OS that gcc is on (or -nodefaultlibs to avoid gcc provided library functions).
Hmm.. the only things I use are:
-fno-leading-underscore -ffreestanding -fpack-struct

I don't want the leading underscore, since it makes my C -> asm easier (as well as linking libraries), freestanding was so it doesn't link the regular C library (or so I thought, and it works for me, so i dunno), and pack-struct because I wanted all my structures packed unless I specifically tell it not to be (so I am 100% positive my structs are what I think they are, and if I compile for 64-bit I don't want it all breaking unless I already know it's going to). Anyways, those are the only arguments I use for gcc and my kernel and drivers compile without issue.
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Post by skyking »

Ready4Dis wrote:
skyking wrote:Actually it's -fno-builtin that does the trick...

And -nostdlib to avoid linking with the libraries used on the OS that gcc is on (or -nodefaultlibs to avoid gcc provided library functions).
Hmm.. the only things I use are:
-fno-leading-underscore -ffreestanding -fpack-struct

I don't want the leading underscore, since it makes my C -> asm easier (as well as linking libraries), freestanding was so it doesn't link the regular C library (or so I thought, and it works for me, so i dunno), and pack-struct because I wanted all my structures packed unless I specifically tell it not to be (so I am 100% positive my structs are what I think they are, and if I compile for 64-bit I don't want it all breaking unless I already know it's going to). Anyways, those are the only arguments I use for gcc and my kernel and drivers compile without issue.
Yes there are different ways things can be done. -O0 is however none of them in this case...
Post Reply