Monday, August 12, 2013

Create C Program with Static Library using Visual Studio 2012

The following procedure is to write and compile C program with a static library using Microsoft Visual Studio 2012 Express edition. For instruction on creating a basic C program using Visual Studio, please refer to the document here.

Brief Introduction to Software Library
A software library is a collections of previously written functions or program which can be reused. A C program always includes a  standard file called stdio.h. This file is the header file that describe all the pre-written function such as printf that comes with the compiler. This library file is also known as the system library. Library file that are created by users are called custom library.

There are two types of library, static library and dynamic link library. Both library files can share their codes and function with other program. A program that link with a static library will include all the program required during compilation time. After which you can ship the execution (exe) program to the user. Any changes in the static library means that you need to re-compile the program and ship to the user again.

For dynamic link library (DLL) the program of external functions are not included with the applications. Both dynamic link library (DLL) file and execution file (exe) must be shipped together. If there is any changes to the library, you just need to recreate the DLL file and ship the new library file to the user without any changes to the main execution program.


Examples:
In this exercise, we are going to create a program that includes some math function.
A normal program is as follows:

#include <stdio.h>
#define PI 3.1415;

double PowerOf2 (double UserNumber);
double PowerOf3 (double UserNumber);
double CircleArea (double UserRadius);
double CircleCircum (double UserRadius);

int main ( )
{

 double p2 = 10.0;
 double p3 = 5.0;
 double radius = 4.0;

 printf ("The number %.2f to the power of 2 is %.2f. \n", p2, PowerOf2(p2));

 printf ("The number %.2f to the power of 3 is %.2f. \n", p3, PowerOf3(p3));

 printf ("A circle with a radius of %.2f, the area is %.2f. \n", radius, CircleArea(radius));

 printf ("A circle with a radius of %.2f, the circumference is %.2f. \n", radius, CircleCircum(radius));

 return 0;

}

double PowerOf2 (double UserNumber)
{
 return UserNumber * UserNumber;
}


double PowerOf3 (double UserNumber)
{
 return UserNumber * UserNumber * UserNumber;
}

double CircleArea (double UserRadius)
{
 return UserRadius * UserRadius * PI;
}
 
double CircleCircum (double UserRadius)
{
 return 2 * UserRadius * PI;
}

Compile the above program to see if it works. For instruction on how to create a C program using MS Visual Studio 2012 Express, please refer to the article here.

To create a static library, we need to split the program into 2 parts. The main program and the static library which contain the functions. The static library will consist of 2 files MyMathLib.h and MyMathLib.c.

To create Static Library
Step 1: Create a new project as static library
Create a new project using MyMathLib as project name


Click "OK". Then click "Next". On following screen you need to specify that you are creating a static library.


Step 2: Create the headers file
Under Solution Explorer, right click Headers Files >> Add >> New Item as shown below.


Select header file as file type and rename the header file to MyMathLib.h


Click "Add".

Add the following to the headers:

#define PI 3.1415;

double PowerOf2 (double UserNumber);
double PowerOf3 (double UserNumber);
double CircleArea (double UserRadius);
double CircleCircum (double UserRadius);


Step 3: Create the Implementation Program
Under Solution Explorer, right click Source Files >> Add >> New Item. Rename the program to MyMathLib.c.


Enter the source code as follows

#include "MyMathLib.h"

double PowerOf2 (double UserNumber)
{
 return UserNumber * UserNumber;
}

double PowerOf3 (double UserNumber)
{
 return UserNumber * UserNumber * UserNumber;
}

double CircleArea (double UserRadius)
{
 return UserRadius * UserRadius * PI;
}
 
double CircleCircum (double UserRadius)
{
 return 2 * UserRadius * PI;
}

Step 4: Build solutions
Build solutions. Once the compilation is complete, close the project.

Create a C Application Program Using the Static Library
Step 1: Create a new project
Create a new project using the name MyApps1 with Console Application as options. Remember to clear all headers and checked Empty Project.

Step 2: Establish Link with the Static Library
Under Solution Explorer, right click and select Add >> Existing Projects


Navigate to the project folders of MyMathLib and select the project file MyMathLib.vcxproj

Your solution explorer should show two projects like below:


Select MyApps1, right click and select properties. Under Common Properties, select Framework and References. Click "Add New Reference"


You should see the following screen, there should be a project named "MyMathLib". Check the project as shown below and click "OK".


On the properties page, under Configuration Properties >> C/C++ expand the list in C/C++ and select "General" as shown below.


For first line "Additional Include Directories", click the arrow and select edit. The following dialog box shows:


Click the "New Line" icon and click on the buttion "...". Now you need to navigate to the static library folder. Do not select the folder "MyMathLib" under Projects, drill one level down and select the folder "MyMathLib". Highlight the folder and click select folder.


Click "OK". Then click "Apply". You can close the property dialog box.

Step 3: Create the application program 
Under Solution Explorer, right click Source Files >> Add >> New Item. Rename the program to MyApps1.c.

Enter the following program:

#include <stdio.h>
#include "MyMathLib.h"

int main ( )
{

 double p2 = 10.0;
 double radius = 4.0;

 printf ("The number %.2f to the power of 2 is %.2f. \n", p2, PowerOf2(p2));

 printf ("A circle with a radius of %.2f, the area is %.2f. \n", radius, CircleArea(radius));

 return 0;

}

Step 4: Build the solution.
After you have built the solutions, you should received a message similar to the one below. Please note that two projects are being built.


Step 5: Run the program
You can test the project from the command prompt as shown below:



Note:
This completes the procedure for creating a static library in C using Visual Studio Express 2012.

Please note that the procedure for creating similar static library using command line is different from using the IDE. 


Please refer to the following post for:

For C programming in other platform please refer to the C Programming page.

*****

1 comment:

  1. This is the best tutorial for creating a static library in Visual Studio, and how to link to your project. Thanks dude.

    ReplyDelete

Note: Only a member of this blog may post a comment.