Win32 Static Libraries
 

Introduction

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

  1. Start Microsoft Visual Studio if necessary and display the New Project dialog box
  2. 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
     
  3. Click OK
  4. In the Win32 Application Wizard, click Application Settings. In the Application Type section, click Static Library
     
  5. Click Finish
  6. 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
  7. 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_
  8. 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
  9. 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;
    	}
    }
  10. To create the library, on the main menu, click Build -> Build BusMath
 

Testing the Static Library on Console

  1. To prepare a test for a console application, display the New Project dialog box. In the Project Type, click Visual C++ Projects and, in the Templates list, click Win32 Project. In the Name edit box, type BusMathTest1 and press Enter
  2. In the Win32 Application Wizard, click Application Settings. In the Application Type section, click the Windows Application radio button. In the Additional Options section, click the Empty Project check box
     
  3. Click Finish
  4. Create a new C++ source file (Project -> Add New Item… -> C++ File (.cpp)) and Name it Exercise
  5. Open Windows Explorer or My Computer
  6. Locate the folder that contains the above project: BusMath. Open its Debug folder and copy the BusMath.lib file. Display the contents of the BusMathTest1 folder and open the BusMathTest1 folder inside of it. Notice that it contains Exercise.cpp. Paste the BusMath.lib file in the same folder that contains Exercise.cpp
  7. From the BusMath project, copy the bmcalc.h header file. Still in Windows Explorer or My Computer, paste bmcalc.h it in the same folder that contains BusMath.lib and Exercise.cpp
  8. Back in Visual Studio, to add the library to the current project, on the main menu, click Project -> Add Existing Item. In the Files of Type combo box, select All Files
  9. In the list of files, click BusMath.lib
  10. Click Open
  11. Type the following in the empty Exercise.cpp file
     
    #include <iostream>
    #include "bmcalc.h"
    using namespace std;
    
    int main()
    {
    double Numbers[] = { 12.55, 94.68, 8.18, 60.37, 104.502, 75.05 };
    int Count = sizeof(Numbers) / sizeof(double);
    
    double Total = Sum(Numbers, Count);
    double Avg = Average(Numbers, Count);
    double Low = Min(Numbers, Count);
    double High = Max(Numbers, Count);
    
    cout << "Characteristics of the list";
    cout << "\nMinimum: " << Low;
    cout << "\nMaximum: " << High;
    cout << "\nTotal: " << Total;
    cout << "\nAverage: " << Avg << endl;
    
    return 0;
    }
  12. Test the application:
     
    Characteristics of the list
    Minimum: 8.18
    Maximum: 104.502
    Total: 355.332
    Average: 59.222
    Press any key to continue
  13. Close it and return to MSVC

Testing the Static Library on a GUI Application

  1. To test the library with a GUI application, display the New Project dialog box. In the Project Types list, make sure Visual C++ Projects is selected. In the Templates list, click MFC Application
  2. In the Name edit box, type BusMathTest2 and press Enter
  3. Set the Application Type as Dialog Based. Set the Dialog Title to Business Mathematics and remove the About Box check box. Click Finish
  4. In Windows Explorer or My Computer, one after the other, copy th BusMath.lib and bmcalc.h files from the BusMath project to the BusMathTest2 folder inside the main BusMathTest2 folder
  5. Delete the OK button and design the dialog box as follows:
     
    Control  ID  Caption
    Static Text   Number &1:
    Text Box IDC_NUMBER1   
    Static Text   Number &2:
    Edit Control IDC_NUMBER2   
    Button IDC_CALCULATE  C&alculate
    Static Text    GCD:
    Edit Control IDC_GCD  
    Button IDCANCEL &Close
  6. Add a Value variable for the IDC_NUMBER1 control and name it m_Number1
  7. Add a Value variable for the IDC_NUMBER2 control and name it m_Number2
  8. Add a Value variable for the IDC_GCD control and name it m_GCD
  9. To add the library to the current project, on the main menu, click Project -> Add Existing Item… and select BusMath.lib
  10. On the dialog box, double-click the Calculate button and implement it OnClick event as follows:
     
    // BusMathTest2Dlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "BusMathTest2.h"
    #include "BusMathTest2Dlg.h"
    #include ".\busmathtest2dlg.h"
    #include "bmcalc.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    // CBusMathTest2Dlg dialog
    
    CBusMathTest2Dlg::CBusMathTest2Dlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CBusMathTest2Dlg::IDD, pParent),
    	  m_Number1(_T("0")),
    	  m_Number2(_T("0")),
    	  m_GCD(_T("0"))
    {
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    . . .
    
    void CBusMathTest2Dlg::OnBnClickedCalculate()
    {
    	// TODO: Add your control notification handler code here
    	int Nbr1, Nbr2, Result;
    
    	UpdateData(TRUE);
    
    	Nbr1 = atoi(m_Number1);
    	Nbr2 = atoi(m_Number2);
    	
    	Result = GreatestCommonDivisor(Nbr1, Nbr2);
    
    	m_GCD.Format("%d", Result);
    
    	UpdateData(FALSE);
    }
  11. Test the application. Here is an example:

    35. Close it and return to MSVC
 

Previous Copyright © 2002-2006 FunctionX, Inc. Next