C - 编译静态文件

标签 c build compilation shared-libraries

我想使用 GCC 将 C 代码编译成一个单一的可执行文件(包括此文件中的每个依赖项)。
通常我可以用这个命令编译代码:

gcc -o server ex-serv-x509.c  -lgnutls

但是当我尝试用这个 -static 参数编译它时,我收到了这个错误:
[root@localhost test]# gcc -static -o server ex-serv-x509.c  -lgnutls
/usr/bin/ld: cannot find -lc
collect2: ld returned 1 exit status
[root@localhost test]# 

我该如何解决问题?

最好的祝愿

最佳答案

尝试使用 ldd命令以查看它在没有 -static 的情况下链接的内容选项。这是我从一个愚蠢的程序中得到的结果。

~$ gcc so.o -lm -o so
~$ ldd so
    linux-gate.so.1 =>  (0x00db7000)
    libm.so.6 => /lib/libm.so.6 (0x00c7f000)
    libc.so.6 => /lib/libc.so.6 (0x0037f000)
    /lib/ld-linux.so.2 (0x002da000)
~$ gcc so.o -static -lm -o so
~$ ldd so
    not a dynamic executable

因此,如果没有静态,我会自动获得 libc 的共享版本,即使它不应该拥有,这也让我感到惊讶。我想你有共享版本但没有静态版本,所以如果你决定 1986 年是你的年份,你需要从某个地方获取静态库:-)。

确保您可以做到:gcc -print-search-dirs并搜索它们并确保找不到 libc.a。

关于C - 编译静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539187/

相关文章:

c - 没有按照设计的公式获得所需的弹性

objective-c - iOS:通过目标在 .xcconfig 文件中指定代码签名标识

c - 构建 Wireshark 时的错误和警告

ios - Xcode 7 : warning: Ambiguous Content: The launch image set "LaunchImage" has 9 unassigned children

java - 编译错误 : Missing return statement

c - 带有指针的 B 树

c - 将按字母顺序排列的列表拆分为相等的 block

compilation - 如何将 ocaml 编译为 native 代码

c++ - 无法链接到 vtk 6.3/usr/bin/ld : cannot find -lvtkCommon

c - 为什么编译器将字符视为整数?