Dealing with libraries in C Plus Plus | C++ Database
The concept of software libraries inC++
the library(Library)It is a file that can contain ready-made classes and functions. Once included in the project, you will be able to use everything in it as if it were part of the project. Usually when the developer finds himself always using the same classes and functions in his projects, he puts them inside one library and whenever he needs them he includes them in his project.
Now, you need to know that there are two types of libraries that can be included in the project:
static libraries(Static Libraries)And its extension is
.lib
on Windows and.a
on Linux and Mac systems.dynamic libraries(Dynamic Libraries)And its extension is
.dll
on Windows and.so
on Linux and Mac systems.
The library that you create yourself or that you download from the Internet, you can place it anywhere you want on your computer, and by this we mean that you are not obligated to put the libraries in a specific place to allow you to use them. Later when the library needs to be used in any project, the compiler must be told where it is so that it will be able to include any file in it and start using the code in it.
The best and easiest for you is always to allocate a place on your computer to put all the libraries that you may use.
The importance of building a project or library inC++
From the beginning of the course until now we always click on the button Build and run
when we want to start it and not just the buttonRun,In other words, we were building the project before it was built.
The following image reminds you of the names of the buttons we use in the programCodeBlocksTo build and operate the project.
The purpose of building the project is for the compiler to ensure that the project code does not have any problems in terms of writing commands(Syntax Error)And then convert all project code(Compile)For an executable file with the extension ..exe
.
Project run is intended to run only the executable file that is the final program resulting from the project build process.
The difference between saving(Save)and construction(Build)
Saving project files means saving only the code that was written in them.
Therefore, if you only save the project files without building them, you will always see the result of the last build you did when running.
Starting point in the library
When we prepare a library, we are preparing code that can be included in projects, and not code that is a program in itself.
For this reason, the library is not supposed to put a function in its name, which main()
we consider as a starting point, as we do in normal projects.
Not having a starting point in the library means you can doBuildonly her and notRunBecause it is not in itself a program that can be run.
The importance of building the library after any update has been made to it
Rebuild the library after you have finished writing the special code in it each time by clicking on the buttonBuildIt is very important for a new copy of the library to be produced, its type .lib
, or , or .a
according to the type of library.dll
.so
(staticorDynamic)And the type of operating system on which the library is built, as mentioned previously.
The difference between static libraries and dynamic libraries inC++
In terms of dealing with the code in the libraries, whether you are using a static library or a dynamic library, there is no difference in this regard.
The main difference between the two types is that static libraries are combined with the code of the project itself when you build the project. Whereas the dynamic libraries are kept separate from the project code when you build it.
Every time you modify the code of a static library that you use in your project, you must rebuild the library code and then the project code that uses it to make the project use the last updated version of it. But if you make a modification to the dynamic library code in your project, you must rebuild the library code only without the need to rebuild the project code.
Technical information
Sometimes when you try to run a program on your computer, an error appears telling you that there is a .dll
missing file that the program needs to run as follows.
This error simply means that the program you are using is basically using the dynamic library MSVCR71.dll
whose name was mentioned and which it could not find.
To solve this problem, you usually search the Internet for the name of the mentioned file (ie the library) that the application needs in order to work.
In our case, we are looking for the file MSVCR71.dll
and after finding it we add it in the program files only.
How to create a library and use it inC++
At the outset we would like to point out that we are using a programCodeBlocksIn the application of the explanation and therefore we will explain how to add external libraries by it.
If you are using another program, the way you include the library you create in the project will be slightly different because each program has a different interface to work with.
Now, in order to make the explanation simplified to the fullest extent and cover all the information you need to know when dealing with libraries, we will divide the example into four basic steps:
Project establishmentC++Normal because we'll use it to try out the library.
Create another project for the library we will include in the first project.
Add the library in the project and call the functions in it.
Re-modifying the library and adding its extension files
.cpp
and.h
placing the code inside a domain(Namespace).
Steps to create a new project using CodeBlocks
Carefully follow the below steps to create a projectC++its nameTest
.
tap on
File
,thenNew
,ThenProject...
as follows.
Click on
Console application
the buttonGo
as follows.
Click on
++C
and then click onNext
as follows.
Specify the name that you want to put the project and the folder path where you want it to be saved on your computer, then click
Next
as follows.
Do not modify anything on this page and click on the
Finish
following.
After the project is successfully created, it will appear in the list of projects as follows.
Now go back to the lesson and go to the second step, from which you will learn how to create the library harmash
, which you will return later and include it in this project.
Steps to create a static library in CodeBlocks
Follow the steps below carefully to create a project named harmash
after it that produces a stable library named after itlibharmash
.
tap on
File
,thenNew
,ThenProject...
as follows.
Click on
Static library
the buttonGo
as follows.
Check the option
Skip this page next time
and then click on theNext
following so that you do not see this page once every time you want to create a library in the future.
Specify the name that you want to put the project and the folder path where you want it to be saved on your computer, then click
Next
as follows.
Do not modify anything on this page and click on the
Finish
following.
After the project is successfully created, it will appear in the list of projects as follows.
Since we want to write code in a languageC++And not in a languageCWe will change the file extension
main
to.cpp
as follows.
Name the file
main.cpp
and then click on the buttonOK
as follows.
Open the file
main.cpp
and delete any default code you find in it as follows.
Then write the following code inside it to define a function whose name will
printMsg()
print a regular statement when called.
#include <iostream> void printMsg() { std::cout << "First time using a static library!"; }
After adding the code in the file
main.cpp
, click on the Save all changes made button.
The last step you should do is to click on the Build button(Build)So a library is produced from the project as follows.
Note that it told you that a library named afterlibharmash.a
the path was createdbin\Debug
in the project itself.
If we view project files from outside the programCodeBlocksWe will find the library libharmash.a
as follows.
Now go back to the lesson and go to the third step, from which you will learn how to include the library libharmash.a
in the projectTest
.
Steps to add a library in the project using CodeBlocks
Follow these steps carefully to add the library libharmash.a
in the project Test
and then call the functions in the library that have been added to make sure that this is done successfully.
At first close any open file so as not to distract yourself because of the open files as follows.
Right-click on the project name,
Test
then clickBuild Options...
as follows.
Click on
Linker Settings
because it is the section where you can add libraries in the project.
Click on the button
Add
and then on the folder icon to start adding the library in the project as follows.
After selecting the library you want to add - in our case the library
libharmash.a
- click on the buttonOpen
as follows.
Click the button
No
to save the full library path(Full Path)As follows.
Click on the button
Ok
below to add the library.
Click on the button
Ok
as follows until the window closes or closes by clicking on the close button.
Right-click on the project name,
Test
then clickActivate Project
as follows.
Open the file
main.cpp
in the projectTest
as follows.
Write the following code inside the file
main.cpp
to call the functionprintMsg()
in the librarylibharmash.a
that will print a regular statement for you when it is executed.
#include <iostream> using namespace std; // Noting that when main() is called so we can call it in the printMsg() function here we mentioned what the function looks like // libharmash.a The compiler will look for its definition until it finds it, and of course in our case it will find it inside the library void printMsg(); int main() { // printMsg() Here we have called the function printMsg(); return 0; }
Click on the button
Build and run
as follows until the file is savedmain.cpp
and then build and run the projectTest
.
You will see the following result after the project is run
Test
.
Now go back to the lesson and go to the fourth and final step where we will add files .cpp
and .h
then include them in the project as Test
well.
Steps to include library files using CodeBlocks
Follow the steps below carefully to add a file named Person.h
and file name Person.cpp
in the projectHarmash
.
Then we included the file Person.h
in the project Harmash
in the projectTest
.
In the end we used the code in the included files to make sure this worked.
At first close any open file so as not to distract yourself because of the open files as follows.
Right-click on the project name,
Test
then clickActivate Project
as follows.
Click on the add a new file icon, then click on the
Class...
following to add a new class in the project inside a.cpp
special file and with it the general shape of the class inside a file.h
at once
Name the class
Person
and then click on the buttonCreate
as follows until it creates a file for you with its namePerson.cpp
and its general shape inside a file namedPerson.h
.
Click on the button
Yes
to add the new files created in the projectHarmash
as follows.
Click on the button
Select All
and then on the buttonOK
as follows.
Note that the two files have been created
Person.cpp
andPerson.h
inside them an empty class with its name has been preparedPerson
as well.
Delete the default code in the file
Person.h
and write the following code in it.
#pragma once #include <iostream> namespace harmash { class Person { public: std::string name; std::string phone; Person(std::string name, std::string phone); void printInfo(); }; }
Delete the default code in the file
Person.cpp
and write the following code in it.
#include "Person.h" namespace harmash { Person::Person (std::string name, std::string phone) { this->name = name; this->phone = phone; } void Person::printInfo() { std::cout << "name: " << name << "\n"; std::cout << "phone: " << phone << "\n"; } }
Click the Build button(Build)So a library is produced from the project
Harmash
as follows.
Note that he told you that a library named afterlibharmash.a
the path was createdbin\Debug
in the project itself, ie the location of the library we created earlier.
Close all open files so you don't confuse yourself with them.
Right-click on the project name,
Test
then clickBuild Options...
as follows.
Close the button
Search directories
as follows until you specify the location of the library fileslibharmash.a
that you want to include in the projectTest
as well.
Inside the section
Compiler
click on the buttonAdd
as follows because you need to tell the compiler the path of the filePerson.h
that you will later include in the projectTest
.
Click on the folder icon to begin adding the location of the file
Person.h
as follows.
After specifying the location of the file
Person.h
- in our case the folderinclude
inside the projectHarmash
- click on the buttonSelect Folder
as follows.
Click the button
No
to save the full folder path(Full Path)As follows.
Click on the button
Ok
below to add the library.
Go to the section
Linker
and then click on the buttonAdd
and add the same path that you added in the sectionCompiler
.
After adding the path, click on the button
Ok
as follows until you close the window or close it by clicking on the close button.
Open the file
main.cpp
in the projectTest
and delete all the code in it as follows.
Write the following code inside the file
main.cpp
to create an object from the classPerson
and call the functionprintInfo()
from it to print the information we passed to the object when it is executed.
#include <iostream> #include <Person.h> int main() { harmash::Person p("Mahamad", "70654200"); p.printInfo(); return 0; }
Right-click on the project name,
Test
then clickActivate Project
as follows.
Click on the button
Build and run
as follows until the file is savedmain.cpp
and then build and run the projectTest
.
You will see the following result after the project is run
Test
.