Page 1 of 1

How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 6:59 am
by Luns
I'm trying to compile a cross-compiler, but I'm not sure if it worked correctly. When I try calling printf, ld complains about undefined references to functions that I assume some standard version of printf uses - like puts and putchar. I thought one of the points of the cross compiler was that it wouldn't pull extra library stuff in like that?

I compiled gcc-4.6.1 with these options: `./configure --target=i586-elf --prefix=/usr/local/cross --disable-nls --enable-languages=c --without-headers`, and it seems to know what the target should be:

Code: Select all

configure:2316: checking target system type
configure:2329: result: i586-pc-elf
binutils-2.21 was compiled with `./configure --target=i586-elf --prefix=/usr/local/cross --disable-nls` and also had the target set correctly.

I compile and link my kernel by adding /usr/local/cross/bin to my path, and then using i586-elf-gcc and i586-elf-ld like I would use gcc/ld normally: `i586-elf-gcc -m32 -o out.o -c in.c -Wall -nostdlib -nostartfiles -nodefaultlibs` and `i586-elf-ld -T link.ld -melf_i386 -o kernel.bin out.o`

Is this how a cross-compiler should be working, or did I do something wrong?

Oh, and the host system I'm using is Arch Linux. Any help is much appreciated :)

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 8:26 am
by gerryg400
Just to be clear, did you write your own printf function ?

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 8:47 am
by Luns
Sorry, I should have noted that - yeah, I did (I know I'm not really supposed to call my version printf, but I wrote it a long time ago and I haven't felt like going back through all my source files to change the name).

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 8:50 am
by Solar
Luns wrote:I compile and link my kernel by adding /usr/local/cross/bin to my path, and then using i586-elf-gcc and i586-elf-ld like I would use gcc/ld normally: `i586-elf-gcc -m32 -o out.o -c in.c -Wall -nostdlib -nostartfiles -nodefaultlibs` and `i586-elf-ld -T link.ld -melf_i386 -o kernel.bin out.o`

Is this how a cross-compiler should be working, or did I do something wrong?
No, that's the general idea. You might want to add -nobuiltins, and sift through the GCC docs to check if there are additional "no's" that you should add. (4.6.1 is somewhat beyond what I ever used for OSDev work, and options might have changed.)

Not naming your functions after standard functions, unless they *are* implementing the standard functionality, is a good design choice.

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 10:00 am
by Luns
Apparently that option has been changed to -fno-builtin, and that worked perfectly. Thank you!

I'll get around to changing that name one of these days...

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 3:31 pm
by Owen
-fno-builtin is generally a bad idea. It significantly hampers GCC's optimization ability, though it will require your libc functions to be conformant, and will cause some calls to be transformed into simpler alternate forms. For example, printf("Hello world\n") -> puts("Hello world")

Re: How do I know if I compiled my cross-compiler correctly?

Posted: Fri Jul 22, 2011 6:23 pm
by Luns
I think that explains my errors perfectly, thanks :)