How to Build Log4cxx From Source

Log4cxx is a hierarchical logging library for C++ based on the popular Log4j for Java. Both Log4cxx and Log4j are Apache community projects and are quite mature. Recently I wanted to integrate Log4cxx into a linux based project of mine and got stumped trying to build it from source. As it turns out the instructions on the official website are difficult to follow and to compound matters there are some source code changes that need to be made before it will compile successfully!

This tutorial provides a step by step process for getting Log4cxx to compile from source on linux that you will definitely find useful if you have run into problems. The tutorial is based from the linux command line.

Getting the Source Code

Now the first thing I found that was not particularly obvious is that Log4cxx is dependent on other Apache libraries and these must be built as well. These are the Apache Portable Runtime (APR) and the Apache Portable Runtime Utilities (APR-util). So you will need to download the following three source code packages:

  1. apr-1.4.2
  2. apr-util-1.3.9
  3. apache-log4cxx-0.10.0

You should be sure to download the versions that are indicated as that is what this tutorial was written with reference to.

Extract to an Appropriate Directory Structure

Now that you have all the source code packages you will need to extract them all into a convenient set of folders. To this end create a directory named whatever you want in your home folder, for the tutorial that folder will be: ~/log4cxx_build.

Now extract each of the files you downloaded previously into that folder. You should end up with the folder structure:

  • ~/log4cxx_build
    • /apr-1.4.2
    • /apr-util-1.3.9
    • /apache-log4cxx-0.10.0

Make and Install Dependencies

In order to build Log4cxx you must first build it's dependencies. First you need to build APR (as APR-util depends on this).
From this folder: ~/log4cxx_build/apr-1.4.2. Run the following commands:

user@computer:$ ./configure --enable-threads CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
user@computer:$ make
user@computer:$ sudo make install

The first step above runs the autotools script and generates the appropriate makefiles based on your system configuration. Next the code is compiled and finally installed onto your system.

Next you need to do something very similar for APR-util:
From this folder: ~/log4cxx_build/apr-util-1.3.9. Run the following commands:

user@computer:$ ./configure --with-apr=../apr-1.4.2 CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
user@computer:$ make
user@computer:$ sudo make install

Make Some Minor Changes to the Log4cxx Source Code

The issue here is that a couple of the Log4cxx source files are missing an include directive. So edit the files:

  • ~/log4cxx_build/apache-log4cxx-0.10.0/src/main/cpp/inputstreamreader.cpp
  • ~/log4cxx_build/apache-log4cxx-0.10.0/src/main/cpp/socketoutputstream.cpp

and add the following line at the end of the other include directives.

#include <string.h>

If you want the examples to complie...

This is enough to get the Log4cxx library files to compile but if you want to have access to the examples then you will also need to edit the file:

  • ~/log4cxx_build/apache-log4cxx-0.10.0/src/examples/cpp/console.cpp

and add the following lines at the end of the other include directives


#include <string.h>
#include <stdio.h>

Build Log4cxx

Now finally we can actually build the library that we were after in the first place!

From this folder: ~/log4cxx_build/apache-log4cxx-0.10.0. Run the following commands:

user@computer:$ ./configure --with-apr=../apr-1.4.2 --with-apr-util=../apr-util-1.3.9 CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
user@computer:$ make

Once that has finished successfully you should have both .a and .so files produced in this folder:

  • ~/log4cxx_build/apache-log4cxx-0.10.0/src/main/cpp/.libs/

9 Responses to “How to Build Log4cxx From Source”


  1. perfect.
    thanks!

    rock
    October 23rd, 2012
  2. This really helped me out
    Thanks!

    Tzafrir
    November 29th, 2012
  3. Thank you. This is very helpful !

    cristian
    September 10th, 2015
  4. Very useful, was able to build for my linux in a short amount of time. But now I need to build a cross-compile version that will use the arm compiler. Any ideas?

    joann
    April 2nd, 2016
    • That would depend on if autotools supports ARM or not. Autotools is the build system that log4cxx comes packaged with, the configure calls in the instructions tell autotools to generate the makefiles appropriate for the system being compiled on. I’m unsure if ARM is supported, you’ll need to do your own investigation 🙂

      radman
      June 3rd, 2016
  5. Thank you. Very helpful!

    Stepan
    October 18th, 2016
  6. Dude – you are the bomb. The instructions on Apache are a disaster. (Download ant, ant-contrib, and then watch the build fail).

    What I don’t understand is that I’ve compiled this on at least 2 other Linux machines in past few years, I don’t remember needing to patch the files before building. Also – I’m sure didn’t use Ant on those machines, so there must have been other instructions for building that I no longer see on Apache’s web site.

    Anyway – thanks a bazillion!

    Shalom Shachne Goldberg
    May 31st, 2017
    • No worries, I’m glad it was helpful to you 🙂

      radman
      June 1st, 2017

Leave a Comment