首页 > 解决方案 > 未定义符号 - 在 centos 7 中使用 log4cxx

问题描述

我很难让我的代码片段在 centos 7 上运行。

所以,我在盒子上安装了这些包:

log4cxx.x86_64、log4cxx-devel.x86_64、apr.x86_64、apr-devel.x86_64、apr-util.x86_64、apr-util-devel.x86_64、glib2.x86_64、glib2-devel.x86_64

在 CMakeList.txt 中,我尝试了一堆组合(成功构建),但在执行二进制文件时我得到了相同的结果。

目前,我有这个:

find_package(PkgConfig)
find_library(LOG4CXX_LIBRARY log4cxx)

虽然我很肯定它可以找到图书馆,但我也尝试过:

-llog4cxx -lapr-1 -laprutil-1 -lexpat -lglib-2.0

我使用两种配置进行构建,当我运行可执行输出时,我会得到:

undefined symbol: _ZN7log4cxx3xml15DOMConfigurator9configureERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

'nm' 输出为:

$ nm -D /usr/lib64/liblog4cxx.so | grep _ZN7log4cxx3xml15DOMConfigurator9configure
00000000000f9ff0 T _ZN7log4cxx3xml15DOMConfigurator9configureERKSbIwSt11char_traitsIwESaIwEE
00000000000f9e40 T _ZN7log4cxx3xml15DOMConfigurator9configureERKSs

在我的 .cpp 中,我基本上使用了这个:

try {
    log4cxx::xml::DOMConfigurator::configure("/root/1.xml");
}
catch (log4cxx::helpers::Exception&) {
    fprintf( stderr, "Error on loading Log-config file" );
    return -1;
}

ps:同一个项目在 FreeBSD 12 上编译和运行没有问题。

标签: c++c++17centos7log4cxx

解决方案


这似乎是C++ 标准库 ABI 不匹配;不匹配的符号分解为:

log4cxx::xml::DOMConfigurator::configure(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)
log4cxx::xml::DOMConfigurator::configure(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)

您的评论表明您在 C++17 模式下使用 GCC 10。CentOS 7 附带 GCC 4.8.5(甚至不支持 C++17),它更老,并且早std::basic_string于 GCC 5.1 中的 ABI 更改(上面的第一个链接)。

您安装的预构建库将由“默认”CentOS 7 工具链构建。

乍一看,我建议从源代码构建您的依赖项,以便使用相同的工具链构建所有内容。

使用一些骇客(例如_GLIBCXX_USE_CXX11_ABI宏)可能可以使两者正确排列,但是如果是这样,您将需要其他人来提供帮助。

char/wchar_t不匹配也很有趣,尽管我怀疑这只是基于错误出现顺序的红鲱鱼。)


推荐阅读