wget的静态编译

wget的静态编译

我下载了wget的源代码并尝试了静态编译。这些是我遵循的步骤:

./configure LDFLAGS=-static

最后显示的输出如下:

  Version:           1.17
  Host OS:           linux-gnu
  Install prefix:    /usr/local
  Compiler:          gcc
  CFlags:              -I/usr/include/p11-kit-1   -DHAVE_LIBGNUTLS   -DNDEBUG  
  LDFlags:           -static
  Libs:              -lpcre   -lgnutls   -lz   
  SSL:               gnutls
  Zlib:              yes
  PSL:               no
  Digest:            yes
  NTLM:              auto
  OPIE:              yes
  Debugging:         yes
  Assertions:        no
  Valgrind:          Valgrind testing not enabled
  Metalink:          no
  GPGME:             no

然后我用了make.这会引发一长串错误。这是摘录:

init.o: In function `home_dir':
init.c:(.text+0x2bc): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
host.o: In function `getaddrinfo_with_timeout_callback':
host.c:(.text+0x495): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libgnutls.a(gnutls_mpi.o): In function `_gnutls_x509_read_int':
(.text+0x6af): undefined reference to `asn1_read_value'
/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/libgnutls.a(gnutls_mpi.o): In function `_gnutls_x509_read_int':
.
.
(.text+0x1a7a): undefined reference to `pthread_mutex_lock'
/usr/lib/gcc/i686-linux-gnu/4.8/libgcc_eh.a(unwind-dw2-fde-dip.o): In function `_Unwind_Find_FDE':
(.text+0x1ac9): undefined reference to `pthread_mutex_unlock'
collect2: error: ld returned 1 exit status
make[3]: *** [wget] Error 1
make[2]: *** [all] Error 2
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

有人遇到过类似的问题吗?如果是这样,请发布解决方案。提前致谢

答案1

对于静态链接,ld不会像wget对共享对象那样神奇地自动搜索依赖项。有些脚本会尝试为您创建这样的列表,有些则不会。你遇到了后一种情况。

您需要自己创建库及其依赖项的列表。

如果您还不知道静态wget二进制文件的完整 deps 列表是什么,您需要首先正常构建它(共享),然后使用 获取这样的列表ldd /path/to/wget/path/to/wget您的 wget 构建的二进制文件在哪里(可以在源树,通常类似于src/wget

您将需要获取共享库的所有静态版本。它们通常包含在您的发行版提供的 -dev 或 devel 包中。

列表中的每个库都必须重新排列或附加两次或更多次,因为ld不会尝试在所有静态档案中搜索特定符号和错误。因此,您的 libgnutls.a 可能依赖于 libtasn1.a 的 asn1_* 符号。然后您需要将其附加到命令行:-lgnutls -ltasn1。如果还有一个库依赖于 libtasn1.a,您需要将其再次附加到链接命令行。

未定义的符号可以通过使用 . 查看静态库的符号列表来解决nm /usr/lib/lib.a。或者在某个目录中使用fgrep -l symbol_name /usr/lib/*.a.请注意,这样的搜索(使用 fgrep)将显示需要并提供此类符号的两个库,因此这只是一个快速测试。

LIBS=库可以将变量附加到configure脚本中:LIBS="-lgnutls -ltasn1"

相关内容