问题
Though there are many tutorials online on how to make a cross compiler, I am actually not getting it.
I am using fedora 16
and already have gcc
installed. I am not sure about binutils
. How do I make a cross compiler for my own OS (the target)? I was reading here on os dev wiki but I don't get it. I mean, what do I do ? I just don't follow there steps.
Note : I want to build a cross compiler for the same architecture I am currently working on.I mean the same architecture that is running fedora.
回答1:
If the architecture of your own OS is the same than your running OS, then you do not need a new compiler, the same you already have will do. You may still need custom binutils
, though, but that depends on the format of the executables your own OS needs.
What I've done for my own toy OS is to make it understand ELF binaries: it is an absurdly easy format to load and run. And this way you can use the standard binutils
of your OS. You just have to link your executables with a custom linker script, and not to link with any linux library.
But note that many people consider this not such a good idea, but it may keep you going for a while.
My compiler command looks like:
%.o: %.c
gcc -c -o $@ -ffreestanding -fno-stack-protector -mpreferred-stack-boundary=2 -Os -g -Wall $<
And my linker command:
%.myexe: %.o
ld -o $@ -T myexe.ld $^ `gcc -print-libgcc-file-name`
For reference, the linker script is myexe.ld
:
ENTRY(main)
SECTIONS
{
.text :
{
*(.text)
}
.data :
{
*(.data)
}
.rodata :
{
*(.rodata)
}
.bss :
{
*(.bss)
}
}
来源:https://stackoverflow.com/questions/17675029/how-to-make-a-cross-compiler-using-gcc