+ 2

Can anyone explain how file are linked during compilation?

For example, why do we only need to include the header file, and not the cpp one?

22nd Jul 2016, 5:57 PM
Aceitunarabe
Aceitunarabe - avatar
13 Answers
+ 2
What does 'contained fonction' means? And how/why choose dynamic or static linking?
22nd Jul 2016, 7:48 PM
Aceitunarabe
Aceitunarabe - avatar
+ 1
In the object files (*.obj, *.o) created by the compiler there is a table of contained functions. Depending on what kind of binary you create, i.e. executable (*.exe), statically linked (*.lib, *.a) or dynamically linked library (*.dll, *.s) the object file functions are either inserted into the (final) build artifact (executable, statically linked library) or referred to in the (final) build artifact. Consequentially, if an executable links dynamically (i.e. at runtime) to some libraries the libraries have to be bundled and deployed on a target system, while for statically linked build artifact that is not required as the include all functions required themselves.
22nd Jul 2016, 7:10 PM
Stefan
Stefan - avatar
+ 1
If you compile a source code file (*.c, *. cpp, *.cxx) the compiler creates an object file (*.obj, *.o). This object file contains what the source code file contains and a table that makes it easier for the linker to gain knowledge of what is in the file. Therefore, "contained functions" just refers to the functions (in binary form) that are inside of the object file. If you compile a source code file (*.c, *. cpp, *.cxx) the compiler creates an object file (*.obj, *.o). This object file contains the binary translation of the source code file and a table that makes it easier for the linker to gain knowledge of what is in the file. Therefore, "contained functions" just refers to the functions (in binary form) that are inside of the object file. Choosing between static and dynamic linking is a strategic decision. Since the content of linked static libraries is copied into the build artifact, the build artifact can become quite big as there's no reuse of the functions. No reuse means that, if you have a software that shares the same functions, each single executable will contain *all* the code it needs. It's standalone. This makes the software bigger. Bigger in the initial download on first deployment but also for subsequent updates of the functions in a static library as these functions now very likely are copied into several executables that must be updated now. Nevertheless, to have all functions you need in one file you deliver makes delivery less error-prone to missing files and dependencies on the target system). On the other hand, dynamically linked libraries can be shared between the executables which makes the software smaller, costs less bandwidth to deliver but is more error-prone to missing files and depencies on the target system.
22nd Jul 2016, 8:12 PM
Stefan
Stefan - avatar
+ 1
Which type of library you build is determined by compiler / linker switches.
22nd Jul 2016, 8:13 PM
Stefan
Stefan - avatar
0
Ok I get it, it's just the term function which is bothering me, can you give me an example? And What do including a header file do?
23rd Jul 2016, 4:07 AM
Aceitunarabe
Aceitunarabe - avatar
0
And for example with VS, how can you choose which type of library you link?
23rd Jul 2016, 4:54 AM
Aceitunarabe
Aceitunarabe - avatar
0
An example... So in my company we use statically linked libraries. Mostly because we wanted to modularize, not necessarily share code between executables. This may lead to a little bit longer iterations in development as a bit more rebuilding of application parts is going on. For deployment we will have to rethink that but for now it's a bit easier to use some tools.
23rd Jul 2016, 5:00 AM
Stefan
Stefan - avatar
0
About headers: Headers (apart from template headers) only *declare* classes/struct/functions etc., they do not *define* them. From mere declarations you cannot generate code, you can only do this from definitions aka the implementation. Headers tell that there is a class/struct/function etc. and it's basic structure. This is helpful when you compile against binary libraries (for which you do not have the source code of its implementation / definition as it's already compiled and available in the binary file). Now, compiling your code using this library, requires the compiler to know what elements exist in the binary library file (as you don't have the source code of the implementation). Compilers do not read the binary library file, they use the declarations from the header files to know what classes/structs/functions exist in the library. Summarized, one can say: headers present the class/structs/functions available (in binary libraries), to the programmer *and* to the compiler.
23rd Jul 2016, 5:27 AM
Stefan
Stefan - avatar
0
If it's a library you make, you can find it in the general project settings, what kind of artifact you build (executable/static library/dynamic lib).
23rd Jul 2016, 5:29 AM
Stefan
Stefan - avatar
0
If I understand well, you can choose to create 3 different things (and so there are 3 types of binary): -an executable in which you can use functions from libraries and create functions (such as int main ()?) -a statically linked library, in which all the functions are stored -a dynamically linked library which refers to functions which need to be already present in the system Are the function we are referring to here are function like a basic functions such as f()? The table of content created by the compiler is used to tell the linker that those functions will be used in the executable?
23rd Jul 2016, 10:57 AM
Aceitunarabe
Aceitunarabe - avatar
0
Almost right. The executable contains functions on it's own, at least main, right. Concerning the libraries something doesn't fit, yet. Static libraries do not have to contain all functions that exist as dynamic libraries can also contain functions etc. The difference is what the linker does when it uses a static or dynamic library. While linking is copying the required elements from a static library into the build artifact, linking a dynamic library just puts a reference to the the file and function in the build artifact. If you start a statically linked executable, the operating system only needs to read the executable (and maybe some system libraries) while for the dynamically linked executable it also needs to read all dynamically linked libraries as the executable does not contain the functions etc. itself but the reference to the library and it's used functions etc.
23rd Jul 2016, 11:14 AM
Stefan
Stefan - avatar
0
Ok thx again for the time you took to answer me! Another thing, correct me if I'm wrong : Iostream is a library, and cout is one of it's object, so a library can also contain object(in addition to function) ?
23rd Jul 2016, 11:56 AM
Aceitunarabe
Aceitunarabe - avatar
0
No problem. :-) Binary libraries can include classes, structs etc. Nevertheless, cout is a class instance, i.e. it's not only a type, it's an actual variable. Cout, is also defined and put in the library. (It's declaration in the iostream header uses the "extern" keyword, which tells the compiler that the actual allocated instance (the memory where the instance resides) is somewhere else (and in this case it is in the lib).
23rd Jul 2016, 12:38 PM
Stefan
Stefan - avatar