How to Create and Use a Static Library in Visual Studio
A static library is a file that contains compiled code that can be reused by multiple applications. Static libraries have the extension .lib and are linked at compile time with the executable or dynamic library that uses them. Static libraries are useful for sharing common functionality without having to distribute additional files or install dependencies.
In this article, we will show you how to create and use a static library in Visual Studio, a popular integrated development environment (IDE) for C++ development. We will cover the following steps:
- Create a static library project
- Add a class to the static library
- Create a console application that references the static library
- Use the functionality from the static library in the application
Create a static library project
To create a static library project in Visual Studio, follow these steps:
- On the menu bar, choose File > New > Project to open the Create a New Project dialog.
- At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library.
- From the filtered list of project types, select Windows Desktop Wizard, then choose Next.
- In the Configure your new project page, enter MathLibrary in the Project name box to specify a name for the project. Enter StaticMath in the Solution name box.
- Choose the Create button to open the Windows Desktop Project dialog.
- In the Windows Desktop Project dialog, under Application type, select Static Library (.lib).
- Under Additional options, uncheck the Precompiled header check box if it's checked. Check the Empty project box.
- Choose OK to create the project.
Add a class to the static library
To add a class to the static library, follow these steps:
- To create a header file for a new class, right-click to open the shortcut menu for the MathLibrary project in Solution Explorer, and then choose Add > New Item.
- In the Add New Item dialog box, select Visual C++ > Code. In the center pane, select Header File (.h).
- Specify a name for the header fileâfor example, MathLibrary.h âand then choose the Add button. A nearly blank header file is displayed.
- Add a declaration for a class named Arithmetic to do common mathematical operations such as addition, subtraction, multiplication, and division. The code should resemble:
```cpp
// MathLibrary.h
#pragma once
namespace MathLibrary
class Arithmetic
public:
// Returns a + b
static double Add(double a, double b);
// Returns a - b
static double Subtract(double a, double b);
// Returns a * b
static double Multiply(double a, double b);
// Returns a / b
static double Divide(double a, double b);
;
```
Create a console application that references the static library
To create a console application that references the static library, follow these steps:
- On the menu bar, choose File > New > Project to open the Create a New Project dialog.
- At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Console.
- From the filtered list of project types, select Console App (C++), then choose Next.
- In the Configure your new project page, enter MathClient in the Project name box to specify a name for the project. Make sure that StaticMath is selected in Solution name box.
- Choose Create button to create an empty console app project.
- To reference MathLibrary from MathClient , right-click on MathClient in Solution Explorer and choose References from shortcut menu. In References dialog box under Projects , check MathLibrary . Choose OK . This step adds MathLibrary.lib as an additional dependency for linker settings of MathClient .
- To include MathLibrary.h header file in MathClient , right-click on Source Files folder under MathClient in Solution Explorer and choose Add > Existing Item from shortcut menu. In 51082c0ec5