创建静态 Mac OS XC 版本

发布于 2024-10-21 16:19:48 字数 203 浏览 1 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-10-28 16:19:48

Mac OS X 的 gcc 不支持它:

http://discussions.apple.com/message .jspa?messageID=11053384

也许“-static”标志在 MacOS X 上不起作用。并非 gcc 的所有功能都在 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)将是动态的,因此程序将是动态可执行文件。

It is not supported in Mac OS X's 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.

And 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.

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.

许你一世情深 2024-10-28 16:19:48

没有动态加载库的二进制文件无法在 OSX 下构建。我尝试了 apple llvm-gcc 和 macports gcc。然而,到目前为止没有提到的答案是,这是没有必要的。您可以静态链接 c/c++ 库(并使用某些动态部分)。

文件 hello.cpp:

#include <iostream>
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)

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:

#include <iostream>
using namespace std; 
int main()
{
    cout << "Hello World!";
}

Compile as usual:

g++ hello.cpp -o hello

Check linkage:

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)

We can not get rid of the libSystem.B.dylib dependency but with macports gcc we can do this:

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)

Apparently just Apple does not support static linking:

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)
十年九夏 2024-10-28 16:19:48

想象一下,您想要将一些函数转换为库。

文件:example.c

#include <stdio.h>

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 是整个程序的静态构建。

Imagine that you want to convert some functions into a library.

File: example.c

#include <stdio.h>

void aFunction( int a )
{
    printf( "%d\n", a );
}

File: example.h

void aFunction( int a );

File: main.c

#include "example.h"

int main( ) 
{
    aFunction( 3 );

    return 0;
}

To create the library:

gcc -c example.c
ar -r libmylibrary.a  example.o

To link the library:

gcc main.c -lmylibrary -L. -I.

And then the file example.c is a static build of the entire program.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文