user3533019
user3533019

Reputation: 303

How does a compiler find out which dynamic link library will be used in my code, if I only include headers-files, where is not describe it?

How does a compiler find out which dynamic link library will be used in my code, if I only include headers-files, where is not describe it?

#include <stdio.h>
void main()
{
    printf("Hello world\n");
}

There is I only include

 stdio.h

and my code is used

printf function

How it is known, in headers-files prototypes , macros and constant are described, but nothing about in which file "printf" is implement. How does then it works?

Upvotes: 3

Views: 2899

Answers (5)

Kerrek SB
Kerrek SB

Reputation: 477120

When you compile a runnable executable, you don't just specify the source code, but also a list of libraries from which undefined references are looked up. With the C standard library, this happens implicitly (unless you tell GCC -nostdinc), so you may not have been consciously aware of this.

The libraries are only consumed by the linker, not the compiler. 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. It is then the job of the loader to find appropriate libraries at load time and resolve the missing dependencies on the fly.

On Linux, you can use ldd to list the load-time dependencies of a dynamically linked executable, e.g. try ldd /bin/ls. (On MacOS, you can use otool -L for the same purpose.)

Upvotes: 8

Sergey L.
Sergey L.

Reputation: 22542

Technically your compiler does not figure out which libraries will be used. The linker (commonly ld) does this. The header files only tell the compiler what interface your library functions use and leaves it up to the linker to figure out where they are.

A source file goes a long path until it becomes an executable. Commonly

source.c -[preprocess]> source.i -[compile]> source.s -[assemble]> source.o -[link]> a.out

When you invoke cc source.c all those steps are done transparently for you in one go and the standard libraries (commonly libc.so) and executable loader (commonly crt0.o) are linked together.

Any additional libraries have to be passed as additional linker flags i.e. -lpthread.

Upvotes: 1

John Slade
John Slade

Reputation: 13042

As others have answered, the standard c library is implicitly linked. If you are using gcc you can use the -Wl,--trace option to see what the linker is doing.

I tested your example code:

gcc -Wl,--trace main.c

Gives:

/usr/bin/ld: mode elf_x86_64
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o
/tmp/ccCjfUFN.o
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/lib/x86_64-linux-gnu/libc.so.6
(/usr/lib/x86_64-linux-gnu/libc_nonshared.a)elf-init.oS
/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
-lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so)
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o

This shows that the linker is using libc.so (and also ld-linux.so).

Upvotes: 2

Shash
Shash

Reputation: 4250

The library glibc is linked by default by GCC. There is no need to mention -l library when you are building your executable. Hence you find that the functions printf and others which are a part of glibc do not need any linking exclusively.

Upvotes: 0

user3494614
user3494614

Reputation: 603

I would say that depends on IDE or the compiler and system. Header file just contains interface information like name of function parameters it expects any attributes others and that's how compiler first convert your code to an intermediate object file. After that comes linking where in code for printf is getting added to the executable either through static library or dynamic library.

Functions and other facilities like STL are part of C/C++ so they are either delivered by compiler or system. e.g on Solaris there is no debug version of C library unless you are using gcc. But on Visual Studio you have debug version msvcrt.dll and you can also link C library statically.

In short the answer is that code for printf and other functions in C library are added by compiler at link time.

Upvotes: -1

Related Questions