0

My question is regarding preprocessor and linking phase confusion in compilers (typically c++ compilers).

  1. From the online sources, its said that the compiler doesnt look at other source codes until the linking phase. But during the preprocessor phase of compiler, its said that all the include statements are replaced with their respective source code. Isnt other source files accessed during this phase then?

  2. Also, its said during linking phase, the functions of libs are linked. But all functions used in the code from libs are in the include statement and hence are already in the source code itself...right?

Please let me know what I am missing.

Ajay
  • 131
  • 8
  • Key term that may fill in some of the blanks: [translation unit](https://en.wikipedia.org/wiki/Translation_unit_(programming)). Translation units are all considered separately until the linking phase. – user4581301 Apr 27 '20 at 17:00
  • 1
    1. The linker doesn't work with source code at all - it works with object files only. The process is that the compiler will be invoked once for each compilations unit and during each such invocation it only looks at the source code of that compilation unit. Then the linker will take all of the object files created by the various compiler invocations and link them together without looking at any source code. 2. Unless you're doing something weird, the files you include are header files and thus do not contain any function definitions - only declarations. – sepp2k Apr 27 '20 at 17:17
  • @sepp2k but we can include .cpp files as well in source files. I am confused about that case. Sorry that I didnt specify this. – Ajay Apr 27 '20 at 17:20
  • 1
    @Ajay If you include source files into each other, you'll get multiple definitions errors at link time. If you do that, you specifically must not compile the included files and consequently not link them together afterwards. In other words including source files breaks separate compilation. So don't do that. – sepp2k Apr 27 '20 at 17:22

0 Answers0