Search code examples
cmacoslinkerstatic-linking

statically linked libraries on mac


I just don't understand.

I'm to believe that gcc automatically links libc.a when invoking the gcc driver.

However, out of curiosity I wanted to link libc.a statically using -static, but I get;

ld: library not found for -lcrt0.o
collect2: ld returned 1 exit status

I just don't understand? Also, whats crt0?

I've also read posts saying to never statically link libc.a ; why? From what I've read (admittedly I'm just an undergrad), I don't see the drawbacks in linking statically..

I read this post which told me to add -lc which seems to compile correctly, but is this statically linking or just adding libc.dylib ?

Additionally, anytime I use -static flag, I get the same error returned from ld. I don't understand? The text I'm working through shows examples using a Unix environment, and since OS X is Unix-based, why am I having so many difficulties with my Mac?

Thanks, Zak


Solution

  • This is one of Apple's decisions about how libraries and development should work. They dissuade you from building static binaries by making it as difficult as possible.

    There's some weak-sauce explanation available here on http://developer.apple.com/, the useful tidbit is:

    >     If your project absolutely must create a statically linked binary, 
    > you can get the Csu (C startup) module from [Darwin][2] and try
    > building crt0.o for yourself. Obviously, we won't support such an
    > endeavor.
    

    If you feel like you're swimming upstream, get used to it - because it won't get any easier with app sandboxing, signing, dyld changes, etc etc.

    On the plus side, trying to do unix stuff on OSX is edgy and dangerous now.

    A couple useful links for down the road:

    http://reverse.put.as/2013/03/20/how-to-compile-gdb-in-mountain-lion-updated/ http://www.osxbook.com/