A static library is a file that contains functions, classes, or resources that an external program can use to complement its functionality. To use a library, the programmer has to create a link to it. The project can be a console application, a Win32 or an MFC application. The library file has the lib extension.
To create a static library, you can open the New Project dialog box and select Win32 Project as the type of application. In the Win32 Application Wizard, select the Static Library radio button in the Application Type section. The following options are also available:
-
If you do not check the Precompiled Header option, an empty project would be created for you
-
If you check the Precompiled Header option, the wizard would generate a StdAfx.h header file and a StdAfx.cpp source file for the project. This allows you to include common header files in StdAfx.h and omit those common header files in every file of the project. When necessary, the compiler would retrieve the needed header file from StdAfx.h
After laying the foundation of a static library, you can add functions, classes, and/or resources that will be part of the library.
Practical Learning: Creating a Project DLL |
|
- Start Microsoft Visual Studio if necessary and display the New Project dialog box
- In the Project Types list, make sure you select the Visual C++ Projects node. In the Templates list, click Win32 Project. In the Name edit box, replace the content with BusMath
- Click OK
- In the Win32 Application Wizard, click Application Settings. In the Application Type section, click Static Library
- Click Finish
- To add a header file, on the main menu, click Project -> Add New Item… In the Add New Item – BusMath dialog box, in the Templates list, click Header File (.h). Replace the content of the Name edit box with bmcalc and click Open
- In the empty file, declare the following functions:
#ifndef _BUSINESSMATH_H_
#define _BUSINESSMATH_H_
double Min(const double *Numbers, const int Count);
double Max(const double *Numbers, const int Count);
double Sum(const double *Numbers, const int Count);
double Average(const double *Numbers, const int Count);
long GreatestCommonDivisor(long Nbr1, long Nbr2);
#endif // _BUSINESSMATH_H_
|
- To add a source file, on the main menu, click Project -> Add New Item… In the Templates list of the Add New Item – BusMath dialog box, click Source File
(.cpp). Replace the content of the Name edit box with bmcalc and click Open
- Implement the functions as follows:
#include "StdAfx.h"
#include "bmcalc.h"
double Min(const double *Nbr, const int Total)
{
double Minimum = Nbr[0];
for(int i = 0; i < Total; i++)
if( Minimum > Nbr[i] )
Minimum = Nbr[i];
return Minimum;
}
double Max(const double *Nbr, const int Total)
{
double Maximum = Nbr[0];
for(int i = 0; i < Total; i++)
if( Maximum < Nbr[i] )
Maximum = Nbr[i];
return Maximum;
}
double Sum(const double *Nbr, const int Total)
{
double S = 0;
for(int i = 0; i < Total; i++)
S += Nbr[i];
return S;
}
double Average(const double *Nbr, const int Total)
{
double avg, S = 0;
for(int i = 0; i < Total; i++)
S += Nbr[i];
avg = S / Total;
return avg;
}
long GreatestCommonDivisor(long Nbr1, long Nbr2)
{
while( true )
{
Nbr1 = Nbr1 % Nbr2;
if( Nbr1 == 0 )
return Nbr2;
Nbr2 = Nbr2 % Nbr1;
if( Nbr2 == 0 )
return Nbr1;
}
}
|
- To create the library, on the main menu, click Build -> Build BusMath
|
|