How to compile for a freestanding environment with GCC?

后端 未结 2 1274
囚心锁ツ
囚心锁ツ 2020-12-15 04:46

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 t

相关标签:
2条回答
  • 2020-12-15 05:30

    This Xen Makefile uses gcc -print-search-dirs to get the directory with stddef.h and similar, adds it with -isystem, then uses -nostdinc to build:

    https://github.com/mirage/xen/blob/2676bc915157ab474ee478d929b0928cf696b385/stubdom/Makefile#L35

    0 讨论(0)
  • 2020-12-15 05:34

    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
    
    0 讨论(0)
提交回复
热议问题