3

I am fairly unfamiliar with static linking in any language, but I'm trying to statically link a Haskell program for usage on other macs. I've managed to statically link on Linux and confirm it to be working on other Linux boxes by using these options: -O2 -static -optl-static -optl-pthread

When I try to use those options on OS X, I get the following error:

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

Any idea what is up? Am I doing something completely nuts?

Rayne
  • 31,473
  • 17
  • 86
  • 101
  • 3
    Is there something specific that makes you think you need to statically link, just to run your executable on a different computer? Typically OS X executables dynamically link against the system-provided libraries; static linking is far less common than on Linux. You can run `otool -Lv` on your executable to see what libraries it dynamically loads. If they're all in `/usr/lib` or `/System/Frameworks` and they're not things you installed, then you're probably already OK. – Kurt Revis Feb 24 '13 at 07:25
  • I didn't know that. It appears all of the libraries it is linked against are in /usr/lib. Excellent. I guess I should keep this open because it probably is still a valid question, but I'll update the question. – Rayne Feb 24 '13 at 07:32
  • 3
    Instead of "far less common" I should have said "it's essentially never done". [More background in this answer](http://stackoverflow.com/questions/844819/how-to-static-link-on-os-x) and in [Apple QA 1118](https://developer.apple.com/library/mac/#qa/qa2001/qa1118.html). – Kurt Revis Feb 24 '13 at 07:45
  • I see. I'm not sure what to do with this question though. Your comments seem like they'd be an acceptable answer. If you want to form that up into one I'll accept it (because it does indeed answer my question). – Rayne Feb 24 '13 at 07:55
  • @KurtRevis I agree that your comments are the correct answer to this question. – sclv Feb 28 '13 at 19:19
  • I always assumed that Haskell binaries on OS X were always statically compiled based on their size (easily up to 100MB). There a tool that [copies all dylib deps](https://github.com/atzedijkstra/macosx-make-standalone) into the application you have created, but this requires your program to be an application bundle. – Alessandro Vermeulen Feb 28 '13 at 20:59

1 Answers1

1

Community-wiki answer for posterity, derived from the comments:

Typically OS X executables dynamically link against the system-provided libraries; static linking is far less common than on Linux. You can run otool -Lv on your executable to see what libraries it dynamically loads. If they're all in /usr/lib or /System/Frameworks and they're not things you installed, then you're probably already OK.

In fact, as per this answer, static linking of mac system libraries is simply disallowed.

Community
  • 1
  • 1
sclv
  • 38,665
  • 7
  • 99
  • 204