创建静态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
Mac OS X的gcc不支持它:
http://discussions.apple.com/message.jspa?messageID=11053384
也许那些“静止”的标志在MacOS X上不起作用。并非所有的gccfunction都在MacOS X上实现。在未来的操作系统版本中,Apple甚至不会使用gcc。
我不知道如何使用“-static”进行链接。 我想不出有任何理由在MacOSX上这样做。 如果我知道你为什么要使用“-static”,我可能会对这个问题更感兴趣。 现在,我只是不明白。 通过寻求帮助,你基本上是要求项目的合作者 – 即使它只有10分钟。 你需要让我感兴趣。
和http://developer.apple.com/library/mac/#qa/qa2001/qa1118.html
Mac OS X不支持静态链接用户二进制文件。将用户二进制文件绑定到Mac OS X库和接口的内部实现会限制我们更新和增强Mac OS X的能力。相反,支持动态链接(链接crt1。 o自动而不是寻找crt0.o,例如)。
我们强烈建议您仔细考虑静态链接的限制,并考虑您的客户及其需求,以及您需要提供的长期支持。
更新:禁止是静态二进制文件。 但是你仍然可以编译一些静态库并将它与你的另一个程序一起使用。 程序将与您的库静态链接,但libc等其他库将是动态的,因此程序将是动态可执行文件。
没有动态加载库的二进制文件无法在OSX下构建。 我尝试了苹果llvm-gcc和macports gcc。 然而到目前为止没有回答的是这不是必需的。 您可以静态链接c / c ++库(并与一些动态部分一起使用)。
文件hello.cpp:
#include using namespace std; int main() { cout << "Hello World!"; }
像往常一样编译:
g++ 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)
我们无法摆脱libSystem.B.dylib依赖,但使用macports gcc,我们可以这样做:
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)
显然只是Apple不支持静态链接:
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)
想象一下,您想将某些function转换为库。
文件:example.c
#include void aFunction( int a ) { printf( "%d\n", a ); }
文件:example.h
void aFunction( int a );
文件:main.c
#include "example.h" int main( ) { aFunction( 3 ); return 0; }
要创建库:
gcc -c example.c ar -r libmylibrary.a example.o
要链接库:
gcc main.c -lmylibrary -L. -I.
然后文件example.c是整个程序的静态构建。