log4cpp给出了“未解决的外部”错误

时间:2009-03-26 15:34:32

标签: c++ logging c++builder-2007

我正在努力使这个库工作并在Borland Codegear 2007上运行最新版log4cpp提供的测试,其中包含一个用于Borland C ++ Builder 5的bpr项目,该项目旨在构建和运行不同的测试。问题是我试图用2007版本打开这个项目,该版本必须进行项目转换。我得到了奇怪的“未解决的外部”错误。然后,我试图自己构建项目而不转换任何东西,但却陷入了同样的困境。

我正在尝试运行以下测试:

#include <stdio.h>
#include "log4cpp/Portability.hh"
#ifdef LOG4CPP_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#ifdef LOG4CPP_HAVE_SYSLOG
#include "log4cpp/SyslogAppender.hh"
#endif
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/NDC.hh"

int main(int argc, char** argv) {
    log4cpp::Appender* appender;
#ifdef LOG4CPP_HAVE_SYSLOG
    log4cpp::SyslogAppender* syslogAppender;

    syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
#else
    log4cpp::Appender* syslogAppender;

    syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
#endif

    if (argc < 2) {
        appender = new log4cpp::OstreamAppender("default", &std::cout);
    } else {
        appender = new log4cpp::FileAppender("default", argv[1]);
    }

    syslogAppender->setLayout(new log4cpp::BasicLayout());
    appender->setLayout(new log4cpp::BasicLayout());

    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(syslogAppender);
    root.setPriority(log4cpp::Priority::ERROR);

    log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
    sub1.addAppender(appender);

    log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));

    log4cpp::NDC::push(std::string("ndc1"));

    std::cout << " root prio = " << root.getPriority() << std::endl;
    std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");

    sub1.setPriority(log4cpp::Priority::INFO);
    std::cout << " root prio = " << root.getPriority() << std::endl;
    std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

    std::cout << "priority info" << std::endl;
    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");

    sub2.warnStream() << "streamed warn";

    sub2 << log4cpp::Priority::WARN << "warn2" << " warn3" 
             << log4cpp::eol << " warn4";

    {
        for(int i = 0; i < 10000; i++) {
            char ndc2[20];
            sprintf(ndc2, "i=%d", i);
            log4cpp::NDC::push(ndc2);
            sub1.info("%s%d", "i = ", i);
            if ((i % 10) == 0) {
                sub1.log(log4cpp::Priority::NOTICE, "reopen log");
                if (log4cpp::Appender::reopenAll()) {
                    sub1.info("log reopened");
                } else {
                    sub1.warn("could not reopen log");
                }
            }
#ifndef WIN32
            sleep(1);
#endif
            log4cpp::NDC::pop();
        }
    }

    return 0;
}

错误都是关于“未解决的外部”,例如:

[ILINK32 Error] Error: Unresolved external 'log4cpp::Category::warn(const char *, ...)' referenced from C:\DOCUMENTS AND SETTINGS\MLERMA\MIS DOCUMENTOS\RAD STUDIO\PROJECTS\DEBUG\TESTMAIN.OBJ

我每次调用log4cpp函数都会遇到这种错误,都是指TESTMAIN.OBJ。

有关于此的任何想法吗?是否有人在Borland工作过log4cpp?

2 个答案:

答案 0 :(得分:1)

您是否在log4cpp库(.LIB)中进行链接?我不熟悉BCB5或Codegear,但检查项目设置中的链接库,并确保包含log4cpp库。

如果是,那么编译器在哪里寻找库可能会有问题。通常IDE会有一个项目级别或全局设置,说明编译器将在哪里查找.lib文件。检查该设置并确保其中一个目录包含log4cpp .lib文件。

麦克

答案 1 :(得分:0)

尝试添加#pragma comment(lib,"ws2_32.lib")您的头文件。