Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does compiler(gcc) know whether a function is to be dynamically linked or not?

Tags:

c

For example,printf is dynamically linked.

But how does the compiler(gcc) know that?

like image 750
mysql_go Avatar asked Apr 04 '11 06:04

mysql_go


People also ask

How does a compiler know if a function belongs to a statically linked source or a dynamically linked source?

The linker locates all the undefined references in the libraries. If the library is a static one, the linker just adds the actual machine code to your final executable. On the other hand, if the library is a shared one, the linker only records the name (and version?) of the library in the executable's header.

How are dynamic libraries linked?

Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.

Is C++ dynamically linked?

Overview of the C++ compilation steps: Here, libstdc++ is GNU's implementation of the C++ Standard Library which is automatically linked dynamically to your executable binary i.e. a. out . The ldd command lists the runtime library dependencies. libc is the C Standard library which is also linked dynamically.

What is the need of DLL differentiate between how dynamic and static linking?

The main difference between static and dynamic linking is that static linking copies all library modules used in the program into the final executable file at the final step of the compilation while in dynamic linking, the linking occurs at run time when both executable files and libraries are placed in the memory.


1 Answers

gcc doesn't know that. It knows there is a function printf and it knows how to call it, but the object file it generates contains a call to an unresolved symbol.

The symbol is then resolved by the linker, which is given all your object files and libraries. The linker finds the symbol printf in a library, and after its combined all the relevant modules, it updates the unresolved calls.

like image 189
Matt Curtis Avatar answered Oct 03 '22 11:10

Matt Curtis