As I understand it the GCC compiler performs four steps when I compile a C program.
- Preprocessing - C code (*.c) with macros to C code without macros (*.c)
- Compiling - C code (*.c) to Assembly language (*.s)
- Assembling - Assembly language (*.s) to Object code (*.o)
- Linking - Object code (*.o) to executable (*)
The first three steps make perfect sense to me, but I am still confused as to what linking actually does.
After step three why can't I run the *.o file? At that point my C code is now in object/machine/byte code and can be interpreted by the CPU directly. Yet when I make my *.o file executable and try to run it I get this error:
bash: ./helloworld.o: cannot execute binary file: Exec format error
Why do I get this error? If I have a tiny C program (for example a hello world program) with only one C file it would appear to me that linking has no purpose because there's nothing to link. So what does linking in the compilation process actually do?
Thanks in advance for any replies.