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?
It does not. You tell him which ones you want to compile, typically though the compilation statement(s) present in a makefile.
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 tomain()
function internally. So, at compilation time, there is no (need for) checking the presence ofmain()
. At linking time, linker should be able to locate one instance ofmain()
which it can link to. That way,main()
will serve as the entry point to your program.So, to answer
It does (and need) not. It's the job of a linker, specifically.