Creating and linking static and shared libraries

In this post, i will explain how to create, link and access static and shared libraries.

Libraries can be created as
1. static library (.a)
2. shared library (.so)

Static library (*.a files) – these libraries are linked to application at compile time and become part of application itself. This increases the size of application.
Shared libraries (*.so files) – Most of us are aware of loading and unloading of dynamic libraries with dlopen(), but there is another method of linking the dynamic libraries. Shared libraries can be accessed in two ways.
a.Dynamically loaded/unloaded, using the dynamic linking loader system function
b.Dynamically linked at run time but statically aware. The libraries must be available during compile/link phase. The shared objects are not included into the executable component but are tied to the execution.

Example for creating static and shared library
//mean.c – file containing a method to calculate the mean
double mean(double a, double b) {
return (a+b) / 2;

//mean.h header file
double mean(double, double);

Create object file from source file
#gcc -c mean.c -o mean.o

Create static library, libmean.a
#ar -cvq libmean.a mean.o
the library name must start with the three letters lib and have the suffix .a

Create shared library ,
#gcc -c -fPIC mean.c -o mean.o
#gcc -shared -Wl,-soname, -o mean.o
the library must start with the three letter lib

How to use the static and shared libraries in program
//main.c where mean() defined in static/shared library is called
#include “mean.h”

int main(int argc, char* argv[]) {

double v1, v2, m;
v1 = 5.2;
v2 = 7.9;

m = mean(v1, v2);

printf(“The mean of %3.2f and %3.2f is %3.2f\n”, v1, v2, m);

return 0;

Statically linked
#gcc -static main.c -L. -lmean -o statically_linked

Dynamically linked at run time but statically aware
#gcc main.c -o dynamically_linked -L. -lmean

Dynamically loaded/unloaded
In this method, compile the program with no reference to shared library. Shared library will be loaded/accessed/unloaded with the system calls dlopen(), dlsym() and dlclose(). Following is how this is done.

Following is the main.c file for dynamically loading and unloding shared libray
#include “mean.h”

int main(int argc, char* argv[]) {

double *lib_handle;
double v1, v2, m;
v1 = 5.2;
v2 = 7.9;

lib_handle = dlopen(“/opt/lib/”, RTLD_LAZY);
if (!lib_handle)
fprintf(stderr, “%s\n”, dlerror());

fn = dlsym(lib_handle, “mean”);
if ((error = dlerror()) != NULL)
fprintf(stderr, “%s\n”, error);

m = (*fn)(v1,v2);

printf(“The mean of %3.2f and %3.2f is %3.2f\n”, v1, v2, m);

return 0;
//compile the program
#gcc main.c -L. -o dynamically_linked

For more detailed information with example, refer to following link

Posted in C Section | Leave a comment

About GPT/MBR disks and diskpart

One of the limitations of MBR partition is limitation of max disk size to 2TB. If disks of size greater than 2TB has to be used, it has to be converted to GPT disk rather than MBR. For more details on limitations on how does the partition look like, refer to below links


Diskpart is a command line utility to manage disks, paritions and volumes. Diskpart command can also be used for convertion of disk to MBR or GPT. For all the options supported by diskpart command do refer to following links


Posted in General Tech Discussion | Tagged , , , , | Leave a comment

“What Got You Here Won’t Get You There” by Marshall Goldsmith, Mark Reiter.

The book is about, what and how to reach higher levels in corporate ladder from where you are now. Book explains about small consistent workplace habits which can make lot of difference which help in moving to next level.
I felt the content is similar to any of the other self improvement books. If other self improvement books are read, this may not a must read and can be given a miss.

I am not sure, if the workplace habits explained by author can be related to current corporate structure and how effective is it, as i think some of the habits will hinder timely execution of projects.

Posted in Personality Development Books | Leave a comment

Building static and shared log4cxx library on Windows and Linux

Recently we used log4cxx for logging in our product. It’s an open source framework and as usual not much documentation is available. We needed to build log4cxx in both windows and linux. First we built shared library on windows (dll) and as we started using the log4cxx we encountered issues which could not be solved. Then we decided to use static libray. Most of the issues were resolved by using static library.

Next we built log4cxx on Linux.

All this information is available at different locations. Here i am consolidating build process (static and dynamic) on both windows and linux

Building dynamic library in windows
Log4cxx Setup, Configuration and build on visual studio 2010

1. Download the latest log4cxx package from “”
2. Download apr and apr-util ZIP packages from “”
3. Extract log4cxx, apr und apr-util to the same directory
4. Rename apr_VERSION and apr-util_VERSION folder to apr and apr-util resulting in a directory with three folder: apache-log4cxx-0.10.0, apr
and apr-util
5. Change into the log4cxx directory and execute configure.bat
6. Change to apr-util/include directory and open apu.hw in a text editor of your choice
7. Find the entry #define APU_HAVE_APR_ICONV, set it to 0 and save the file
8. Open apr_ldap.hw from the same directory and find the entry #define APR_HAS_LDAP, set it to 0 and save the file, too.
9. Change to log4cxx/projects directory and open log4cxx.dsw with VS2010. Answer the converting prompts of VS2010 with yes/ok for each project
(apr, apr-util, log4cxx, xml)

Building log4cxx now will result in around 2000 errors.

Search (Ctrl+F) and find each entry of the “LOG4CXX_LIST_DEF” macro. Move these entries out of its related class and right before the same class. Sometimes it might be required to move a typedef too or add the class right before the macro.

Some examples:

// telnetadapter.h

typedef log4cxx::helpers::SocketPtr Connection;
LOG4CXX_LIST_DEF(ConnectionList, Connection);
class LOG4CXX_EXPORT TelnetAppender : public AppenderSkeleton

// appender.h

class Appender;
LOG4CXX_LIST_DEF(AppenderList, AppenderPtr);

class Layout;
typedef log4cxx::helpers::ObjectPtrT LayoutPtr;

class LOG4CXX_EXPORT Appender :
public virtual spi::OptionHandler

If the compiler complains about KeySet not being member of LoggingEvent, remove the scope (since we moved the type to outside the class in the previous step, these types no longer are inside the class)

// old
LoggingEvent::KeySet set;
// new
KeySet set;
If the compiler complains about insert_iterator not being in the namespace std, add #include to the include section of the source file.

Last but not least, right-click on log4cxx project and select Add References and select the other 3 projects as reference

1) Open log4cxx project/solution
2) Go to log4cxx project, and open its properties dialog
3) Select Configuration Properties->General and set Configuration Type to Static Library (.lib)
4) Go to Configuration Properties->Preprocessor and add to Preprocessor Definitions the following (LOG4CXX_STATIC)
5) Go to Configuration Properties->Code Generation and set RunTime Library to Multi-threaded Debug (/MTd) or /MT
6) Also perform step#5 for apr, apr_util and xml projects
7) In log4cxx project (only for 64bit libray)
librarian->commandline-> add the option /MACHINE:X64
8) Build log4cxx
9) Result is a file named log4cxx.lib with about 37MB of space

1) Use the same configuration /MTD or /MT for the project
2) add LOG4CXX_STATIC to Preprocessor Definitions
3) add the path of log4cxx.lib, apr-1.lib,aprutil-1.lib and xml.lib directory
4) Add all libs required namely (Linker->Input->Additional Dependencies)
WS2_32.Lib, MsWSock.Lib, AdvAPI32.Lib, odbc32.lib, log4cxx.lib apr-1.lib, apr_util-1.lib and xml.lib static libraries
5) Build your project

Building and static and dynamic log4cxx library on Linux
1) extract log4cxx, apr and apr-util
2) go to ../apr-util/xml/expat/
and execute
./configure CPPFLAGS=”-fPIC -Bstatic” –prefix=/staticlibs/ –with-pic –enable-static
make install

3) go to the apr home directory and execute the following
./configure CPPFLAGS=”-fPIC -Bstatic” –prefix=/staticlibs/ –with-pic –enable-static
make install

4) go to ../apr-util/
./configure CPPFLAGS=”-fPIC -Bstatic” –prefix=/staticlibs/ –with-apr=../apr
make install

5) modify some of the files

add directive “#include ”
and apache-log4cxx-0.10.0/src/examples/cpp/console.cpp
add directives


5) go to ../apache-log4cxx-0.10.0 folder
and execute
./configure CPPFLAGS=”-fPIC -Bstatic” –prefix=/staticlibs/ –with-pic –enable-static –with-apr=../apr –with-apr-util=../apr-util
make install

6) for compiling sample application with static library
use : g++ example.cpp -pthread -I/usr/local/include/ -L/staticlibs/ -llog4cxx -lapr-1 -laprutil-1 -lexpat
7) similarly shared library can be used for compiling example.cpp program
8) both log4cxx static and shared library will be placed under /staticlibs/ after build


Posted in Open Source | Leave a comment

“What young India wants” by Chetan Bhagat

I was a bit skeptical before buying this book as Chetan Bhagat is a novel writer. Since author is a well know writer and i liked couple of movies based on his novel, went ahead and bought the book. This is the first book of Chetan Bhagat i have read :).

Many of us know the most common issues of India like corruption, our education system, political issues, corporate issues,terror attacks, corruption in cricket to mention a few. Book is all about this. Author has discussed about Anna Hazare movement which has finally led to down fall of congress government in Delhi. Book is just not only about the issues but has suggestions on how the issues could be resolved.

In my view, most of us know the issues discussed in the book. But the book is a documentation of all this at single place. Worth reading the book.

Posted in Personality Development Books | Leave a comment

Lateral Thinking by EDWARD DE BONO

This book provides information on how our mind is tuned to think in only specific way. By reading the book one can learn to think differently (lateral thinking) to find a solution. Some of the example problem and solution explained are really interesting which will change the way one thinks about solving the problem.

I felt some of the chapters are not interesting and can be skipped.

Posted in Personality Development Books | Leave a comment

Debugging application core dump

Quite often, developers need to analyze core dumps generated in customer production system’s to fix the issue. It could so happen that the version of application/patches installed in developer’s machine are different than the one customer has. When the core file is loaded in GDB, errors similar to
/usr/lib/hpux64/ in the core file is different from
/usr/lib/hpux64/ used by gdb

could be displayed by GDB. Starting to analyze the core file with these set of different libraries will not provide the required results. GDB will not provide useful data even if one of the libraries is mismatching.

So developer is left with two choices
a. setup a system similar to customer environment (os+patches and application+patches ) or
b. get the all libraries which are used by binary which core dumped from customer setup.

Option “a” is tedious task and as such option “b” is definitely much easier and less time consuming.

Consider option “b”. How to can we get all the libraries used by application binary which resulted in crash? Use the following set of command to collect all information from customer setup

1. execute “file ” command to get the binary which core dumped.
2. use the ldd command to get all the library information which the binary uses.
eg. If a.out is the binary name
# ldd a.out

a.out: => /usr/lib/hpux32/ => /usr/lib/hpux32/
Collect all the libraries listed from customer system and copy them to specific location in development machine say /tmp/lib/

Set the following environment variable

Start analyzing the core file

Errors which were displayed earlier will not be displayed now.

Libraries displayed need not be only system libraries, they can be application specific libraries too.

Posted in C Section | Leave a comment