问题描述
好的,我知道,这个问题听起来与其他问题相似(都没有回答),但我不知道为什么失败。最简单的例子是这样的:
#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 滚动中测试)。如果我在 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::allocator<char> >::append(char const*,unsigned long) ()
#3 0x000000000040ed15 in boost::log::v2s_mt_posix::basic_formatting_ostream<char,std::allocator<char> >::formatted_write(char const*,long) ()
#4 0x000000000040eaee in boost::log::v2s_mt_posix::basic_formatting_ostream<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 设置的默认版本)。
我希望这能在未来对其他人有所帮助。