Understanding C++ compilers from a Java / C# perspective

后端 未结 4 1955
有刺的猬
有刺的猬 2021-02-06 14:29

I\'m a moderately experienced Java / C# programmer, and I\'ve recently started learning C++. The problem is, I\'m having trouble understanding how to structure the various heade

4条回答
  •  孤独总比滥情好
    2021-02-06 14:35

    I would actually recommend staying away from explanations about C++ compilers and looking at explanations at C compilers. In my experience these are explained better and avoid confusing you with OOP issues. Look for material about C separate compilation. I would have referred you to a great slide booklet from my alma mater, but it's not in English.

    The main difference between C compilation and Java/C# is that the compilation does not create a resolved entity. In other words, when you compile in Java, the compiler looks for already-compiled class files for any referenced classes, and makes sure that everything is available and consistent. The underlying assumption is that when you eventually run the program, those files would also be available.

    A compiled C file, on the other hand, is just a "promise". It relies on a declaration of what the dependencies would look like (in the form of function declarations), but there are no guarantees that these are defined anywhere. The most difficult mindset switch that you need to do is to think of a C file not just as that file, but rather as the aggregation of that file with everything that it includes (i.e., what the preprocessor generates). In other words, the compiler does not see header files, it seems one large file. The compiler keeps track in the generated object file of everything that "is still missing". Later on, at link time, the linker makes settles this by trying to fill all the blanks with materials from the different object files.

提交回复
热议问题