C语言 创建静态 Mac OS XC 构建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5259249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:01:19  来源:igfitidea点击:

Creating static Mac OS X C build

cmacosgccstatic

提问by Daniel

How can i create a static build of a .c file on Mac OS X ? When i try:

如何在 Mac OS X 上创建 .c 文件的静态版本?当我尝试:

gcc -o test Main.c -static

I get:

我得到:

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

采纳答案by osgx

It is not supported in Mac OS X's gcc:

Mac OS X 的 gcc 不支持它:

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

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.

也许那个“-static”标志在 MacOS X 上无法正常工作。并非 gcc 的所有功能都在 MacOS X 上实现。Apple 甚至不会在未来版本的操作系统中使用 gcc。

我不知道如何使用“-static”进行链接。我想不出有什么理由在 MacOSX 上这样做。如果我知道您为什么要使用“-static”,我可能会对这个问题更感兴趣。现在,我只是不明白。通过寻求帮助,您实际上是在寻求项目的合作者——即使只有 10 分钟。你需要引起我的兴趣。

And http://developer.apple.com/library/mac/#qa/qa2001/qa1118.html

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.

Mac OS X 不支持用户二进制文件的静态链接。将用户二进制文件绑定到 Mac OS X 库和接口的内部实现会限制我们更新和增强 Mac OS X 的能力。相反,支持动态链接(针对 crt1.x 的链接)。 o 自动而不是寻找 crt0.o,例如)。

我们强烈建议您非常仔细地考虑静态链接的局限性,并考虑您的客户及其需求,以及您需要提供的长期支持。

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.

更新:被禁止的是静态二进制文件。但是您仍然可以编译一些静态库并将其与另一个程序一起使用。程序将与您的库静态链接,但其他库(如 libc)将是动态的,因此程序将是动态可执行文件。

回答by gbasler

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

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

File hello.cpp:

文件 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:

我们无法摆脱 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)

Apparently just Apple does not support static linking:

显然只有 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)

回答by momboco

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

想象一下,你想把一些函数转换成一个库。

File: example.c

文件:example.c

#include <stdio.h>

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

File: example.h

文件:example.h

void aFunction( int a );

File: main.c

文件: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.

然后文件 example.c 是整个程序的静态构建。