1

我对 C++ 比较陌生,但我使用过log4j(它是 Python 克隆logging)。因此,我想log4cxx用于登录我的新 C++ 项目。

我安装了 log4cxx

brew install log4cxx

现在我需要将它包含在我的源文件中。我试过了,例如

#include "log4cxx/logger.h"

namespace EnsembleClustering {

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

}

这似乎还不够,因为这给了我链接器错误

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

我需要包括什么才能开始使用 log4cxx?或者我是否需要通过修改我的 Eclipse 构建设置来显式链接到 log4cxx?log4cxx 简介在这方面并不是特别有用。

4

2 回答 2

1

这是我设法正确链接 log4cxx 的方法。Homebrew 会提示您安装库的位置:

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

正如用户组件 10 所说,我需要在链接器行中添加-L<dir>和,其中is和是 library name (not filename) 。-l<lib><dir>/usr/local/Cellar/log4cxx/0.10.0/lib/<lib>log4cxx

在 Eclipse 中,我需要编辑以下设置:

在此处输入图像描述

于 2012-10-18T11:41:18.767 回答
1

为了使这稍微简单一些,你可以做

brew link log4cxx 

它将创建指向 /usr/local/lib 和 /usr/local/include 中的库的符号链接,这些库通常位于默认的链接器/包含搜索路径中。

您仍然需要指定 -llog4cxx。

于 2013-03-09T02:31:55.917 回答