如何使用gcc制作交叉编译器? [英] How to make a cross compiler using gcc?

查看:95
本文介绍了如何使用gcc制作交叉编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在线上有很多关于如何制作交叉编译器的教程,但实际上我没有得到.

Though there are many tutorials online on how to make a cross compiler, I am actually not getting it.

我正在使用 fedora 16 ,并且已经安装了 gcc .我不确定 binutils .如何为自己的操作系统 (目标) 制作交叉编译器?我在操作系统开发者wiki上的此处阅读,但我不明白.我的意思是,我该怎么办?我只是不遵循那里的步骤.

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.

注意: 我想为当前正在使用的相同体系结构构建交叉编译器.我的意思是运行fedora的相同体系结构.

推荐答案

如果您自己的OS 的体系结构与运行中的OS相同,则不需要新的编译器,相同你已经有意愿了.不过,您可能仍需要自定义 binutils ,但这取决于您自己的操作系统所需的可执行文件的格式.

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.

我为自己的玩具操作系统 做的事情是使它理解ELF二进制文件:这是一种非常容易加载和运行的格式.这样,您就可以使用操作系统的标准 binutils .您只需要使用自定义的链接描述文件链接可执行文件,而不必链接任何Linux库.

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.

我的编译器命令如下:

%.o: %.c
        gcc -c -o $@ -ffreestanding -fno-stack-protector -mpreferred-stack-boundary=2 -Os -g -Wall $<

还有我的链接器命令:

%.myexe: %.o
        ld -o $@ -T myexe.ld $^ `gcc -print-libgcc-file-name`

作为参考,链接描述文件为 myexe.ld :

For reference, the linker script is myexe.ld:

ENTRY(main)

SECTIONS
{
    .text :
    {
        *(.text)
    }
    .data :
    {
        *(.data)
    }
    .rodata :
    {
        *(.rodata)
    }
    .bss :
    {
        *(.bss)
    }
}

这篇关于如何使用gcc制作交叉编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆