Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include directories vs. lib directory concept question

Tags:

c++

What is the difference between linking to include files versus linking to lib files?

I am fairly new to C/C++ and I'm having a hard time figuring out the difference between using include files and a static lib file to call functions. In my mind, the include files have functions that one can call just like .lib files.

like image 992
foboi1122 Avatar asked Feb 16 '11 21:02

foboi1122


People also ask

What is a lib directory?

lib – Application specific libraries. Basically, any kind of custom code that doesn't belong under controllers, models, or helpers. This directory is in the load path.

What is #include directory in C++?

#include <stdio.h> The example adds the contents of the file named stdio. h to the source program. The angle brackets cause the preprocessor to search the directories that are specified by the INCLUDE environment variable for stdio. h , after it searches directories that are specified by the /I compiler option.

What is the use of lib folder?

lib is short for library which is often used for common files, utility classes, imported dependencies, or 'back in the days' also for dlls for (desktop) applications. It's in general a 'library' of supporting code for the core application.

What does LIB file contain?

lib file contains all the code and data for the library. The linker then identifies the bits it needs and puts them in the final executable. For a dynamic library, the . lib file contains a list of the exported functions and data elements from the library, and information about which DLL they came from.


2 Answers

In C++ (and C and other similar languages) a function is said to have both a declaration and a definition.

The declaration is simply a short statement that declares that the function exists, and what its interface looks like. Consider a basic function add that adds two integers together. Its declaration might look like the following:

int add(int, int); 

This means "there exists a function add that takes two integers and returns an integer". It does not specify what the function actually does, despite the fact that we can make a good guess based on its name.

The definition of the function is where we define exactly what the function does. This might be what you consider to be the actual function code. Using the add function as an example:

int add (int a, int b) {     return a + b; } 

So how does this fit with your question? Well, suppose we have a number of math functions in math.cpp:

// math.cpp  int add (int a, int b) {     return a + b; }  int sub(int a, int b) {     return a - b; } 

And also suppose we decide to use some of these in our main function in main.cpp:

// main.cpp  #include <iostream>  int main (int argc, char* argv[]) {     std::cout << "1 + 2 = " << add(1, 2) << std::endl;     std::cout << "8 - 3 = " << sub(8, 3) << std::endl; } 

If you try to compile main.cpp as it is, it will complain that it doesn't know what add and sub are. This is because you are trying to use them without declaring that they exist - which is exactly what a declaration is for. So you might do the following:

// main.cpp  #include <iostream>  int add(int, int); int sub(int, int);  int main (int argc, char* argv[]) {     std::cout << "1 + 2 = " << add(1, 2) << std::endl;     std::cout << "8 - 3 = " << sub(8, 3) << std::endl; } 

This would work, but is not very flexible. If we add a new function mul, we need to go and add its declaration to main.cpp and every other .cpp file that uses it (which is a lot of work if you have a lot of .cpp files). So what we do is instead we put all the declarations into a single file (say, math.h) so we only have to maintain the list of declarations in a single spot. Then, we simply include math.h into any file that uses the math functions. This is the purpose of header files (a.k.a. include files).

This works great, but could be even better. As it is, we have a main.cpp file, and a math.cpp file, both of which are compiled every time you compile the program*. If your math functions don't change at all, surely it's better to compile them once and just insert the pre-compiled definitions into your executable whenever you recompile main.cpp? That is exactly the purpose of .lib files. They contain the pre-compiled code for definitions of the relevant functions. You still need the include file to let you know what functions exist in the lib.

The purpose of the linking stage of compilation is to take these pre-compiled functions and the functions you have just compiled, and roll them together into a single executable file.

Essentially, you can look at a static lib as the pre-compiled code for a number of predefined functions, and its matching include file as a tool to let any code wanting to use those functions know which are available and what their description is.


* This is not strictly true, but is sufficient for our purposes here.
like image 200
Mac Avatar answered Oct 23 '22 12:10

Mac


To provide a simpler answer:

.lib files are precompiled libraries. If you include a .lib, you also need to include the .h/hpp header files, so your compiler knows how to access the functions in the .lib.

When you compile your program, all the functions used from the lib are only linked, they are not compiled again.

like image 34
cppanda Avatar answered Oct 23 '22 13:10

cppanda