我有這個示例代碼:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(RHBuildTest CXX)
message(STATUS "C Compiler: ${CMAKE_CXX_COMPILER}")
add_executable(script1 script1.cpp)
set_target_properties(script1 PROPERTIES COMPILE_FLAGS "-flto")
// script1.cpp
#include <string>
#include <iostream>
int main()
{
const std::string msg = "this is a string";
std::cout << "msg.size(): " << msg.size() << "\n";
std::cout << "msg: " << msg << "\n";
std::cout << "msg.substr(0): " << msg.substr(0) << "\n";
return 0;
}
我們現在g 10.2.0在 RHEL 7 和 RHEL 8 上進行編譯,但 RHEL 8 提供了segault。如果我們-flto去掉 ,那么 RHEL 8 運行得很好。這是 ABI 問題嗎?我是否需要設定某些路徑以便加載正確的標準庫(使用時-flto)?什么可能導致這個問題?
RHEL 7:
[~/code/rh_build_test/build_rh7]$ cat /etc/redhat-release; cmake ..; make; ./script1
Red Hat Enterprise Linux Server release 7.7 (Maipo)
-- C Compiler: /app/.../el7.3.10/x86_64-gcc10.2.x/gcc-10.2.0/bin/g
-- Configuring done
-- Generating done
-- Build files have been written to: ~/code/rh_build_test/build_rh7
[ 50%] Building CXX object CMakeFiles/script1.dir/script1.cpp.o
[100%] Linking CXX executable script1
[100%] Built target script1
msg.size(): 16
msg: this is a string
msg.substr(0): this is a string
RHEL 8:
[~/code/rh_build_test/build_rh8]$ cat /etc/redhat-release; cmake ..; make; ./script1
Red Hat Enterprise Linux release 8.4 (Ootpa)
-- C Compiler: /app/.../el8_4.4.18/x86_64-gcc10.2.x/gcc-10.2.0/bin/g
-- Configuring done
-- Generating done
-- Build files have been written to: ~/code/rh_build_test/build_rh8
[ 50%] Building CXX object CMakeFiles/script1.dir/script1.cpp.o
[100%] Linking CXX executable script1
[100%] Built target script1
Segmentation fault (core dumped)
有時 RHEL 8 會列印更多,但它總是在substr以下位置失敗:
...
[100%] Built target script1
msg.size(): 16
msg: this is a string
Segmentation fault (core dumped)
uj5u.com熱心網友回復:
這是 RHEL 中的一個已知錯誤:當 -flto 用于在 RHEL 8.4 上編譯 Catch 框架測驗時出現段錯誤
要確認這是您遇到的相同錯誤,請查看暫時將 binutils 降級到 2.30-79.el8 是否可以使其正常作業。如果是這樣,那么看起來它會在 RHEL 8.5 發布時得到正確修復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/350583.html
