FunctionX Practical Learning Logo

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 a VCL application. The library file has the lib extension.

To create a static library, you can open the New Items dialog box and select Library as the type of application. In the Win32 Application Wizard, select the Static Library radio button in the Application Type section. After laying the foundation of a static library, you can add functions, classes, and/or resources that will be part of the library.

 

Creating the Library Project

  1. Start Borland C++ Builder and, on the main menu, click File -> New -> Other...
  2. In  the New property page of the New Items dialog, click Library
     
  3. Click OK
  4. To save the project, on the Standard toolbar, click the Save All button
  5. In the Save Project As dialog box, click the Create New Folder button to create a new folder. Type BusMath and press Enter twice to display the new folder in the Save combo box
  6. Replace the name of the project with BusinessMath and click Save
  7. To add a new unit, on the main menu, click File -> New -> Unit
  8. To save the new unit, on the Standard toolbar, click the Save All button
  9. Replace the content of the Name edit box with bmcalc and click Save
  10. In the header file, declare the following functions:
     
    //---------------------------------------------------------------------------
    #ifndef bmcalcH
    #define bmcalcH
    //---------------------------------------------------------------------------
    double __fastcall Average(const double *Numbers, const int Count);
    long   __fastcall GreatestCommonDivisor(long Nbr1, long Nbr2);
    //---------------------------------------------------------------------------
    #endif
  11. In the source file, implement the functions as follows:
     
    //---------------------------------------------------------------------------
    
    #pragma hdrstop
    
    #include "bmcalc.h"
    
    //---------------------------------------------------------------------------
    
    #pragma package(smart_init)
    double __fastcall 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 __fastcall GreatestCommonDivisor(long Nbr1, long Nbr2)
    {
    	while( true )
    	{
    		Nbr1 = Nbr1 % Nbr2;
    		if( Nbr1 == 0 )
    			return Nbr2;
    
    		Nbr2 = Nbr2 % Nbr1;
    		if( Nbr2 == 0 )
    			return Nbr1;
    	}
    }
    //---------------------------------------------------------------------------
  12. To create the library, on the main menu, click Project -> Build BusinessMath
     
  13. When the library has been built, click OK on the Compiling dialog box
  14. Save All

Testing the Static Library on Console

A static library created can be used by either a console application, a Win32 application, or a VCL application. That's one of its advantages

  1. To create a console application, on the main menu, click File -> New -> Other...
  2. In the New property page of the New Items dialog box, click Console Wizard and click OK
  3. In the Console Wizard dialog box, make sure the C++ radio button and the Console Application check box are selected
     
  4. Click OK
  5. To save the new project, on the Standard toolbar, click the Save All button
  6. Click the Create New Folder button. Type BusMathTest1 and press Enter twice
  7. Replace the name of Unit with Exercise
  8. Replace the name of the project with BusMathTest and press Enter
  9. Open Windows Explorer or My Computer
  10. Locate the folder that contains the above project: BusMath. Click the BusinessMath.lib file. Press and hold Ctrl, then click the bmcalc.h header file
  11. Press Ctrl + C to copy them
  12. Locate the BusMathTest1 folder. Right-click it and click Paste to paste both the BusinessMath.lib and the bmcalc files in the same folder that contains Exercise.cpp
  13. Back in C++ Builder, to add the library to the current project, on the main menu, click Project -> Add To Project...
  14. In the Files of Type combo box, select Library Files (.lib)
  15. In the list of files, click BusinessMath.lib
     
  16. Click OK
  17. Type the following in the empty Exercise.cpp file
     
    //---------------------------------------------------------------------------
    #include <iostream>
    #include <conio>
    using namespace std;
    #pragma hdrstop
    #include "bmcalc.h"
    //---------------------------------------------------------------------------
    
    #pragma argsused
    int main(int argc, char* argv[])
    {
        double Numbers[] = { 12.55, 94.68, 8.18, 60.37, 104.502, 75.05 };
        int Count = sizeof(Numbers) / sizeof(double);
    
        double Avg = Average(Numbers, Count);
    
        cout << "Average of the list: " << Avg;
    
        cout << "\n\nPress any key to continue...";
        getch();
        return 0;
    }
    //---------------------------------------------------------------------------
  18. Save All
  19. Press F9 to test the application:
     
    Average of the list: 59.222
    
    Press any key to continue...
  20. Close it and return to Borland C++ Builder

Testing the Static Library on a GUI Application

  1. To test the library with a GUI application, on the main menu, click File -> New -> Application
  2. To save the new project, on the Standard toolbar, click the Save All button
  3. Click the Create New Folder button. Type BusMathTest2 and press Enter twice
  4. Replace the name of the unit with Main
  5. In Windows Explorer or My Computer, copy the BusinessMath.lib and the bmcalc.h files from the BusMath folder to the BusMathTest2 folder
  6. Then, on the main menu in C++ Builder, click Project -> Add To Project... Change the Files of Types to Library Files (.lib). Select he BusinessMath.lib file and press Enter
  7. Design the dialog box as follows:
     
    Control  Caption or Text Name Other Properties
    Form Business Mathematics frmMain BorderStyle: bsDialog
    Label Number &1:    
    Edit 1 edtNumber1  
    Label Number &2:    
    Edit 1 edtNumber2  
    BitBtn C&alculate btnCalculate  
    Label GCD:    
    Edit   edtGCD  
    BitBtn     Kind: bkClose
  8. On the form, double-click the Calculate button to access its OnClick event
  9. Implement the event as follows:
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Main.h"
    #include "bmcalc.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
        : TForm(Owner)
    {
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::btnCalculateClick(TObject *Sender)
    {
        int Nbr1, Nbr2, Result;
    
        Nbr1 = atoi(edtNumber1->Text.c_str());
        Nbr2 = atoi(edtNumber2->Text.c_str());
    	
        Result = GreatestCommonDivisor(Nbr1, Nbr2);
    
        edtGCD->Text = IntToStr(Result);
    }
    //---------------------------------------------------------------------------
  10. Test the application. Here is an example:
     
  11. Close it and return to MSVC
 

Previous Copyright 2002-2007 FunctionX, Inc. Next