2

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?

Shadow
  • 21
  • 1
  • https://stackoverflow.com/questions/17692428/what-is-ffreestanding-option-in-gcc - "The option -ffreestanding directs the compiler to *not assume that standard functions have their usual definition*." – user2864740 Jan 02 '19 at 03:22
  • Not assuming default definitions does not preclude functions from being available if/as they are declared *and linked*. See `-nostdinc` as well, which will probably elicit the expected behavior. – user2864740 Jan 02 '19 at 03:23
  • Also see https://stackoverflow.com/q/45458528/2864740 – user2864740 Jan 02 '19 at 03:26
  • Thanks user2864740. As I did not ask for explicitly linking the std libs, do you mean the behavior of -ffreestanding has been overlapped by the gcc default linkage script? – Shadow Jan 02 '19 at 03:36
  • Or do you know how to show the difference between a freestanding build and a non-freestanding build? Then I can understand the option better. By now, the freestanding option looks useless as actually I have to add -nostdinc or a few other options to make a build "freestanding". – Shadow Jan 02 '19 at 03:40
  • I think maybe the option was just less-optimally named.. maybe someone with history might know. – user2864740 Jan 02 '19 at 03:56

0 Answers0