Min menu

Pages

VB

Dealing with libraries in C Plus Plus | C++ Database

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 .libon Windows and .aon Linux and Mac systems.

  • dynamic libraries(Dynamic Libraries)And its extension is .dllon Windows and .soon 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 runwhen 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 .aaccording 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 .dllmissing 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.dllwhose 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.dlland 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:

  1. Project establishmentC++Normal because we'll use it to try out the library.

  2. Create another project for the library we will include in the first project.

  3. Add the library in the project and call the functions in it.

  4. Re-modifying the library and adding its extension files .cppand .hplacing 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.

  1. tap onFile,thenNew,Then Project...as follows.


  1. Click on Console applicationthe button Goas follows.


  1. Click on ++Cand then click on Nextas follows.


  1. 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 Nextas follows.


  1. Do not modify anything on this page and click on the Finishfollowing.


  1. 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 harmashafter it that produces a stable library named after itlibharmash.

  1. tap onFile,thenNew,Then Project...as follows.


  1. Click on Static librarythe button Goas follows.


  1. Check the option Skip this page next timeand then click on the Nextfollowing so that you do not see this page once every time you want to create a library in the future.


  1. 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 Nextas follows.


  1. Do not modify anything on this page and click on the Finishfollowing.


  1. After the project is successfully created, it will appear in the list of projects as follows.


  1. Since we want to write code in a languageC++And not in a languageCWe will change the file extension mainto .cppas follows.


  1. Name the file main.cppand then click on the button OKas follows.


  1. Open the file main.cppand delete any default code you find in it as follows.


  1. Then write the following code inside it to define a function whose name will printMsg()print a regular statement when called.

main.cpp
	  #include <iostream>
	  
		  void printMsg()
		  {
		  std::cout << "First time using a static library!";
		  }
	

  1. After adding the code in the file main.cpp, click on the Save all changes made button.


  1. 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 after libharmash.athe path was created bin\Debugin the project itself.


If we view project files from outside the programCodeBlocksWe will find the library libharmash.aas follows.


Now go back to the lesson and go to the third step, from which you will learn how to include the library libharmash.ain the projectTest.



Steps to add a library in the project using CodeBlocks

Follow these steps carefully to add the library libharmash.ain the project Testand then call the functions in the library that have been added to make sure that this is done successfully.

  1. At first close any open file so as not to distract yourself because of the open files as follows.


  1. Right-click on the project name, Testthen click Build Options...as follows.


  1. Click on Linker Settingsbecause it is the section where you can add libraries in the project.


  1. Click on the button Addand then on the folder icon to start adding the library in the project as follows.


  1. After selecting the library you want to add - in our case the library libharmash.a- click on the button Openas follows.


  1. Click the button Noto save the full library path(Full Path)As follows.


  1. Click on the button Okbelow to add the library.


  1. Click on the button Okas follows until the window closes or closes by clicking on the close button.


  1. Right-click on the project name, Testthen click Activate Projectas follows.


  1. Open the file main.cppin the project Testas follows.


  1. Write the following code inside the file main.cppto call the function printMsg()in the library libharmash.athat will print a regular statement for you when it is executed.

main.cpp
	  #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;
		  }
	

  1. Click on the button Build and runas follows until the file is saved main.cppand then build and run the projectTest.


  1. You will see the following result after the project is runTest.


Now go back to the lesson and go to the fourth and final step where we will add files .cppand .hthen include them in the project as Testwell.



Steps to include library files using CodeBlocks

Follow the steps below carefully to add a file named Person.hand file name Person.cppin the projectHarmash.
Then we included the file Person.hin the project Harmashin the projectTest.
In the end we used the code in the included files to make sure this worked.

  1. At first close any open file so as not to distract yourself because of the open files as follows.


  1. Right-click on the project name, Testthen click Activate Projectas follows.


  1. Click on the add a new file icon, then click on the Class...following to add a new class in the project inside a .cppspecial file and with it the general shape of the class inside a file .hat once


  1. Name the class Personand then click on the button Createas follows until it creates a file for you with its name Person.cppand its general shape inside a file namedPerson.h.


  1. Click on the button Yesto add the new files created in the project Harmashas follows.


  1. Click on the button Select Alland then on the button OKas follows.


  1. Note that the two files have been created Person.cppand Person.hinside them an empty class with its name has been prepared Personas well.


  1. Delete the default code in the file Person.hand write the following code in it.

Person.h
	  #pragma once
	  
		  #include <iostream>
	  
		  namespace harmash
		  {
		  class Person
		  {
		  public:
		  std::string name;
		  std::string phone;
	  
		  Person(std::string name, std::string phone);
	  
		  void printInfo();
		  };
		  }
	

  1. Delete the default code in the file Person.cppand write the following code in it.

Person.cpp
	  #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";
		  }
		  }
	

  1. Click the Build button(Build)So a library is produced from the project Harmashas follows.
    Note that he told you that a library named after libharmash.athe path was created bin\Debugin the project itself, ie the location of the library we created earlier.


  1. Close all open files so you don't confuse yourself with them.

  2. Right-click on the project name, Testthen click Build Options...as follows.


  1. Close the button Search directoriesas follows until you specify the location of the library files libharmash.athat you want to include in the project Testas well.


  1. Inside the section Compilerclick on the button Addas follows because you need to tell the compiler the path of the file Person.hthat you will later include in the projectTest.


  1. Click on the folder icon to begin adding the location of the file Person.has follows.


  1. After specifying the location of the file Person.h- in our case the folder includeinside the project Harmash- click on the button Select Folderas follows.


  1. Click the button Noto save the full folder path(Full Path)As follows.


  1. Click on the button Okbelow to add the library.


  1. Go to the section Linkerand then click on the button Addand add the same path that you added in the sectionCompiler.


  1. After adding the path, click on the button Okas follows until you close the window or close it by clicking on the close button.


  1. Open the file main.cppin the project Testand delete all the code in it as follows.


  1. Write the following code inside the file main.cppto create an object from the class Personand call the function printInfo()from it to print the information we passed to the object when it is executed.

main.cpp
	  #include <iostream>
		  #include <Person.h>
	  
		  int main()
		  {
		  harmash::Person p("Mahamad", "70654200");
	  
		  p.printInfo();
	  
		  return 0;
		  }
	  
	

  1. Right-click on the project name, Testthen click Activate Projectas follows.


  1. Click on the button Build and runas follows until the file is saved main.cppand then build and run the projectTest.


  1. You will see the following result after the project is runTest.