如何在 Mac OS X 上创建 .c 文件的静态构建?当我尝试时:
gcc -o test Main.c -static
我得到:
ld: library not found for -lcrt0.o
collect2: ld returned 1 exit status
最佳答案
Mac OS X 的 gcc 不支持它:
http://discussions.apple.com/message.jspa?messageID=11053384
Perhaps that "-static" flag flat out won't work on MacOS X. Not all features of gcc are implemented on MacOS X. Apple won't even be using gcc in future versions of the OS.
I don't know how to link using "-static". I can't think of any reason to do so on MacOSX. If I knew why you wanted to use "-static" I might be more interested in the problem. Right now, I just don't get it. By asking for help, you are essentially asking for collaborators on the project - even if it is only for 10 minutes. You need to get me interested.
和http://developer.apple.com/library/mac/#qa/qa2001/qa1118.html
Static linking of user binaries is not supported on Mac OS X. Tying user binaries to the internal implementation of Mac OS X libraries and interfaces would limit our ability to update and enhance Mac OS X. Instead, dynamic linking is supported (linking against crt1.o automatically instead of looking for crt0.o, for example).
We strongly recommend that you consider the limitations of statically linking very carefully, and consider your customer and their needs, plus the long-term support you will need to provide.
更新:禁止的是静态二进制文件。但是您仍然可以编译一些静态库并将其与另一个程序一起使用。程序将与您的库静态链接,但其他库(如 libc)将是动态的,因此程序将是动态可执行文件。
关于创建静态 Mac OS X C 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5259249/