Page 1 of 1

printf and puts problem...

Posted: Mon Jan 21, 2008 11:51 am
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]

Posted: Mon Jan 21, 2008 1:07 pm
by Wave
Where do you define printf()?

Posted: Mon Jan 21, 2008 1:13 pm
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.

Posted: Mon Jan 21, 2008 1:31 pm
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

Posted: Mon Jan 21, 2008 1:33 pm
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

Posted: Mon Jan 21, 2008 1:45 pm
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

Posted: Mon Jan 21, 2008 2:27 pm
by AlfaOmega08
proxy wrote: GCC does do basic function substitution when certain optimizations are enabled
how can I disable that optimizations?

Posted: Mon Jan 21, 2008 3:07 pm
by lukem95
using the -O argument you can control the optimisation level.

-O0 is none.

Posted: Mon Jan 21, 2008 5:21 pm
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.

Posted: Tue Jan 22, 2008 12:06 pm
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).

Posted: Tue Jan 22, 2008 1:07 pm
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.

Posted: Tue Jan 22, 2008 2:09 pm
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...