How the compiler knows where my main function is?

1.6k Views Asked by At

I am working on a project that contains multiple modules (source files, header files, libraries). One of the files in all that soup contains my main function.

My questions are:

  • How the compiler knows which modules to compile and which not?
  • How does the compiler recognize the module with the main() inside?
3

There are 3 best solutions below

0
On

How the compiler knows which modules to compile and which not?

It does not. You tell him which ones you want to compile, typically though the compilation statement(s) present in a makefile.

How does the compiler recognize the module with the main() inside?

Altogether it's a big process, already answered in this related question.

To summarize, while compiling a program with standard C library, the entry point of your program is set to _start. Now that has a reference to main() function internally. So, at compilation time, there is no (need for) checking the presence of main(). At linking time, linker should be able to locate one instance of main() which it can link to. That way, main() will serve as the entry point to your program.

So, to answer

How the compiler knows where my main function is?

It does (and need) not. It's the job of a linker, specifically.

1
On

The compiler itself doesn't care about what file contains which functions; main() is not special. However, in the linking stage, all these symbols from different files (and compilation units, possibly) are matched. The linker has a hidden "template" which has code at a fixed address that the OS will always call when you run a program. That code will call your main; hence, the linker looks for a main in all files. If it isn't there, you get an unresolved symbol error, exactly like if you used a function that you forgot to implement.

The same as for any other function applies to main: You can only have one implementation; having two main in two files that get linked together, you get a linker error, because the linker can't decide which of these to use.

0
On

The assembly code (often referred as startup code by embedded people) that starts up the program specifically calls main().
The prototype for main() is included in compiler documentation. When you compile a program, an object file is produced. The object file from your source code is then linked with a startup runtime component (usually called crt0.o[bj]) and the C library components, etc. If main() is changed to an unrecognizable signature, the compilation unit will complain about an unresolved external reference to _main or __main.