A static library (with a. A or. lib extension) is a file that contains functions that are used to consolidate the execution of the program during the link phase, and that the dynamic link library (extension. dll) is not integrated into the execution program in the link phase.
DLL files are dynamically invoked during the execution phase
Below we will develop the static library with the free development tool Codebocks
Create a static library
Start Codeblocks and create a project of type "Static Library" (file/new/project/static library/go).
Give the project a name ("Malibrairie" for instance) and assign a target directory ("C:/essai" for instance)
Delete the default content in the Main.cpp file. Enter into the program by following the contents below.
void cinq (int *i)
{
int n;
N=*i;n=5*n;
*i=n;
}
Select the "Build/run" menu.
Codeblocks immediately generates the library file to the "C:/essai/malibrairie" directory and Names "Libmalibrairie a".
Codeblocks and Devcpp build the library file with the extension ". A", Visual Studio will build the library file with the extension ". Lib".
We use this library file below.
The library file "Libmalibrairie.a" has been created.
Use the Static library
Create a new project with the type "Win32 GUI".
Select the application type "Frame based".
Name ("Monprogramme") and select the project directory ("C:/essai").
Remove the main.cpp default code and replace the code with the following code to test our static library.
#include "Windows.h"
extern "C" void cinq (int *);
int Apientry WinMain (hinstance h1,hinstance h2,lpstr l,int N)
{
int x;
Char texte[80];
x=2;
Cinq (&x);
wsprintf (Texte, "%d", x);
MessageBox (Null,texte, "", MB_OK);
return 0;
}
Usually, Codeblocks does not know that the library file is being used and its location, we must declare the library file so that it does not go wrong when linking. Select the menu "Project/build options/linker Settings" To add the "Libmalibrairie.a" library file. (For personal testing, you need to add a directory of library files to the linker search directory).
Click "Build/build and Run". The program runs correctly.
When using the-lapue library in Apue, write the apue directly. Because the static library file has been added to the/usr/lib/libapue.a.
Codeblocks creating and using a static library (C language)