如何使用GCC为独立的环境进行编译? [英] How to compile for a freestanding environment with GCC?

查看:120
本文介绍了如何使用GCC为独立的环境进行编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的代码应该可以在托管环境和独立环境中构建,并为后一种情况的某些stdlib函数提供私有实现。

The code I'm working on is supposed to be possible to build for both hosted and freestanding environments, providing private implementations for some stdlib functions for the latter case.

我能否仅在普通的工作站/构建服务器上使用GCC可靠地进行测试? 使用GCC为独立环境编译

Can I reliably test this with just GCC on a normal workstation/build server? Compile for freestanding environment with GCC


  • -ffreestanding选项看起来很有希望,但似乎仅禁用了内置功能并正确设置了STDC_HOSTED宏,它仍然提供了所有系统头文件。

  • The "-ffreestanding" option looked promising, but it seems that it "only" disables built-ins and sets the STDC_HOSTED macro properly, it still provides all system headers.

-nostdinc选项限制太严格;我仍然想使用独立实现所需的标头(尤其是stddef.h和limits.h)。

The option "-nostdinc" is too restrictive; I still want to use the headers required for a freestanding implementation (in particular stddef.h and limits.h).

我在这里想念什么?

哦,我目前使用的是GCC 4.4.3,很快就会升级到4.5.0。

Oh, and I'm using GCC 4.4.3 for the moment, will upgrade to 4.5.0 "soon".

推荐答案

由于没有给出答案,所以我不妨描述一下我是如何完成这项工作的。这很简单,尽管取决于目标系统可能很乏味。

Well, since no answer is given yet I'd might as well describe how I made this work. It's pretty simple although depending on the target system it can be tedious.

使用 -nostdinc表示将跳过标准系统包含路径;当然,仍将在搜索其他带有 -I的包含路径的标头。

Using "-nostdinc" means that the standard system include paths will be skipped; other include-paths given with "-I" will of course still be searched for headers.

因此,对于独立构建目标,我创建一个文件夹'include-freestanding-c89'并链接相关的系统头文件- float.h iso646.h limits.h stdarg.h stddef.h -在那里。其他标头可能会包含在其中,具体取决于您的平台,因此您可能必须进行一些研究并建立更多的链接(因此,如果您需要针对多个目标平台执行此操作,则很繁琐)。

So, for the freestanding build target I create a folder 'include-freestanding-c89' and link the relevant system headers -- float.h, iso646.h, limits.h, stdarg.h and stddef.h -- there. Other headers might be included in these, depending on your platform, so you might have to do some research and set up more links (hence the tediousness if you need to do this for several target platforms).

然后可以将C89目录用作'include-freestanding-c99'的基础,要链接的其他头文件是 stdbool.h stdint.h

The C89 directory can then be used as base for 'include-freestanding-c99', the extra headers to link are stdbool.h and stdint.h

要使用的命令行为

gcc -std=c89 -nostdinc -nostdlib -ffreestanding -I include-freestanding-c89 

gcc -std=c99 -nostdinc -nostdlib -ffreestanding -I include-freestanding-c99

这篇关于如何使用GCC为独立的环境进行编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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