Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does linking in the compilation process actually do?

As I understand it the GCC compiler performs four steps when I compile a C program.

  1. Preprocessing - C code (*.c) with macros to C code without macros (*.c)
  2. Compiling - C code (*.c) to Assembly language (*.s)
  3. Assembling - Assembly language (*.s) to Object code (*.o)
  4. 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.


2 Answers

If I have a tiny C program (for example a hello world program)

Even your helloworld program does use #inlude<stdio.h>, doesn't it? That means you're using a library, and the linking step is there to combine the necessary object code (here the library code) to create a binary for you.


For a detailed descriptions of what the linking step does (and compare with compiling) - see this question

like image 142
artm Avatar answered Oct 18 '25 06:10

artm


Linking in rough explanation is:

  • Find all the matching segments from each object file, and concat them together. This way we end up with one large .code, one .data, one .bss etc.
  • Resolve all symbols that are used. Many symbols are local, so that they can be resolved immediately. Unresolved symbols will be searched for in the libraries requested to link with. When this is done, the result will be a symbol table / link map.
  • Make an file that is actually executable. On Linux, it usually just happens that both executable, libraries and object files all are in the ELF format. This is not true for all platforms.
like image 27
Stian Skjelstad Avatar answered Oct 18 '25 08:10

Stian Skjelstad