测试代码是官方给的样例,
// main.cpp
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
int main(int argc, char** argv) {
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
appender1->setLayout(new log4cpp::BasicLayout());
log4cpp::Appender *appender2 = new log4cpp::FileAppender("default", "program.log");
appender2->setLayout(new log4cpp::BasicLayout());
log4cpp::Category& root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::WARN);
root.addAppender(appender1);
log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
sub1.addAppender(appender2);
// use of functions for logging messages
root.error("root error");
root.info("root info");
sub1.error("sub1 error");
sub1.warn("sub1 warn");
// printf-style for logging variables
root.warn("%d + %d == %s ?", 1, 1, "two");
// use of streams for logging messages
root << log4cpp::Priority::ERROR << "Streamed root error";
root << log4cpp::Priority::INFO << "Streamed root info";
sub1 << log4cpp::Priority::ERROR << "Streamed sub1 error";
sub1 << log4cpp::Priority::WARN << "Streamed sub1 warn";
// or this way:
root.errorStream() << "Another streamed error";
return 0;
}
错误如下:
/tmp/ccoWxTVC.o:在函数‘__static_initialization_and_destruction_0(int, int)’中:
/usr/local/include/log4cpp/Appender.hh:164:对‘log4cpp::Appender::AppenderMapStorageInitializer::AppenderMapStorageInitializer()’未定义的引用
/usr/local/include/log4cpp/Appender.hh:164:对‘log4cpp::Appender::AppenderMapStorageInitializer::~AppenderMapStorageInitializer()’未定义的引用
collect2: 错误:ld 返回 1
我认为最主要的是__static_initialization_and_destruction_0这个函数,但是我不知道是怎么回事?
Appender.hh的 164 行是
static Appender::AppenderMapStorageInitializer appenderMapStorageInitializer; // static initializer for every translation unit
系统是CentOS8,先记录一下。
网友评论