未定义符号 - 在 centos 7 中使用 log4cxx

undefined symbol - using log4cxx in centos 7

我很难让我的代码片段在 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 上编译和 运行 没有问题。

这似乎是 a C++ standard library ABI mismatch; the mismatched symbols demangle 至:

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),它更旧,并且早于 GCC 5.1 中 ABI 更改为 std::basic_string(上面的第一个 link)。

您安装的预构建库将由 "default" CentOS 7 工具链构建。

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

使用 _GLIBCXX_USE_CXX11_ABI 宏之类的一些技巧可能可以使两者正确对齐,但如果是这样,您需要其他人来帮忙。

char/wchar_t 不匹配也很有趣,尽管根据错误出现的顺序我怀疑这只是转移注意力。)

您用 C++17 标记了问题,但 centos-7 的默认编译器非常旧。您至少可以从 scl(softwarecollections) 安装带有 devtoolset-8 的 gcc-8。 devtoolset-8 for centos 7

您也可以使用 define _GLIBCXX_USE_CXX11_ABI=0 来禁用新的 ABI。使用此定义编译的代码将在没有新 ABI 的旧 libstdc++ 上运行。