0

I am trying to write a simple C program without including standard libraries and trying to compile and run with gcc. but I was getting segmentation fault( core dumped )

main.c:

int main(){}

commands executed:

  1. gcc main.c -c -ffreestanding
  2. gcc main.o -e main -nostdlib
  3. ./a.out

output:

Segmentation fault (core dumped)

any help would be greatly appreciated.

  • 3
    Programs built with `-ffreestanding` must be run in special ways, as by being part of the operating system. You cannot run them from the command line like ordinary hosted executable programs. You would need to add special code to start up the program. Like the start-up code in the standard C library. – Eric Postpischil May 08 '22 at 11:24
  • 1
    You run `freestanding` "programs" on refrigerators, keyfobs, elevators, micro wave ovens, kitchen scales, ..., ... not on computers running Windows or MacOS or Linux... If you really really really want to run such programs on your computer, what would happen is that your program would become the Operating System itself; your program would need to know how to access hard disks, keyboard, video, ..., ... – pmg May 08 '22 at 11:47
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 09 '22 at 06:20

1 Answers1

1

Besides the problem mentioned in the comments, there is also a problem during the link time. If you run it with the flag -nostdlib, it will ignore the symbol symbol _start, which is part of a default library, crt0.o.

joaopfg
  • 1,227
  • 2
  • 9
  • 18
  • Thank you everyone for helping. Even after removing the -ffreestanding I faced the same issue. As mentioned by John,I think the issue lies in symbol _start being ignored. Is there any simple way to call _start from within the program without including the standard library? –  Jagadeesh Reddy May 09 '22 at 19:00
  • Check this answer: https://stackoverflow.com/a/2548601/11709021 – joaopfg May 10 '22 at 18:19
  • Though, for 64-bit architecture, it will work with the flag -m64 instead of -m32. – joaopfg May 10 '22 at 18:21