Click here to Skip to main content
15,884,739 members
Articles / Programming Languages / C++

Building and Implementing Static Library in VC++

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
5 Aug 2012CPOL2 min read 28.2K   6   4
Learn how to build a Static Library Project from scratch, and implement it in Visual C++ solution

Objective

To create a simple static library and implement it on a Console application.

Requirements

Have at least Visual Studio C++ 2008 Express Edition installed in your PC.

Build Static Library Project

  1. Create a new project.

    Image 1

  2. Select Project type as General. Template as Empty Project. I set the name of the project as MyMathLib. After you set the name, press OK.

    Image 2

  3. The Win32 Application Wizard dialog will pop-up. Press Next, and check on the Application type option Static Library, and in additional options you can uncheck precompiled options.

    Image 3

  4. You will have to write the code for your classes, properties and functions that you need to implement. Let’s write some simple functions for our libraries. First, we need to create one header file to declare the two functions. These functions will be named as add and multiply.

    Each function will receive two integer arguments, and return an integer value.

    C++
    #ifndef __MY_MATH_LIB__
           #define __MY_MATH_LIB__
    
           int add(int number_a, int number_b);
    
           int multiply(int number_a, int number_b);
    
           #endif

    Once we declare, let’s write the definition of our functions in a .cpp file.

    C++
    #include "MyMathLib.h"
    
           int add(int number_a, int number_b)
           {
    	return (number_a) + (number_b);
           }
    
           int multiply(int number_a, int number_b)
           {
    	return (number_a) * (number_b);
           }
  5. Press F7 to build the library solution. If we go to our Debug or Release folder (depending on our compiling configuration), our output file will be the static library. That means our that our library can be implemented in any Visual C++ project. Let’s use these functions in a Console Application.

Implementing the Static Library

  1. Go to solution explorer, and do right click on the Solution Item, then go Add -> New Project.

    Image 4

  2. Set project type as Win32 and the template as Win32 Console Application. Then click OK. I named my project TestMyMathLib.

    Image 5

  3. In the Win32 Application Wizard window, press Next. Make sure the Application type is Console Application and in Additional Options have selected empty.

    Image 6

  4. In the solution explorer, go to your Win32 Application project root item, press right click, and in submenu, click on the option Set as StartUp Project.

    Image 7

  5. Before you start writing any code in the Win32 App. Project, we need to set a registry our Static Library Project as a dependency. So again, in the solution explorer, go to your Win32 Application project root item, press right click, and in submenu, click on the option Project Dependencies...

    Image 8

  6. The Project Dependencies windows will pop-up. On the Dependencies tab, make sure that your Win32 App Project is selected. Then on the list of dependencies, select your Static Library Project.

    Image 9

    On the Build Order tab, make sure your Static Library Project is built before your Win32 App Project.

    Image 10
  7. Create a CPP file for Win32 Application project and write the following code:
    C++
    #include <iostream>
    #include "../MyMathLib/MyMathLib.h"
    using namespace std;
    
    int main ()
    {
    	cout << "Testing my math lib" << endl;
    	cout << "Please enter a number ";
    	int number_a, number_b;
    	cin >> number_a;
    	cout << "Please enter another number ";
    	cin >> number_b;
    	cout << "The addition result for these numbers is ";
    	cout << add(number_a, number_b);
    	cout << endl;
    	cout << "The multiply result for these numbers is ";
    	cout << multiply(number_a, number_b);
    	cout << endl;
    	system("pause");
    	return 0;
    }
  8. Press F7 to build the solution. And press F5 to run and debug your application. This is how the application should look:

    Image 11

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Mexico Mexico
I've working since 2009. I started as a intern programming in SQL Server and C# language for a metallurgical factory of my hometown as Software support, debugging and patching the company system.

Now I'm working as a developer programming in Java and C++ in a company dedicated to mobile software.

I'm using third party home-brew tools, python and batch script to build releases.

Comments and Discussions

 
General[My vote of 2] Superficial Pin
.:floyd:.28-Mar-14 6:28
.:floyd:.28-Mar-14 6:28 
GeneralRe: [My vote of 2] Superficial Pin
Southmountain13-Jun-16 18:41
Southmountain13-Jun-16 18:41 
QuestionVS 2010 Pin
waleri5-Aug-12 22:28
waleri5-Aug-12 22:28 
AnswerRe: VS 2010 Pin
chaq6866-Aug-12 6:49
professionalchaq6866-Aug-12 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.