编译fmt 10及以上版本时出现format.cc编译错误
编译fmt 10+版本时format.cc触发编译器内部段错误的处理方案
编译fmt库10及以上版本(如示例中的11.0.2)时,构建过程中format.cc编译会触发编译器内部段错误,详细编译日志如下:
[05:40:03] : [Step 4/6] [ 4%] 编译CXX目标文件 CMakeFiles/fmt.dir/src/format.cc.o [05:40:03] : [Step 4/6] /root/usr/bin/g++ -Ifmt/11.0.2/include -fPIC -g -fvisibility=hidden -fvisibility-inlines-hidden -MD -MT CMakeFiles/fmt.dir/src/format.cc.o -MF CMakeFiles/fmt.dir/src/format.cc.o.d -o CMakeFiles/fmt.dir/src/format.cc.o -c fmt/11.0.2/src/format.cc [05:40:03]W: [Step 4/6] 包含自 fmt/11.0.2/include/fmt/format.h:41:0 的文件中, [05:40:03]W: [Step 4/6] 来自 fmt/11.0.2/include/fmt/format-inl.h:27, [05:40:03]W: [Step 4/6] 来自 fmt/11.0.2/src/format.cc:8: [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h: 在‘void fmt::v11::detail::check_format_string(S)’的实例化中 [其中 Args = {unsigned int&}; S = fmt::v11::formatter<fmt::v11::detail::bigint>::format(const fmt::v11::detail::bigint&, fmt::v11::format_context&) const::<lambda()>::FMT_COMPILE_STRING; typename std::enable_if<std::is_base_of<fmt::v11::detail::compile_string, S>::value, int>::type <anonymous> = 0]: [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2887:41: 要求自‘fmt::v11::basic_format_string<Char, Args>::basic_format_string(const S&)’ [其中 S = fmt::v11::formatter<fmt::v11::detail::bigint>::format(const fmt::v11::detail::bigint&, fmt::v11::format_context&) const::<lambda()>::FMT_COMPILE_STRING; typename std::enable_if<(std::is_convertible<const S&, fmt::v11::basic_string_view<Char> >::value || (std::is_base_of<fmt::v11::detail::compile_string, S>::value && std::is_constructible<fmt::v11::basic_string_view<Char>, const S&>::value)), int>::type <anonymous> = 0; Char = char; Args = {unsigned int&}]: [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/format-inl.h:1387:60: 要求自此处 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2792:56: 在‘fmt::v11::detail::parse_format_string<true, char, fmt::v11::detail::format_string_checker<char, unsigned int> >(s, fmt::v11::detail::format_string_checker<char, unsigned int>(s))’的constexpr展开中 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2613:44: 在‘fmt::v11::detail::parse_replacement_field<char, fmt::v11::detail::format_string_checker<char, unsigned int>>((p + 18446744073709551615u), end, ((fmt::v11::detail::format_string_checker<char, unsigned int>&)handler))’的constexpr展开中 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2591:13: 在以下内容的constexpr展开中: ‘(& handler)->fmt::v11::detail::format_string_checker<Char, Args>::on_format_specs<char, {unsigned int}>(adapter.fmt::v11::detail::parse_replacement_field(const Char*, const Char*, Handler&&) [其中 Char = char; Handler = fmt::v11::detail::format_string_checker<char, unsigned int>&]::id_adapter::arg_id, (begin + 1u), end)’ [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2763:51: 在‘((fmt::v11::detail::format_string_checker<char, unsigned int>*)this)->fmt::v11::detail::format_string_checker<char, unsigned int>::parse_funcs_[id](((fmt::v11::detail::format_string_checker<char, unsigned int>*)this)->fmt::v11::detail::format_string_checker<char, unsigned int>::context_)’的constexpr展开中 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2677:55: 在‘fmt::v11::formatter<unsigned int, char, void>().fmt::v11::formatter<unsigned int, char, void>::<anonymous>.fmt::v11::detail::native_formatter<T, Char, TYPE>::parse<fmt::v11::detail::compile_parse_context<char> >(ctx)’的constexpr展开中 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2829:34: 在‘fmt::v11::detail::parse_format_specs<char>((& ctx)->fmt::v11::detail::compile_parse_context<char>::<anonymous>.fmt::v11::basic_format_parse_context<Char>::begin<char>(), (& ctx)->fmt::v11::detail::compile_parse_context<char>::<anonymous>.fmt::v11::basic_format_parse_context<Char>::end<char>(), ((fmt::v11::detail::native_formatter<unsigned int, char, (fmt::v11::detail::type)2>*)this)->fmt::v11::detail::native_formatter<unsigned int, char, (fmt::v11::detail::type)2>::specs_, (& ctx)->fmt::v11::detail::compile_parse_context<char>::<anonymous>, (fmt::v11::detail::type)2)’的constexpr展开中 [05:40:03]W: [Step 4/6] fmt/11.0.2/include/fmt/base.h:2792:77: 编译器内部错误:段错误 [05:40:03]W: [Step 4/6] FMT_CONSTEXPR bool error = (parse_format_string<true>(s, checker(s)), true); ^
处理方法
- 升级GCC版本:该错误是GCC在处理fmt库constexpr格式检查代码时的内部bug,升级到GCC 12.2及以上版本可修复此问题。
- 禁用编译期格式检查:若无法升级编译器,在CMake构建时添加编译选项
-DFMT_DISABLE_COMPILE_STRING_CHECKS=ON,跳过编译期格式字符串检查,避免触发崩溃。 - 降级fmt版本:暂时使用fmt 9.x版本,该版本代码结构不会触发此编译器bug。
内容的提问来源于stack exchange,提问作者NIlesh Srivastava
相关产品推荐
相关产品推荐

