59

What is ffreestanding in gcc ? What is it used for ? I came across the following :

gcc -ffreestanding -m32 -c kernel.c -o kernel.o

and do not understand, what does it mean exactly.

saplingPro
  • 20,769
  • 53
  • 137
  • 195

1 Answers1

90

A freestanding environment is one in which the standard library may not exist, and program startup may not necessarily be at "main". The option -ffreestanding directs the compiler to not assume that standard functions have their usual definition.

By default, GCC will act as the compiler for a hosted implementation, defining __STDC_HOSTED__ as 1 and presuming that when the names of ISO C functions are used, they have the semantics defined in the standard. To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding. It will then define __STDC_HOSTED__ to 0, and not make assumptions about the meanings of function names from the standard library.

For more Info, This link may help.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Dayal rai
  • 6,548
  • 22
  • 29
  • 1
    @Dayalrai I used `-ffreestanding` option with `arm-non-eabi-gcc` : `arm-non-eabi-gcc -g -O0 -ffreestanding ......`, but I still get main symbol by calling `nm` command `U main` which generate me debugging error with `gdb` : `Function "main" not defined`. how can I avoid generating this `main`symbol –  Apr 28 '15 at 09:01
  • 1
    Typically, `-ffreestanding` is used for compiling kernels. Besides, it is rarely used to compile applications which would directly interact with the kernel without making the use of a library. Normal applications would not use `-ffreestanding`. – Christian Hujer Oct 06 '15 at 10:56
  • I am running Dhrystone benchmark on Cortex-M7 and inclusion of `-ffreestanding` as compiler option causes drop in DMIPS/MHz number. So, it's better to avoid it in such cases. – Nee Mar 25 '22 at 06:29
  • As @Nee notes, compiling with `-ffreestanding` may well result in decreased performance of the compiled code. The reason for this is that **the `-ffreestanding` option implicitly enables `-fno-builtin`**, which means that you cannot benefit from the automatic substitution of any standard library function calls with GCC built-ins, such as `memcpy` being compiled directly into an inline copy loop. The [merits of this design decision](https://stackoverflow.com/a/18713375) aside, it is definitely something that one should be aware of when evaluating the use of `-ffreestanding`. – Cody Gray - on strike Apr 11 '23 at 06:51