c++ 代码在 FreeBSD 上没有捕获异常

c++ code doesn't catch exception on FreeBSD

我在许多不同的 linux 发行版上都有一个程序 运行。当我出于某种原因在 FreeBSD 10.1 上编译它时,catch 子句停止工作并且应该捕获的异常使我的程序崩溃。调试时,我将其中一个 catch 子句修改为 "catch (...)",但仍然没有捕获到异常。我猜这个问题与 linker 有关,但我不知道如何进一步调试它。当我尝试编译一个简单地抛出和捕获异常的测试程序时它起作用了 - 所以我猜 linker 无法正确地 link 不同的对象。

有人知道我该如何解决吗? 谢谢

编辑: 编译示例(命令中的原始路径较长,为清楚起见已将其删除):

我编译了很多类这样的:

/usr/local/bin/g++ -O3 -c   -DFreeBSD  -D_FreeBSD  -I. -I/usr/local/openjdk8/include -I/usr/local/openjdk8/include/freebsd -DBOOL_DEFINED -D_BOOL -DFreeBSD -fPIC -I../../../../common/cpp -DVERSION_MAJOR=8 -DVERSION_MIDDLE=2 -DVERSION_MINOR=8 -DNSC_DEBUG -DUSE_HINT_FILES -o CNBCommand.o CNBCommand.cpp

然后用

创建一个存档
ar srv "bin/FreeBSD_10.1-RELEASE/mechanism.a" <many .o files compiled like above>

最终的可执行文件是 link编辑的:

/usr/local/bin/g++ -O3  -B/usr/local/bin -rpath=/usr/local/lib -lstdc++ -lpthread  -o "../bin/FreeBSD_10.1-RELEASE/nbstatus" <many *.o files compiled like above> bin/FreeBSD_10.1-RELEASE/mechanism.a

这是我使用的 g++:

/usr/local/bin/g++ -v
Using built-in specs.
COLLECT_GCC=/usr/local/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.1/4.9.3/lto-wrapper
Target: x86_64-portbld-freebsd10.1
Configured with: ./../gcc-4.9-20141126/configure --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc49 --libexecdir=/usr/local/libexec/gcc49 --program-suffix=49 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gcc49/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc49 --build=x86_64-portbld-freebsd10.1
Thread model: posix
gcc version 4.9.3 20141126 (prerelease) (FreeBSD Ports Collection)

您必须 link 与 -Wl,-rpath=/usr/local/lib/gcc<VERSION> 否则您将 link 反对 libc++,这与 headers gcc 使用不匹配。

检查 pkg info -Dx gcc 以获得正确的路径。