Why the printf function is still usable after adding the freestanding option

271 Views Asked by At

Generally, I've already read the following articles, checked the "freestanding/hosted" definition in gcc language standard, but not got my doubts resolved yet.

https://gcc.gnu.org/onlinedocs/gcc/Standards.html

https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html

Freestanding GCC and builtin functions

I'm using a hosted gcc/cygwin in Win7. I find that the generated .out, .map, or .exe files are same for different builds with -ffreestanding and without -ffreestanding.

the C file (test.c):

#include <stdio.h>

int main(int ac, char **av)
{
    printf("test1");
    return 0;
}

the command lines for the 2 different builds are listed below:

gcc test.c -o test1.exe -std=gnu99 -O2 -Wall -Wextra -Wl,-Map,test1.map

gcc test.c -o test2.exe -std=gnu99 -ffreestanding -O2 -Wall -Wextra -Wl,-Map,test2.map

Both the test1.exe and test2.exe can pass building and print "test1" when running. But I had thought that with -ffreestanding the compilation might fail due to "cannot find stdio header", "no standard lib included", or "cannot find printf implementation".

It seems that even with -ffreestanding option, a hosted gcc will not work as a freestanding gcc either.

Can anyone help to clarify it? Why the printf function is still usable after adding the freestanding option?

0

There are 0 best solutions below