Posts

Showing posts from August, 2013

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.  #in

Create C Program with Dynamic Link Library (DLL) using Visual Studio 2012 (Implicit Link)

Image
The following procedure is to create a C program with a dynamic link library using Microsoft Visual Studio Express 2012. 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 without

Create C Program with Static Library using Command Line in Windows

Image
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(

Create C Program with Static Library using Visual Studio 2012

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

Compiling C Program in Linux Part 1

Most of Linux are written in C. To compile C program we need a minimum of the follow packages: Gcc – the compiler for C program Glibc – contains standard C libraries Glibc-devel – contain standard headers file Binutils – contains various utilities such as assembler and linker Writing C Program You can use many tools in write C code. The most popular tool is vim and gedit . Listed below is a list of tools. GUI (Graphic User Interface) Editing Tools gedit geany Eclipse Command Line Editing Tools vim vi Emacs A Simple C Program Listed below is a simple C program called sick.c #include <stdio.h> int main() { printf ("Sick of printing hello world!\n"); return 0; } To compile the program $gcc -Wall sick.c -o verysick The option -Wall display all possible warning (-W is for warning and all is to display all possible warnings) and -o indicate to the compiler that user want the output to be name after the -o option. To run t