使用GCC编译为独立环境 [英] Compile for freestanding environment with GCC

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

问题描述

我正在编写的代码应该可以为托管和独立环境构建,为后者提供一些stdlib函数的私有实现。



我可以在普通的工作站/构建服务器上使用GCC进行可靠的测试吗?
$ b


  • -freestanding选项看起来很有前途,但它似乎只禁用内建并设置了STDC_HOSTED宏,它仍然提供所有系统标头。

  • 选项-nostdinc我仍然想使用独立实现所需的头文件(特别是stddef.h和limits.h)。




我在这里错过了什么?



哦,我现在使用GCC 4.4.3,将很快升级到4.5.0。

解决方案

好吧,既然没有给出答案,我还是可以描述我是如何完成这项工作的。这很简单,虽然取决于目标系统,但它可能很乏味。

使用-nostdinc表示包含路径的标准系统将被跳过;其他包含-I的包含路径当然仍会被搜索标题。



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


然后,C89目录可以用作'include-independentstanding-c99'的基础,要链接的额外头文件是 stdbool.h stdint.h em>



然后使用命令行

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

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


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.

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

  • 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.

  • 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).

What am I missing here?

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.

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.

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).

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

The command-line to use is then

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

or

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

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

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