Create C Program with Static Library using Command Line in Windows

The following procedure is to create C program with static library using Microsoft Visual Studio Express 2012 command prompt. For brief introduction to software library, please refer to the previous post Create C Program with Static Library using Visual Studio. Click here for procedure of creating a C program using Visual Studio.

Example:
The following example is a normal C program with functions without using any custom static library. 

#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;
}

We will use the above example to create a static library and an application program that use this library. We will place the static library in a shared public folder and place the application in the personal document folder.

Create Static Library

Step 1: Create headers file for static library
Open a notepad  and create the headers of the static library as follows. Named the file as MyCMathLib.h

#define PI 3.1415;

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


Important: Please make sure that you save the file in program.c instead of program.c.txt. Notepad save everything in .txt extension by default. To avoid this problem, under the file explorer select Organize >> Folder and search option, select the view tab and clear the check on "Hide  extensions for known file types".

Step 2: Create the source code for the functions
Open a notepad  and create the source of the static library as follows. Named the file as MyCMathLib.c.

#include "MyCMathLib.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 3: Compile and create the static library
Open the "Developer Command Prompt for VS2012". Use the following command to compile without linking the library.

cl /c MyCMathLib.c 


Debug any error for compilation error. The compiler should generate an object file named MyCMathLib.obj

Next, use the following command to create the static library.

lib MyCMathLib.obj 


The compiler will generate the static library named as MyCMathLib.lib


Create Application Program using Static Library

Step 1: Create an application program
Open a notepad and create the application program making use of the functions in the static library as follows: Named the file as MyApp2.c. Save the file in your personal folder.

Please note that you must know the location of the header file. You need to indicate the header file name and extension if the header file is in the same folder as this program. Otherwise you need to specify the full path in the program or alternatively you could specify the include path during compilation time. The latter method is much preferred becasue you don't need to change the program when the header file relocates.

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

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;

}

Step 2: Compile the program
Before compilation, you must know where the location of the static library (lib file). You need the full path for compilation.

cl MyApp2.c /I C:\Users\Public\Documents\MyCMathLib\ C:\Users\Public\Documents\MyCMathLib\MyCMathLib.lib 


If your program do not specify the full path for the header file, then you need to use the following command with /I option to include the directory for the search path.

If your program include the full path of the header file as shown below:

#include <stdio.h>
#include "C:\Users\Public\Documents\MyCMathLib\MyCMathLib.h"

// The rest of the program....

}
Then, the command for compilation is:

cl MyApp2.c /I C:\Users\Public\Documents\MyCMathLib\MyCMathLib.lib 


Step 3: Run the program
Test the program using normal command prompt as shown



Please note that you can place the execution file anywhere and run them because all the code in the static library is copied to the exe file while linking.

Please refer to the following post for:

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


*****

Comments

  1. Very helpful article. I was banging my head against the wall trying to make a static library because I was accidentally using "link" instead of "lib". Seems this is one of the few pages that show how to use VS to build a static library in strictly command line. Thanks.

    ReplyDelete

Post a Comment

Popular posts from this blog

Revive Old Mac Mini (2009) with Linux

Configure Unattended Upgrades on Raspberry Pi

Install and Configure RealVNC in Linux Ubuntu 18.04 LTS