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


This entry was posted in Open Source. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s