如何将 gcc 命令与“-static”一起使用

How to use gcc command with '-static'

当我尝试使用gcc命令编译带有静态库的测试程序时't1',我自己存档了。

我用来归档静态库的命令是这样的:

ar rcv libt1.a t1.o

使用以下命令出现错误:

gcc -L. -static -lt1 t.c -o t

ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,如果我像这样删除“-static”:

gcc -L. -lt1 t.c -o t

编译成功。我得到了正确的结果。

但我搜索了很多,发现其他人使用“-static”,他们没有得到错误。所以,请帮我算一下。

ar rcv libt1.a t1.c

打字错误?您通常不需要库中的 C 源文件.....

众所周知,Clang 有点挑剔,尤其是对于静态库(这是在 MacOS 上吗,有机会吗?)并且会尝试 link 到一个不存在的运行时库 - 你可以变通这通过将静态库作为 目标文件 而不是像

这样的 移交
gcc t.o libt1.a -o t

这应该有效。

如评论中所述,-static 不需要 link 针对您的静态库,但用于指示编译器 link 静态针对 C (和 C++,如果是 g++)运行时库。这 is not supported on OS X (link taken from the linked answer 以上),因此错误。

要link针对你的静态库,就像你在第二个命令行中做的那样,不指定-static

But I search a lot, find out other guys use '-static', and they do not get an error. So, please help me figure that.

他们可能正在 Linux 或 Windows 上工作,其中支持针对 libc 的静态 linking(尽管即使在 Linux 上它也带来了相当多的问题).