Thursday, September 5, 2013

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

The following instructions is a simplified procedure to create a dynamic library. Please note that the actual implementation of dynamic library is very complex if the library is very large and multiple users or versions are involved. Please consult the Xcode user guide for more information.

Example

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


To create a dynamic library so that we could reuse all the custom math functions for other programs, we will split the function declaration into libmydymath.h. Then, we place the functions for circle in mydymath1.c and the power functions in mydymath2.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 Dynamic Library in Xcode

To create dynamic 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 mydymath. Make sure that the type is set to Dynamic. 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 .dylib. Therefore a typical library should be named as libmydymath.dylib. However, you can ignore the prefix and the extension when using Xcode. In this case, the library name mydymath 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 libmydymath.dylib.



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 libmydymath.h. Make sure the targets mydymath is checked as shown below. Click Create.



The header file for libmydymath.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 mydymath1.c as shown below. Click Create.


The program file for libmydymath1.c is as follows:

#include "libmydymath.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 libmydymath2.c is as follows:

#include "libmydymath.h"


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

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



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

You should have the following files in your project.



Create the Application Program

There are many ways to include a dynamic library into a projects.
Listed below are two simpler methods.

Method 1

Select File >> New >> Project.... 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 C. You 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
The fist method is to add all the files from the library project file. Select File >> Add Files to "app1"....


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



Method 2

Select File >> New >> Project.... Then select Applications and Command Line Tool. Click Next.


On the options page enter the program name app2. Make sure that the type is set to C. You 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
The second method is to just copy the product (libmydymath.dylib) and header file (libmydymath.h). Open your dynamic library project and your application program. Stack them as shown below.


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


Your application project should be similar to the screen below:



Select app2 project file (blue icon) and select Build Phases in the middle. See below.



On the lower right corner, click Add Build Phase and select Add Copy Files.


On the new Copy Files box, set Destination to Product Directory. Click the + sign and add the dynamic library libmydymath.dylib as shown below.



Create the Main Program

Create the following application program in main.c:

#include <stdio.h>
#include "libmydymath.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: 



Note:
  • Method 2 is much cleaner if your dynamic library is very large. If your library implementation is not big, first method is good enough.
  • The additional build phase step in method 2 is to copy the dynamic library and place the file together with the application binary file so that when the application runs, it could find the dynamic library.
  • If you intend to place the dynamic library at /usr/lib, please refer to the Xcode user guide.
  • Please note that this example does not involved in multiple versions of the same library. 
  • I've also not cover the actual packaging and distribution of the dynamic library itself. 
  • My final word is dynamic library implementation is a very complex subject, please conduct more research. 
*****

No comments:

Post a Comment

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