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

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
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

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

Post 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 :)
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

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

Post by gerryg400 »

Just to be clear, did you write your own printf function ?
If a trainstation is where trains stop, what is a workstation ?
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

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

Post 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).
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

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

Post 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.
Every good solution is obvious once you've found it.
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

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

Post 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...
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

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

Post 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")
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

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

Post by Luns »

I think that explains my errors perfectly, thanks :)
Post Reply