通过 CMake 构建和使用 Log4cplus

问题描述 投票:0回答:1

社区,

我在通过 Embarcadero 构建和使用 Log4cplus 库时遇到以下问题。 首先,我从 http://sourceforge.net/p/log4cplus/wiki/Home/ 下载库,然后导航到要构建库的目录,然后输入 cmake -G "Borland Makefiles" mypathtotherootomypreviousdownload 。然后它说,构建文件已写入当前目录。 现在我启动 MSYS sh.exe 并输入 make。这也确实有效。我得到一个 bin 文件夹,其中包含库的各种工作测试(作为 exe 文件)。在这个bin文件夹中还有一个dll log4cplusUD.dll。现在我还在src文件夹中找到了相应的LIB log4cplusUD.lib。 我当然熟悉如何将动态库与 Embarcadero 静态链接。但在尝试编译时

#pragma hdrstop
#pragma argsused

#ifdef _WIN32
#include <tchar.h>
#else
  typedef char _TCHAR;
  #define _tmain main
#endif

#include <stdio.h>

#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <log4cplus/loggingmacros.h>
#include <iomanip>

using namespace std;
using namespace log4cplus;



 int _tmain(int argc, _TCHAR* argv[]) 
{

    log4cplus::initialize ();
    SharedAppenderPtr append_1(new ConsoleAppender());
    append_1->setName(LOG4CPLUS_TEXT("First"));
    Logger::getRoot().addAppender(append_1);

    Logger root = Logger::getRoot();
    Logger test = Logger::getInstance(LOG4CPLUS_TEXT("test"));

    LOG4CPLUS_DEBUG(root,
                    "This is"
                    << " a reall"
                    << "y long message." << endl
                    << "Just testing it out" << endl
                    << "What do you think?");
    test.setLogLevel(NOT_SET_LOG_LEVEL);
    LOG4CPLUS_DEBUG(test, "This is a bool: " << true);
    LOG4CPLUS_INFO(test, "This is a char: " << 'x');
    LOG4CPLUS_INFO(test, "This is a short: " << static_cast<short>(-100));
    LOG4CPLUS_INFO(test, "This is a unsigned short: "
        << static_cast<unsigned short>(100));
    LOG4CPLUS_INFO(test, "This is a int: " << 1000);
    LOG4CPLUS_INFO(test, "This is a unsigned int: " << 1000u);
    LOG4CPLUS_INFO(test, "This is a long(hex): " << hex << 100000000l);
    LOG4CPLUS_INFO(test, "This is a unsigned long: " << 100000000ul);
    LOG4CPLUS_WARN(test, "This is a float: " << 1.2345f);
    LOG4CPLUS_ERROR(test,
                    "This is a double: "
                    << setprecision(15)
                    << 1.2345234234);
    LOG4CPLUS_FATAL(test,
                    "This is a long double: "
                    << setprecision(15)
                    << 123452342342.25L);
    LOG4CPLUS_WARN(test, "The following message is empty:");
    LOG4CPLUS_WARN(test, "");

    return 0;
}

我收到链接器错误, 未解析外部符号 log4cplus::Logger::getInstance。

上面的代码只是从 sh.exe 运行 make 时编译和运行良好的测试之一。 那我做错了什么?

我还尝试将上面的 main.cpp 与 log4cplusUD.lib 与以下 CMakeLists.txt 文件链接

cmake_minimum_required (VERSION 2.6)
project (log4cplustest)

include_directories("C:/Users/fin/Software/log4cplus-1.2.0-rc3/log4cplus-1.2.0-rc3/include")
add_executable(log4cplustest main.cpp)
target_link_libraries(log4cplustest log4cplusUD)

但是我得到了同样的链接器错误!

c++ cmake msys borland-c++ log4cplus
1个回答
0
投票

如何解决这个问题? 我遇到同样的错误。

© www.soinside.com 2019 - 2024. All rights reserved.