Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between include and link when linking to a library?

Tags:

c

linker

What does include and link REALLY do? What are the differences? And why do I need to specify both of them? When I write #include math.h and then write -lm to compile it, what does #include math.h and -lm do respectively?

In my understanding, when linking a library, you need its .h file and its .o file. Does this suggest #include math.h means take in the .h file while -lm take in the .o file?

like image 641
RexYuan Avatar asked Mar 18 '23 02:03

RexYuan


2 Answers

The reason that you need both a header (the interface description) and the library (the implementation) is that C separates the two clearer than languages like C# or Java do. One can compile a C function (e.g. by invoking gcc -c <sourcefile>) which calls library code even when the called library is not present; the header, which contains the interface description, suffices. (This is not possible with C# or Java; the assemblies resp. class files/jars must be present.) During the link stage though the library must be there, even when it's dynamic, afaik.

With C#, Java, or script languages, by contrast, the implementation contains all information necessary to define the interface. The compiler (which is not as clearly separated from the linker) looks in the jar file or the C# assembly which contain called implementations and obtains information about function signatures and types from there.

Theoretically, that information could probably be present in a library written in C as well — it's basically the debug information. But the classic C compiler (as opposed to the linker) is oblivious to libraries or object files and cannot parse them. (One should remember that the "compiler" executable you usually use to compile a C program , e.g. gcc, is a "compiler driver" which interprets the command line arguments and calls the programs which actually do stuff, e.g. the preprocessor, actual compiler and actual linker, to create the desired output.)

So in theory, if you have a properly annotated library in a known location, you could probably write a compiler which compiles a C function against it without having function declarations and type definitions; the compiler would have to produce the proper declarations. The compiler would have to know which library to parse (which corresponds to setting a C# project "Reference" in VS or having a class path and name/class correspondence in Java).

It would probably be easiest to use a well-known debugging format like stabs or dwarf and extract the interface definitions from it with a little helper program which uses the API for the debug format, extracts the information and produces a C header which is prepended to every source file. That would be the job of the compiler driver, and the actual compiler would still be oblivious to that.

like image 141
Peter - Reinstate Monica Avatar answered Apr 06 '23 08:04

Peter - Reinstate Monica


It's because headers files contain only declaration and .o files (or .obj, .dll or .lib) contain definitions of methods. If you open an .h file, you will not see the code of methods, because that is in the libraries. One reason is commercial, because you need to publish your code and have the source code in your company. Libraries are compiled, so you could publish it. Header files only tell compiler, what classes and methods it can find in the library.

like image 37
Patrik Valkovič Avatar answered Apr 06 '23 10:04

Patrik Valkovič