链接器错误与log4cxx

时间:2016-07-07 04:43:14

标签: c++ log4cxx

我想使用Apache log4cxx并遇到问题。我在Debian 8上运行并安装了log4cxx using

apt-get install liblog4cxx10-dev

我已将Documentation Page中的一个代码段嵌入到以下代码中:

#include <log4cxx/logger.h>
#include <math.h>

int main(int argc, char* argv[]) {
    log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("com.foo"));
    int i = 10;
    const char* region = "World";

    LOG4CXX_INFO(logger, "Simple message text.")
    LOG4CXX_INFO(logger, "Hello, " << region)
    LOG4CXX_DEBUG(logger, L"Iteration " << i)
    LOG4CXX_DEBUG(logger, "e^10 = " << std::scientific << exp(10.0))
    LOG4CXX_WARN(logger, L"" << i << L" is the number of the iteration.")

    return 0;
}

使用Eclipse CDT(从源代码构建的gcc 5.3.0)进行编译时,我收到以下链接器错误:

make all 
Building file: ../src/tester.cxx
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/tester.d" -MT"src/tester.o" -o "src/tester.o" "../src/tester.cxx"
Finished building: ../src/tester.cxx

Building target: logtest
Invoking: GCC C++ Linker
g++ -L/usr/lib/x86_64-linux-gnu -o "logtest"  ./src/tester.o   -llog4cxx
./src/tester.o: In function `main':
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::ostream&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
makefile:45: recipe for target 'logtest' failed
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
collect2: error: ld returned 1 exit status
make: *** [logtest] Error 1

我仔细检查了apt安装库的位置:

root@starsim-dev:/home/andand# find / -name *log4cxx*
/usr/lib/x86_64-linux-gnu/liblog4cxx.a
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10.0.0
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10
/usr/lib/x86_64-linux-gnu/liblog4cxx.so
/usr/lib/x86_64-linux-gnu/pkgconfig/liblog4cxx.pc
/usr/include/log4cxx
/usr/include/log4cxx/private/log4cxx_private.h
/usr/include/log4cxx/log4cxx.h
/usr/share/doc/liblog4cxx10
/usr/share/doc/liblog4cxx10-dev
/usr/share/lintian/overrides/liblog4cxx10
/usr/share/lintian/overrides/liblog4cxx10-dev
/var/cache/apt/archives/liblog4cxx10-dev_0.10.0-4_amd64.deb
/var/cache/apt/archives/liblog4cxx10_0.10.0-4_amd64.deb
/var/lib/dpkg/info/liblog4cxx10:amd64.postinst
/var/lib/dpkg/info/liblog4cxx10:amd64.shlibs
/var/lib/dpkg/info/liblog4cxx10-dev.list
/var/lib/dpkg/info/liblog4cxx10:amd64.list
/var/lib/dpkg/info/liblog4cxx10:amd64.md5sums
/var/lib/dpkg/info/liblog4cxx10:amd64.postrm
/var/lib/dpkg/info/liblog4cxx10-dev.md5sums

Apache网站上的文档并不完全清楚存在其他库依赖项。有没有人对我所缺少的东西有所了解?

1 个答案:

答案 0 :(得分:1)

我在Ubuntu 18.04上尝试使用log4cxx时遇到了同一组链接器错误。通过将以下其他Apache库添加到我的MakeFile中,我能够使他们的代码段有效-包括上面的代码段

LIBS+= -llog4cxx -laprutil-1 -lapr-1

确保按以上顺序列出它们。