我目前正在维护的C++库中进行正确的错误管理。当为某些阴性情况编写单元测试(即测试正确抛出异常)时,单元测试套件简单地用SIGABRT中止。我进行了一次搜索,试图将错误归结为简单的异常,抛出更简单的异常,并尝试各种catch语句。但是,即使是一个无所不包的块也无法防止坠机( MWE见下文)。
我的设置是这样的:我正在使用最新的OSXBigSu11.1和最新的XCode命令行工具来开发Mac。我用的是来自家酿的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)
我也用苹果系统工具链编译了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)
结果仍然相同:异常中止程序。
我最起码的工作例子是:
#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;
}
这可以很好地编译,并在我的Linux上产生预期的输出。
我使用以下命令在我的Mac上编译它:
$ 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
使用LLDB产量:
(lldb) run
Process 61177 launched: './bin/main' (x86_64)
Process 61177 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x00007fff202fa462 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
-> 0x7fff202fa462 <+10>: jae 0x7fff202fa46c ; <+20>
0x7fff202fa464 <+12>: movq %rax, %rdi
0x7fff202fa467 <+15>: jmp 0x7fff202f46a1 ; cerror_nocancel
0x7fff202fa46c <+20>: retq
Target 0: (main) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00007fff202fa462 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00007fff20328610 libsystem_pthread.dylib`pthread_kill + 263
frame #2: 0x00007fff2027b720 libsystem_c.dylib`abort + 120
frame #3: 0x000000010048b00a libgcc_s.1.dylib`uw_init_context_1.cold + 5
frame #4: 0x0000000100488475 libgcc_s.1.dylib`_Unwind_RaiseException + 69
frame #5: 0x00000001001382f7 libstdc++.6.dylib`__cxa_throw + 55
frame #6: 0x0000000100003d55 main`main + 52
frame #7: 0x00007fff20343621 libdyld.dylib`start + 1
在我看来,似乎另一个错误发生在展开阶段,然后导致终止。这也解释了为什么从来没有到达catch块。
这超出了我的专业范围,所以任何想法都是受欢迎的。
编辑:最新问题后GCC自制版。
发布于 2021-01-11 01:25:02
我证实了意外行为的大苏尔,自制GCC 10.2.0_2设置。将链接的brew libstdc++改为system (假设/usr/lib
中找到的brew是由macOS安装的)解决了我的设置上的问题:
$ 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
或者,在运行export DYLD_LIBRARY_PATH=/usr/lib
之前设置main
具有相同的效果。
更新:,bug是固定,该补丁包含在brew的gcc-10.2.0_3中。
https://stackoverflow.com/questions/65323946
复制相似问题