开发者

Confused about the Linker in C++

I am new to C++ and trying to understand everything.开发者_StackOverflow社区 I know the Linker links the object file to any external files. Correct? Does the linker link the preprocessors? or just additional files?

For instance in a simple Hello World program. Would the linker link with my Hello World Program?


This is a good explanation.

Wikipedia on linkers


In a very general nutshell, the primarily function of the linker is to package your Hello World program together with any library routines you have called in your program, such as prinf, into an executable file that is ready for the OS to load into memeory and receive transfer of control. This packaging creates the arrangement of code in the program's address space. As part of this task, it this necessary for linker to "resolve" symbols in your code by, for example, assigning to the symbol an address offset within the executable image. The linker also creates a relocation table that contains pointers to certain global symbols that should be absolute memory addresses. Since absolute memory addresses cannot be known at link-time, they are determined at load-time by the OS loader, and the executable images is then patched with the correct absolute addresses before transfer of control.


Linker would link the library and .o files. The compiler will generate the .o files. preprocessor would be handled in compilation process.

For example

step 1: compile: g++ -c relatedFile.cpp

step 2: Link: g++ -o proName relatObjFile.o (external library)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜