Boost 日志崩溃的简单示例 (Linux)

Boost log crashes with a simple example (Linux)

好的,我知道,这个问题听起来与其他问题相似(none 个问题已得到回答),但我无法弄清楚为什么会失败。最简单的示例可能是这样的:

#include <boost/log/trivial.hpp>

int main() {
  BOOST_LOG_TRIVIAL(info) << "hello world\n";
}

当然,要编译这个我们需要 boost 作为依赖项,为此我决定使用 Conan,这是 conanfile.txt:

[requires]
boost/1.75.0

[generators]
cmake_find_package

而且,好吧,一个简单的 CMake 来构建它:

cmake_minimum_required(VERSION 3.15)
project(concurrency)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
find_package(Boost REQUIRED COMPONENTS log)

add_executable(concurrency main.cpp)
target_link_libraries(concurrency Boost::log)

这在 Windows、macOS 和 Linux 中构建没有问题(在 Fedora 33 和 Ubuntu rolling 中测试)。如果我 运行 在 Windows 和 macOS 中简单登录,它完全可以正常工作,但在 Linux 中它会因分段错误而失败(Fedora 33 中的 运行):

Reading symbols from concurrency...
(No debugging symbols found in concurrency)
(gdb) run
Starting program: /tmp/build/concurrency
warning: Error disabling address space randomization: Operation not permitted
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.32-4.fc33.x86_64
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00007f1aa97896a2 in __memmove_avx_unaligned_erms () from /lib64/libc.so.6
Missing separate debuginfos, use: dnf debuginfo-install libgcc-10.2.1-9.fc33.x86_64 libstdc++-10.2.1-9.fc33.x86_64
(gdb) where
#0  0x00007f1aa97896a2 in __memmove_avx_unaligned_erms () from /lib64/libc.so.6
#1  0x00007f1aa9a92ce0 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned long) () from /lib64/libstdc++.so.6
#2  0x000000000040ee81 in boost::log::v2s_mt_posix::aux::basic_ostringstreambuf<char, std::char_traits<char>, std::allocator<char> >::append(char const*, unsigned long) ()
#3  0x000000000040ed15 in boost::log::v2s_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::formatted_write(char const*, long) ()
#4  0x000000000040eaee in boost::log::v2s_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::operator<<(char const*) ()
#5  0x000000000040e853 in boost::log::v2s_mt_posix::basic_record_ostream<char>::operator<<(char const*) ()
#6  0x000000000040e455 in main ()

知道会发生什么吗?我已经尝试包括 Boost::log_setup 但存在相同的段错误。

@AndreySemashev 的建议帮助我找到了问题所在。显然它与标准库的 GCC ABI 兼容性和版本控制有关。

Conan documentation 很好地涵盖了这个问题(以及如何解决它)。因此,对于我之前的示例,我唯一要做的就是在安装时在 Linux 中设置标准库:

conan install .. -s compiler.libcxx=libstdc++

显然,Conan 假定 GCC 5 的旧标准库(为 Boost 设置的默认版本)。

我希望这对以后的其他人有所帮助。