SWIG tcl:log4cpp包装器的未定义符号错误

时间:2018-01-10 10:21:33

标签: tcl swig log4cpp

我是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

如何删除此错误?

1 个答案:

答案 0 :(得分: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