Tuesday, September 3, 2013

Create C Program with Static Library using Xcode in Mac OS X

The following program is a complete program that uses some math functions:

#include <stdio.h>
#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);
 
int main ( )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;

 
}
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}
 

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

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


We create a static library so that we could reuse all the custom math functions for other programs. In this example, we will place the function declaration into libmymath.h. Then, we place the functions for circle in mymath1.c and the power functions in mymath2.c.

Note: You can have many C program file with just one header file such as lib.h, lib1.c, lib2.c ... and so on.


Create C Static Library in Xcode

To create static library in Xcode, please fellow the procedure:

On the "Welcome to Xcode" page, select "Create a new Xcode project".


Alternatively, you can use File >> New >> Project....



On the template page, select Framework & Libraries >> C/C++ Library. Click Next.


On the options page, enter the name of static library. In my case, it is mymath. Make sure that the type is set to Static. You may want to check "Use Automatic Reference Counting" if your library implementation contains pointers.


Library Naming Convention
The naming convention of a library package must be prefix with lib follow by the name of the library and the file must end with .a. Therefore a typical library should be named as libmymath.a. However, you can ignore the prefix and the extension when using Xcode. In this case, the library name mymath is alright.

Click Next. Then select the file location of your project. Click Create.

Once the project is created you will notice that the target is automatically name libmymath.a.



To create the header file, select File >> New >> File...


On the template page, select "Header File" and click Next.


On the Save As dialog page, enter the name of header file libmymath.h. Make sure the Targets mymath is checked as shown below. Click Create.



The header file for libmymath.h is as follows:

#define PI 3.1415;
 
double CircleArea (double radius);
double CircleCircum (double radius);

double PowerOf2 (double number);
double PowerOf3 (double number);



To create the library implementation file, select File >> New >> File...

On the template page, select C File. Click Next.


On the dialog box, enter the first program as mymath1.c as shown below. Click Create.


The program file for libmymath1.c is as follows:

#include "libmymath.h"
 
double CircleArea (double radius)
{
 return radius * radius * PI;
}

double CircleCircum (double radius)
{
 return 2 * radius * PI;
}


To create the next implementation use the same procedure as above.

The program file for libmymath2.c is as follows:

#include "libmymath.h"


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

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



To compile the library program, select Product >> Build:


Create the Application Program

Select File >> New >> Project.... On the template page select Applications and Command Line Tool. Click Next.


On the options page enter the program name app1. Make sure that the type is set to CYou may want to check "Use Automatic Reference Counting" if your library implementation contains pointers. Click Next.



Enter the location of your file and click Create.

Include Library File
This is the most important section in this tutorial. To use the static library you've just created, you need to add the static library to your current project.

Method 1
There are many ways to include static library. The fist method is to add all the files from the library project file. Select File >> Add Files to "app1"....


Navigate to your static library project folder and select the entire folder of the static library. Your project panel should be similar to the one shown below:


Method 2
The second method is to just copy the product (libmymath.a) and header file (libmymath.h). Open your static library project and your application program. Stack them as shown below.


Then drag the file libmymath.h and libmymath.a to your current application project. Check "Copy items..." and "Add to targets". Click Finish.


Your application project should be similar to the screen below:


Note:
  • Method 2 is much cleaner if your static library is very large. If your library implementation is not big, first method is good enough.
  • One way to distribute the static library for different user is to copy the header file and the static library to a share folder. Other users will will use Method 2 to copy the library and header files into their project. 
  • For more complex multiple user scenario, please consult the Xcode user guide.

Create the following application program in main.c:
#include <stdio.h>
#include "libmymath.h"
 
int main(int argc, const char * argv[] )
{
 
 double r = 4.0;
 double n = 5.0;

 printf ("Radius %.2f, area is %.2f \n", r, CircleArea(r));
 printf ("Radius %.2f, circumference is %.2f \n", r, CircleCircum(r));
 printf ("%.2f to the power of 2 is %.2f \n", n, PowerOf2(n));
 printf ("%.2f to the power of 3 is %.2f \n", n, PowerOf3(n));

 return 0;
 
}




Running The Application

To run the project, you can click the run button or select Product >> Run. The results should be shown as follow: 



Special Note
Please note that the methods are the simplified methods of using static library. Instead of copying files to your projects, you can also change the build settings to reference headers file and library file elsewhere. Please consult the Xcode user guide to explore further options.  

*****

No comments:

Post a Comment

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