创建静态 Mac OS XC 版本
如何在 Mac OS X 上创建 .c 文件的静态构建?当我尝试:
gcc -o test Main.c -static
我得到:
ld: library not found for -lcrt0.o
collect2: ld returned 1 exit status
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 found for -lcrt0.o
collect2: ld returned 1 exit status
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Mac OS X 的 gcc 不支持它:
http://discussions.apple.com/message .jspa?messageID=11053384
和 http://developer.apple.com/library/mac/# qa/qa2001/qa1118.html
更新:禁止的是静态二进制文件。但是您仍然可以编译一些静态库并将其与另一个程序一起使用。程序将与您的库静态链接,但其他库(如 libc)将是动态的,因此程序将是动态可执行文件。
It is not supported in Mac OS X's gcc:
http://discussions.apple.com/message.jspa?messageID=11053384
And http://developer.apple.com/library/mac/#qa/qa2001/qa1118.html
Update: The prohibited is a static binary. But you still can compile some static library and use it with you another program. Program will be linked statically with your library, but other libraries like libc will be dynamic, so program will be a dynamic executable.
没有动态加载库的二进制文件无法在 OSX 下构建。我尝试了 apple llvm-gcc 和 macports gcc。然而,到目前为止没有提到的答案是,这是没有必要的。您可以静态链接 c/c++ 库(并使用某些动态部分)。
文件 hello.cpp:
照常编译:
检查链接:
我们无法摆脱 libSystem.B.dylib 依赖项,但使用 macports gcc 我们可以做到这一点:
显然只是 Apple 不支持静态链接:
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:
Compile as usual:
Check linkage:
We can not get rid of the libSystem.B.dylib dependency but with macports gcc we can do this:
Apparently just Apple does not support static linking:
想象一下,您想要将一些函数转换为库。
文件:example.c
文件:example.h
文件:main.c
创建库:
链接库:
然后文件 example.c 是整个程序的静态构建。
Imagine that you want to convert some functions into a library.
File: example.c
File: example.h
File: main.c
To create the library:
To link the library:
And then the file example.c is a static build of the entire program.