1

I am relatively new to C++, but I have used log4j (and it's Python clone logging). Therefore I want to use log4cxx for logging in my new C++ project.

I installed log4cxx with

brew install log4cxx

Now I need to include it in my source files. I tried, e.g.

#include "log4cxx/logger.h"

namespace EnsembleClustering {

METISGraphParser::METISGraphParser() {
    // logging
    log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("METISGraphParser"));
    logger->setLevel(log4cxx::Level::getInfo());
    this->logger = logger;
}

}

which does not seem to include enough, since this gives me the linker error

13:06:29 **** Incremental Build of configuration Debug for project EnsembleClustering ****
make all 
Building file: ../src/METISGraphParser.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/METISGraphParser.d" -MT"src/METISGraphParser.d" -o "src/METISGraphParser.o" "../src/METISGraphParser.cpp"
../src/METISGraphParser.cpp: In member function 'virtual void EnsembleClustering::METISGraphParser::connectNode(EnsembleClustering::id, std::vector<unsigned int, std::allocator<unsigned int> >)':
../src/METISGraphParser.cpp:128: warning: unused variable 'deg'
../src/METISGraphParser.cpp: In member function 'virtual EnsembleClustering::Graph EnsembleClustering::METISGraphParser::parse(std::string)':
../src/METISGraphParser.cpp:112: warning: control reaches end of non-void function
Finished building: ../src/METISGraphParser.cpp

Building target: EnsembleClustering
Invoking: MacOS X C++ Linker
g++  -o "EnsembleClustering"  ./src/EdgeScoring.o ./src/EdgeTripleGraphData.o ./src/EnsembleClustering.o ./src/EnsembleClusteringAlgo.o ./src/Graph.o ./src/METISGraphParser.o ./src/Matching.o ./src/Modularity.o   
Undefined symbols for architecture x86_64:
  "log4cxx::spi::LocationInfo::LocationInfo(char const*, char const*, int)", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::Level::getInfo()", referenced from:
      EnsembleClustering::METISGraphParser::METISGraphParser()in METISGraphParser.o
      EnsembleClustering::METISGraphParser::METISGraphParser()in METISGraphParser.o
  "log4cxx::Level::getDebug()", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::Logger::getLogger(char const*)", referenced from:
      EnsembleClustering::METISGraphParser::METISGraphParser()in METISGraphParser.o
      EnsembleClustering::METISGraphParser::METISGraphParser()in METISGraphParser.o
  "log4cxx::helpers::MessageBuffer::str(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::helpers::MessageBuffer::MessageBuffer()", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::helpers::MessageBuffer::~MessageBuffer()", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::helpers::MessageBuffer::operator<<(char const*)", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::helpers::ObjectPtrBase::exchange(void**, void*)", referenced from:
      log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::exchange(log4cxx::Logger const*)in METISGraphParser.o
  "log4cxx::helpers::ObjectPtrBase::ObjectPtrBase()", referenced from:
      log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::ObjectPtrT()in METISGraphParser.o
  "log4cxx::helpers::ObjectPtrBase::~ObjectPtrBase()", referenced from:
      log4cxx::helpers::ObjectPtrT<log4cxx::Level>::~ObjectPtrT()in METISGraphParser.o
      log4cxx::helpers::ObjectPtrT<log4cxx::Level>::~ObjectPtrT()in METISGraphParser.o
      log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::~ObjectPtrT()in METISGraphParser.o
      log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::~ObjectPtrT()in METISGraphParser.o
  "log4cxx::helpers::CharMessageBuffer::operator<<(int)", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::Logger::isDebugEnabled() const", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const", referenced from:
      EnsembleClustering::METISGraphParser::initGraph(int, int)in METISGraphParser.o
  "typeinfo for log4cxx::helpers::ObjectPtrBase", referenced from:
      typeinfo for log4cxx::helpers::ObjectPtrT<log4cxx::Level>in METISGraphParser.o
      typeinfo for log4cxx::helpers::ObjectPtrT<log4cxx::Logger>in METISGraphParser.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [EnsembleClustering] Error 1

What do I need to include to get me started with log4cxx? Or do I need to explicitly link to log4cxx by modifying my Eclipse build settings? The Introduction to log4cxx isn't particularly helpful in this respect.

clstaudt
  • 21,436
  • 45
  • 156
  • 239
  • In your link line you don't seem to be adding any log4cxx library paths or libraries. Is that correct? Aren't there some libraries that you have to link with? – Component 10 Oct 18 '12 at 11:19
  • That's what I don't know. I did not change my build settings in Eclipse. So I have to link explicitly to additional libraries after I installed them? If yes, how? How do I find out their path? – clstaudt Oct 18 '12 at 11:21
  • 1
    If you want to use installed libraries then you'll have to add reference to them on your link line with `-L` and `-l`. Check what you did when you installed log4cxx and any README - I've not used it so couldn't say but it could be specific to your installation in any case. – Component 10 Oct 18 '12 at 11:24
  • Ok, thanks. Isn't there a command that gives you the paths of a library by name? (Similar to `which `). – clstaudt Oct 18 '12 at 11:28
  • No. Unless the library has been placed directly in one of the standard directories searched by the linker, it'll never find it. Even if it did, you'll still need to know the name of the library you're linking with and specify it also on the linker command line. – Component 10 Oct 18 '12 at 13:00

2 Answers2

1

Here's how I managed to link log4cxx properly. Homebrew gives you a hint where it has installed the library:

cls ~/ $ brew info log4cxx
log4cxx: stable 0.10.0
http://logging.apache.org/log4cxx/index.html
Depends on: automake, libtool
/usr/local/Cellar/log4cxx/0.10.0 (182 files, 7,3M) *
https://github.com/mxcl/homebrew/commits/master/Library/Formula/log4cxx.rb

As user Component 10 has said, I need to add -L<dir>and -l<lib>to the linker line, where <dir> is /usr/local/Cellar/log4cxx/0.10.0/lib/and <lib> is the library name (not filename) log4cxx.

In Eclipse, I needed to edit the following settings:

enter image description here

clstaudt
  • 21,436
  • 45
  • 156
  • 239
1

To make this slightly simpler you can do

brew link log4cxx 

It'll create the symlinks to the libraries in /usr/local/lib and /usr/local/include which are usually in the default linker/include search paths.

You'll still need to specify -llog4cxx.

Arran Cudbard-Bell
  • 5,912
  • 2
  • 26
  • 48