Where is the implementation of included C++/C header files?

后端 未结 4 1622
我寻月下人不归
我寻月下人不归 2021-01-15 04:11

This may seem a little stupid:) But it\'s been bothering a while. When I include some header files which are written by others in my C++/C program, how does the compiler kno

4条回答
  •  鱼传尺愫
    2021-01-15 04:58

    The compiler toolchain contains at least two major tools: the compiler and the link editor (it is very common to name compiler the whole chain, but strictly speaking it is wrong).

    The compiler is in charge of producing object code from the available source code. In that phase the compiler knows where to locate standard headers, and can be told to use non-standard dirs to locate headers. For example, gcc uses -I to let you specify some more alternate dirs that may contains headers.

    The link editor is in charge of producing executable files (its basic common usage) from object codes. To produce an executable it needs to find every implementation of declared things at compile-time for which you didn't provide source code. These can be other object codes, object codes in libraries, etc. The link editor knows where are located standard libraries and can be told to let you specify non-standard dirs. For example you can tell the gcc toolchain to use alternate dirs with L that may contain libraries. You may be aware that link edition is now usually a two phase process: location-and-name-resolution at link-time and real link-edition at run-time (dynamic libraries are very common).

    Basically a library is just a collection of object code. Consult internet to see how you can easily build libraries either from source code of from object code.

提交回复
热议问题