Friday, 5 October 2012

Create a Static Library using Visual C++

Procedure to develop, implement Static Library and procedure to use a static library in the Visual C++ MFC application.

Please download source code from below link:


Step 1: Run Visual Studio 2008 Application
Step 2: File->New->Project
Step 3: Now Please add the Static Lib name and click on Ok button you can see below screen

Step 4: Select Static Library radio button click on Finish button. Empty project will be created as below:


Step 5: Now add the staticlib.h and staticlib.cpp files and please write your own functions prototypes in staticlib.h:

int Add(int iFirst,int iSecond);
int Subtract(int iFirst,int iSecond);

Step 6: Please implement the declared functions in staticlib.cpp:

#include "stdafx.h"
#include "staticlib.h"
int Add(int iFirst,int iSecond)
{
return (iFirst+iSecond);
}

int Subtract(int iFirst,int iSecond)
{
return (iFirst-iSecond);
}


Step 7: Now build the application your Static lib will be ready at application path in release or debug folder depending on build settings.


Now static library is ready. now how to test this library..?

Use Static Library in the Application

Step 1: Right click on Solution "StaticLib" and add new MFC Dialog based Application with name MathsApp




Step 2: After Project name is entered click on Ok button and your can see the below dialog from which please select Dialog Based  and click on finish.


Step 3: Now in the dialog please add labels and text boxes as  shown below screen shot


Step 4: Please write below code in the Add button click event handler. Please see the ADD function of static lib is called.


void CMathsAppDlg::OnBnClickedAdd()
{
// TODO: Add your control notification handler code here
CString strInputA = _T("");
CString strInputB = _T("");
CString strResult = _T("");

CEdit* pEdit = NULL;
int iInputA =0x00;
int iInputB =0x00;
int iResult =0x00;

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_A);
pEdit->GetWindowTextW(strInputA);

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_B);
pEdit->GetWindowTextW(strInputB);

iInputA = (int)_tcstol(strInputA,0,10);

iInputB = (int)_tcstol(strInputB,0,10);

iResult = Add(iInputA,iInputB);
strResult.Format(_T("%d"),iResult);
pEdit = (CEdit*)GetDlgItem(IDC_EDT_RESULT);
pEdit->SetWindowText(strResult);

}


Step 5: Please write below code in the Subtract button click event handler.Please see the Subtract function of static lib is called

void CMathsAppDlg::OnBnClickedSubract()
{
// TODO: Add your control notification handler code here
CString strInputA = _T("");
CString strInputB = _T("");
CString strResult = _T("");

CEdit* pEdit = NULL;
int iInputA =0x00;
int iInputB =0x00;
int iResult =0x00;

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_A);
pEdit->GetWindowTextW(strInputA);

pEdit = (CEdit*)GetDlgItem(IDC_EDT_INPUT_B);
pEdit->GetWindowTextW(strInputB);

iInputA = (int)_tcstol(strInputA,0,10);

iInputB = (int)_tcstol(strInputB,0,10);

iResult = Subtract(iInputA,iInputB);
strResult.Format(_T("%d"),iResult);
pEdit = (CEdit*)GetDlgItem(IDC_EDT_RESULT);
pEdit->SetWindowText(strResult);
}

Step 6:
now if you compile you will get below errors 
Error 1 error C3861: 'Add': identifier not found e:\samples\staticlib\mathsapp\mathsappdlg.cpp 176 MathsApp
Error 2 error C3861: 'Subtract': identifier not found e:\samples\staticlib\mathsapp\mathsappdlg.cpp 207 MathsApp

Please do the below settings to compile application successfully:
 a. Please include the header path on top of the cpp file
        #include "staticlib.h"
b. Please set the header path as below screen shot


c. In the linker settings please do the below settings to run and call static lib functions successfully from application.
Set the Library directory path:
In Input settings add staticlib.lib as shown below screen shot

Now run the application and test the static lib functions and the Add function out put screen :


Subtract function output screen :


Now you have learned how implement a static library and how use the static library in the application.

Source code for above sample is available in the below link:

https://sites.google.com/site/staticlibsample/system/app/pages/admin/attachments

1 comment:

  1. Hi Sivaprasad,

    Please Explain what is difference between Static Library and Dynamic Library and advantages and disadvantages of both libraries.

    Regards,
    Sudhakara Maddi

    ReplyDelete