简体   繁体   English

macOS将libstdc ++与g ++ 4.7.2静态链接

[英]macos statically linking libstdc++ with g++ 4.7.2

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. 由于各种原因,我需要将libstdc ++链接到我的可执行文件中,因此它没有额外的依赖性。 Using g++'s -static-libstdc++ and -static-libgcc flags I was able to achieve this, however, no exceptions were being caught. 使用g ++的-static-libstdc ++和-static-libgcc标志可以实现这一点,但是,没有异常被捕获。

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. 当我以32模式而不是64位编译时,代码似乎可以正常工作。 I do not understand why the exception is not being caught and its rather frustrating. 我不明白为什么未捕获到异常并且它令人沮丧。

Setup 设定

  • Macos 10.7 64 bit Macos 10.7 64位
  • G++ 4.7.2 G ++ 4.7.2

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 32位模式

$ 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 64位模式

$ 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: 我尝试了多种方法来解决此问题,包括:

  • Linking directly with the static libraries ie /usr/local/lib/libstdc++.a 直接与静态库(即/usr/local/lib/libstdc++.a)链接
  • Using -Wl,-bstatic -lstdc++ -lgcc_eh -bdynamic 使用-Wl,-bstatic -lstdc ++ -lgcc_eh -bdynamic

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. 我知道mac在4.2.1版本停止了对g ++的支持,因此最好改用clang并希望该二进制文件仍适用于不同版本的OSX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM