0

我是 log4cpp 和 swig 包装器的新手。我正在尝试使用 log4cpp 编写一个用于简单日志记录的接口。我已经在我的 Ubuntu 机器上安装了 log4cpp 和 swig。

log4cpp.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"
#include "log4cpp.h"

void writeLog() {
        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";
}

log4cpp.h:

void writeLog(void);

log4cpp.i:

%module log4cpp

%{
#include "log4cpp.h"
%}

%inline %{
extern void writeLog(void);
%}

我已完成以下步骤来生成 log4cpp.so 文件:

swig -tcl -c++ log4cpp.i
g++ -c -fPIC log4cpp.cpp log4cpp_wrap.cxx -I/usr/include/tcl8.5
g++ -shared log4cpp.o log4cpp_wrap.o -o log4cpp.so

它生成的log4cpp_wrap.cxx, log4cpp.o, log4cpp_wrap.o and log4cpp.so文件没有任何警告和错误。

每当我在 tcl 中运行以下命令时。

load ./log4cpp.so

它会生成一个未定义的符号错误:

% load ./log4cpp.so
couldn't load file "./log4cpp.so": ./log4cpp.so: undefined symbol: _ZN7log4cpp8Appender29AppenderMapStorageInitializerD1Ev

该怎么做才能消除此错误?

4

1 回答 1

2

您需要将您的 SWIG 共享库链接到 log4cxx,就像使用该库的任何其他 C++ 应用程序一样。所以当你打电话

g++ -shared log4cpp.o log4cpp_wrap.o -o log4cpp.so

它真的需要是这样的(但适应了真正的图书馆和搜索路径)

g++ -shared log4cpp.o log4cpp_wrap.o -L/path/to/your/install/of/log4cxx -llog4cxx -o log4cpp.so 
于 2018-01-11T07:56:34.210 回答