SIGABRT 在 Mac OS X 上使用 GCC 抛出和捕获异常

lep*_*589 5 c++ macos homebrew gcc

我目前正在维护的 C++ 库中进行正确的错误管理。当为一些负面情况编写单元测试时(即测试正确抛出异常),单元测试套件只是用 SIGABRT 中止了。我继续寻找并尝试通过抛出更简单的异常并尝试使用各种 catch 语句来归结错误。但即使是一个包罗万象的块也无法阻止崩溃(对于 MWE,见下文)。

我的设置是这样的:我在 Mac 上使用最新的 OS X Big Sur 11.1,并安装了最新的 XCode 命令行工具。我正在使用 Homebrew 的 GCC,目前是 v10.2.0_1。

$ g++-10 -v
Using built-in specs.
COLLECT_GCC=g++-10
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/10.2.0_1/libexec/gcc/x86_64-apple-darwin20/10.2.0/lto-wrapper
Target: x86_64-apple-darwin20
Configured with: ../configure --build=x86_64-apple-darwin20 --prefix=/usr/local/Cellar/gcc/10.2.0_1 --libdir=/usr/local/Cellar/gcc/10.2.0_1/lib/gcc/10 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-10 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 10.2.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk SED=/usr/bin/sed
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (Homebrew GCC 10.2.0_1)
Run Code Online (Sandbox Code Playgroud)

我还使用 Apples 系统工具链自己编译了 GCC。自编译 GCC 的输出是这样的:

$ /opt/gcc/10.2.0/bin/g++-10 -v
Using built-in specs.
COLLECT_GCC=/opt/gcc/10.2.0/bin/g++-10
COLLECT_LTO_WRAPPER=/opt/gcc/10.2.0/libexec/gcc/x86_64-apple-darwin20/10.2.0/lto-wrapper
Target: x86_64-apple-darwin20
Configured with: ../configure --build=x86_64-apple-darwin20 --prefix=/opt/gcc/10.2.0 --libdir=/opt/gcc/10.2.0/lib/gcc/10 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-10 --with-system-zlib --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk SED=/usr/bin/sed
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)
Run Code Online (Sandbox Code Playgroud)

结果还是一样:异常中止程序。

我的最低工作示例是这样的:

#include <iostream>
#include <stdexcept>

int main()
{
    try {
        throw "this is an exception text";
    }
    catch(const char* e)
    {
        std::cerr << e << std::endl;
    }
    catch(...)
    {
        std::cerr << "Unknown error!" << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这可以很好地编译并在我的 Linux VM 上产生预期的输出。

我正在使用以下命令在我的 Mac 上编译它:

#include <iostream>
#include <stdexcept>

int main()
{
    try {
        throw "this is an exception text";
    }
    catch(const char* e)
    {
        std::cerr << e << std::endl;
    }
    catch(...)
    {
        std::cerr << "Unknown error!" << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用 LLDB 收益:

$ g++-10 -o bin/main.o -c -std=c++11 main.cpp
$ g++-10 -o bin/main bin/main.o
$ ./bin/main
[1]    60310 abort      ./bin/main
Run Code Online (Sandbox Code Playgroud)

在我看来,好像在展开阶段发生了另一个错误,然后导致终止。这也可以解释为什么没有到达 catch 块。

这超出了我的专业领域,因此欢迎提出任何想法。

编辑:在最新的 GCC Homebrew 版本之后更新了问题。

mlu*_*und 1

我确认了 Big Sur、Homebrew GCC 10.2.0_2 设置中的意外行为。将链接的brew libstdc++更改为系统一(假设在/usr/libmacOS中安装的)解决了我的设置问题:

$ g++-10 main.cpp -o main -std=c++11
$ ./main
Abort trap: 6
$ otool -L main
main:
    /usr/local/opt/gcc/lib/gcc/10/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.28.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
    /usr/local/lib/gcc/10/libgcc_s.1.dylib (compatibility version 1.0.0, current version 1.0.0)
$ install_name_tool -change /usr/local/opt/gcc/lib/gcc/10/libstdc++.6.dylib /usr/lib/libstdc++.6.dylib main
$ ./main
this is an exception text
Run Code Online (Sandbox Code Playgroud)

或者,export DYLD_LIBRARY_PATH=/usr/lib在运行前设置main也具有相同的效果。

更新:该错误已修复,补丁包含在brew的gcc-10.2.0_3中。