I am reading an article about the c++ libraries and static/dynamic libraries and linking. My questions are the following:
1) We have to compile the files to be inserted into the library and then to use the command ar -crsv to create the library file? (is the archive which must begin with lib and end to the .a ?)
2) The command -I and -L of g++ are used once? to "Say" to the compiler where are the libraries? or every time on compilation of source code which makes use of library?
3) Libray must be included using #include<>? and if yes with what name?
I didn't understand well what is meant by static libraries and static linking and what with dynamic linking? What I did understand is that with static libraries and linking is when we are merge a program with the library so that no need of the original one, in the original location? For Dynamic what I know is the DDl which stand for dynamic link library. Then the only difference I can think of for this DDL is that is loaded into the main memory, but I am confused
Can anyone with knowledge to put my - may wrong - knowledge into correct order and to explain more this terms?
Thank you!
Let us say that your code is a file named main.cpp which looks like this:
#include<headerFromSomeLibrary>
#include<headerFromSomeOtherLibrary>
int main()
{
int var = functionFromTheLibrary();
int otherVar = functionFromTheOtherLibrary();
return var + otherVar;
}
Compilation will take place in two steps.
First, you will compile main.cpp into an object file, with a command like this one:
g++ -o main.o -c main.cpp -IheaderDirectory
where main.o is the name of the object file that will be generated and headerDirectory the path to the directory that contains the header files included in the main.cpp.
To be able to check if your syntax is correct, the compiler will need to know what the classes and functions that are called in main.cpp, but not defined there, look like (in this case, those come from the libraries, but it works the same if they are defined by you in another file).
That is where the #include directives come in: they point to the headers that contain the declarations of the functions that are called, and allow the compiler to do its work. If the declarations are in a header file called headerFromSomeLibrary.h, the corresponding directive will be:
#include<headerFromeSomeLibrary>
At this point, the resulting file (main.o) contains the low level version of the functions defined in main.cpp (only main() in this case). It also contains a lot of symbols, allowing to identify the functions that are defined in main.cpp and called by main.cpp.
The second step is the linking step. The linking command will look like this one:
g++ -o myProgram main.o -LsomeDirectory -lsomelibrary -lsomeotherlibrary
where myProgram is the name you want to give to the executable, headerDirectory is the path to the directory containing the header files, someDirectory is the path to the directory containing libsomeLibrary.a and libsomeOtherLibrary.a (the binaries of the libraries you are using).
Similarly to main.o, the libsomelibrary.a and libsomeotherlibrary.a contain the definitions of functions (namely, those used in main()), as well as the symbols identifying them. The role of the linking step is to use the symbols to connect the function definitions to the function calls.
Had the functions from the libraries been declared in a file myfunctions.h and defined in myfunctions.cpp, compilation instructions would have looked like these:
g++ -o main.o -c main.cpp
g++ -o myfunctions.o -c myfunctions.cpp
g++ -o myProgram main.o myfunctions.o
Basically, -I options are used to tell the compiler where the missing headers are, -l options to tell it the names of the missing binaries, and -L options to tell it where it can find those binaries. Those options don't "stay" from one call of g++ to the next (it wouldn't make sense).
Now, you asked about the difference between static and dynamic linking. What I explained above is actually static (i.e. compilation-time) linking. In the case of static linking, the compiler will go fetch the function definitions it needs in the libraries, and add them to the final executable. This is good because your executable won't need anything else to work, and because your compiler will be able to make optimizations after fetching the functions.
However, this is not always what you want to do. Some libraries are used by lots of different programs, and you can save a lot of space by sharing libraries among programs. That is what happens in the case of dynamic (i.e. runtime) linking. In this case, the program will simply fetch the libraries when it needs them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With