Creating static Mac OS X C build

后端 未结 3 738
遥遥无期
遥遥无期 2020-11-28 07:51

How can i create a static build of a .c file on Mac OS X ? When i try:

gcc -o test Main.c -static

I get:

ld: library not fo         


        
3条回答
  •  醉话见心
    2020-11-28 08:02

    A binary that has no dynamic loaded libraries can not be built under OSX. I tried both apple llvm-gcc and macports gcc. However what no answer mentioned so far is that this is not needed. You can link the c/c++ library statically (and live with some dynamic part).

    File hello.cpp:

    #include 
    using namespace std; 
    int main()
    {
        cout << "Hello World!";
    }
    

    Compile as usual:

    g++ hello.cpp -o hello
    

    Check linkage:

    otool -L hello
    hello:
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    

    We can not get rid of the libSystem.B.dylib dependency but with macports gcc we can do this:

    g++-mp-4.6 -static-libgcc -static-libstdc++ hello.cpp -o hello
    
    otool -L hello
    hello:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    

    Apparently just Apple does not support static linking:

    llvm-g++ -static-libgcc -static-libstdc++ hello.cpp -o hello
    
    otool -L hello
    hello:
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    

提交回复
热议问题