Just for interest sake, I want to compile and run the simplest C program by hand;
//t.c
int main() {
return 0;
}
So I want to do:
$ cpp t.c
: should do nothing in this case, since no preprocessing to be done$ cc1 t.c
: should compile t.c
-> t.s
$ as -o t.o t.c
: should assembe t.s
-> t.o
$ ld t.o
: should produce executable a.out
(nothing really to be done)The problem I'm having is with as
, since running the final command yields:
ld: warning: cannot find entry symbol _start; defaulting to 00000000000400b0
What's going on? I purposly left out libc
to keep this as simple as possible, but I don't understand what's happening. What flags am I missing?
There's a bit more to loading and executing a program. As you can guess from the linker output, execution starts not at main
, but at _start
, which is provided by the CRT (C runtime) library that is bundled with the compiler and gets linked with your code behind the scene.
Here and here are some overviews of what's going on at program startup on Linux.
Do cc -v -Wall
on your dummy source to see all the required steps in detail.