Tuesday 21 January 2014

C Program to Check Increasing, Decreasing and Bouncy Numbers


  Starting from left-to-right if no digit is exceeded by the digit to its left the number is called an increasing number. 125569 is an increasing number. Similarly if no digit is exceeded by the digit to its right the number is called a decreasing number. 77632 is a decreasing number. A positive integer that is neither increasing nor decreasing is called a bouncy number. 244369 is a bouncy number.

The C Program given with this post checks whether the given number is an increasing, decreasing or bouncy number.


Figure Below Shows a Sample Output


Click here to download the C Program.

Creating C Dynamic Libraries in Linux


  Dynamic library is a relocatable object file with metadata. Dynamic libraries are linked at run time. They are built from one or more object files ( .o files). In Linux Dynamic libraries are called shared objects and the extension is .so. The name of a Dynamic library should start with lib. For example libtest.so is a valid Dynamic library name. 

Creating the Dynamic Library File

Let the name of the C Program file be p1.c,

  • Creating object files: Use gcc to create object files as Position independent Code as follows.

          gcc -fPIC -c p1.c 

  • Creating the Shared Object: 
          gcc -shared p1.o -o libtest.so

Storing & Linking the Static Library File

You need to change the LD_LIBRARY_PATH as follows,

 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/newpath

  • Store the dynamic library in the standard directory /usr/lib

          Compile & link the dynamic library file with target.c as follows,
          gcc target.c -ltest

  • Store the static library in a directory you have created. 
          For example in /usr/student/mylib.
          Compile & link the static library file with target.c as follows,
          gcc target.c -L/usr/student/mylib -ltest


  • You can also change the LD_LIBRARY_PATH as follows so that the compiler will search even the new path, 

          export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/student/mylib

          Now compile & link the dynamic library file with target.c as follows,
          gcc target.c -ltest


Thursday 9 January 2014

Creating C Static Libraries in Linux


  A static library is a set of functions and variables which can be added to the target application at compile time. In Linux static libraries has an extension .a. Archive files are made up of object files ( Extension is .o ). The name of the static library should start with lib. For example libtest.a is a valid static library name

Creating the Static Library File

The command ar is used to create static libraries.

The procedure is described below.

Let target.c be the source file to which static libraries are to be linked.

Let p1.c and p2.c contain the functions to be added to the static library libtest.a.

gcc p1.c creates p1.o and gcc p2.c creates p2.o. 

The command to create the static library libtest.a is shown below,

ar -rcs libtest.a p1.o p2.o

Storing & Linking the Static Library File

  • Store the static library in a standard directory like /usr/local/lib.
            Compile & link the static library file with target.c as follows,
         
                     gcc target.c -ltest
  • Store the static library in a directory you have created. For example in /usr/student/mylib.
            Compile & link the static library file with target.c as follows,         
                     gcc target.c -L/usr/student/mylib -ltest

      The command nm can be used to find out the object files stored inside a static library file.