I've been looking at this issue for a number of weeks now with no joy so its time to ask for the wisdom of stack overflow...
For various reasons I need to link libstdc++ into my executable so it has no extra dependencies. Using g++'s -static-libstdc++ and -static-libgcc flags I was able to achieve this, however, no exceptions were being caught.
I produced the following test code to investigate the problem further. It seems the code works when I compile in 32 mode but not in 64 bit. I do not understand why the exception is not being caught and its rather frustrating.
Setup
The proram
#include <cstdio>
#include <stdexcept>
void myMethod() {
throw std::invalid_argument("foo");
}
int main () {
try {
myMethod();
} catch (const std::invalid_argument& ex) {
printf("caught: %s\n", ex.what());
} catch (...) {
printf("caught it\n");
}
return 0;
}
32 bit mode
$ g++ -m32 -o main Main.cpp -static-libgcc -static-libstdc++ && otool -L ./main && ./main
./main:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
caught: foo
64 bit mode
$ g++ -o main Main.cpp -static-libgcc -static-libstdc++ && otool -L ./main && ./main
./main:
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
Abort trap: 6
I have tried many different methods to try to solve this problem including:
But no avail.
Is there some part of the compiler configuration I need to check? A flag I'm missing?
I know mac stopped supporting g++ at version 4.2.1 so it might be better to move over to using clang and hoping the binary still works on different versions of OSX.
I think exceptions are a bit broken with GCC on Mac OS X:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45486
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42159
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.