Posts

Create C Program with Dynamic Library using Command Line in Mac OS X

For those who are not familiar with static and dynamic library, the differences between static and dynamic library is the way it was compile and run. To use a static library, the application and the static library must be present during compilation. The compiler will extract the code from the static library and incorporated into the application program. You'll have only one application binary file. When using dynamic library, the compiler do not incorporate the code from the dynamic library into the application program. The application program will load the library during runtime. In this case, you'll need to deliver two binary files to the user; the main application binary and the dynamic library file. The advantages of using dynamic library are smaller application size since the library code is not incorporated in the application program and it may reduce memory usage when since the library is loaded only when the function is needed. There are two usage of dynamic ...

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

Image
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 pro...

Create C Program with Static Library using Command Line 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 pro...

Compiling C Program in Linux Part 2

In the second part, we will write and compile the same program using separate headers file and program. This exercise is useful when you are teaming with your colleague or classmate working on a single project. We will also attempt to use makefile for our compilation. Writing and Compiling C Program using Multiple Programs We can convert the following program into multiple programs: #include <stdio.h> #include <math.h> double tvm (double rate, int terms, double principal); int main ( ) { double myrate; int myterm; double myprincipal; double myreturn; printf ("Please enter the following:\n"); printf ("Rate of return: "); scanf ("%lf", &myrate); printf ("\n"); printf ("Number of terms:(No decimals): "); scanf ("%d", &myterm); printf ("\n"); printf ("The principal amount: "); scanf ("%lf", &myprincipal); printf ("\n"); myreturn = tvm ...

Create C Program with Shared Library (Dynamic Link Library) in Linux

Shared Library (Dynamic Link Library) Shared library (a.k.a dynamic link library) is a library file that is called and linked by a dynamic linker during program execution time. Naming Convention In Linux, all shared library are name with a prefix lib and ends with .so. The actual file name of a shared library should follow the convention follow by a period and a version number, follow by a period and a minor version number follow by a period and a release number. Therefore a shared library file should be named as: libmymath.so.1.0.1 . Please note that you also need to create a so name file using the same file name but without the minor version and release number. Therefore a shared library file should be named as: libmymath.so.1 . You will also need a linker name using the file name as so name but without the version number. Therefore a shared library file should be named as: libmymath.so A typical shared library should list as actual file, so name file and linker n...

Create C Program with Static Library in Linux

Consider the following C program: #include <stdio.h> #include <math.h> double tvm (double rate, int terms, double principal); int main ( ) { double myrate; int myterm; double myprincipal; double myreturn; printf ("Please enter the following:\n"); printf ("Rate of return:(divide by 100, 4% is 0.04) "); scanf ("%lf", &myrate); printf ("\n"); printf ("Number of terms:(No decimals): "); scanf ("%d", &myterm); printf ("\n"); printf ("The principal amount: "); scanf ("%lf", &myprincipal); printf ("\n"); myreturn = tvm (myrate, myterm, myprincipal); printf ("Your return after %d term is $%.2lf.\n", myterm, myreturn); return 0; } double tvm (double rate, int terms, double principal) { double dn; double pp; dn = terms; pp = pow (1+rate, dn); return principal * pp; } You can place the tvm (Time Value of Money) function into ...

Create C Program with Dynamic Link Library (DLL) using Command Line (Implicit Link) in Windows

Image
The following procedure is to create a C program with a dynamic link library using Developer Command Prompt for VS2012. Brief Introduction to Dynamic Link Library (DLL) The important difference between DLL and static library is that the code of external function is stored in the DLL file, whereas when linking with static library, the code of the external functions is copied to the execution file of the application program. With static library, you just need an execution file whereas with DLL you need both the execution file and the DLL file for the program to work. Implicit or Explicit Link When creating DLL, we have the choice of implicit linking or explicit linking. Since implicit linking is easier and common, this article will focus on implicit linking. For additional information please refer to the following: Linking Implicitly Linking Explicitly Determining Which Linking Method to Use Examples: The following example is a normal C program with functions.  ...