Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean to link against something?

Tags:

I commonly hear the term "to link against a library". I'm new to compilers and thus linking, so I would like to understand this a bit more.

What does it mean to link against a library and when would not doing so cause a problem?

like image 978
user2030677 Avatar asked Apr 20 '14 01:04

user2030677


People also ask

What does linking mean in programming?

Linking is the process in which references to "externally" defined objects (code and data) are processed so as to make them operational. Traditionally linking used to be performed as a task after basic translation of the user program files, and the output of this stage is a single executable program file.

What happens during linking?

Linking − The linker is produces the final compilation output from the object files the compiler produced. This output can be a shared (or dynamic) library or an executable. It links the object files by replacing the undefined references with the correct addresses.

How is linking done?

Linking is a process of collecting and maintaining piece of code and data into a single file. Linker also links a particular module into system library. It takes object modules from assembler as input and forms an executable file as output for the loader.

Why is linking necessary in a program?

The linking step is necessary to resolve all the references to external functions and to include the machine code for those functions in the final executable. Why is "linking" a "separate step"? You need at least two "separate steps" to get the assembler output, and two separate steps after that to get an executable.


1 Answers

A library is an "archive" that contains already compiled code. Typically, you want to use a ready-made library to use some functionality that you don't want to implement on your own (e.g. decoding JPEGs, parsing XML, providing you GUI widgets, you name it).

Typically in C and C++ using a library goes like this: you #include some headers of the library that contain the function/class declarations - i.e. they tell the compiler that the symbols you need do exist somewhere, without actually providing their code. Whenever you use them, the compiler will place in the object file a placeholder, which says that that function call is to be resolved at link time, when the rest of the object modules will be available.

Then, at the moment of linking, you have to specify the actual library where the compiled code for the functions of the library is to be found; the linker then will link this compiled code with yours and produce the final executable (or, in the case of dynamic libraries, it will add the relevant information for the loader to perform the dynamic linking at runtime).

If you don't specify that the library is to be linked against, the linker will have unresolved references - i.e. it will see that some functions were declared, you used them in your code, but their implementation is nowhere to be found; this is the cause of the infamous "undefined reference errors".

Notice that all this process is identical to what normally happens when you compile a project that is made of multiple .cpp files: each .cpp is compiled independently (knowing of the functions defined in the others only via prototypes, typically written in .h files), and at the end everything is linked together to produce the final executable.

like image 90
Matteo Italia Avatar answered Sep 29 '22 11:09

Matteo Italia