1

I downloaded the source code of wget and tried a static compilation. These are the steps that I followed :

./configure LDFLAGS=-static

At the end the output shown is as follows :

  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

Then I used make . This throws a long list of errors. Here is an excerpt :

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

Has anyone faced a similar issue? If so please post a solution. Thanks in advance

7
  • 1
    Are you running the binary in the same machine you compiled it? Commented Nov 17, 2015 at 9:22
  • 1
    For undefined reference to pthread_mutex_lock', add -lpthread as an option and try again.
    – schaiba
    Commented Nov 17, 2015 at 9:23
  • Running ldd on the libc file will list the needed libraries Commented Nov 17, 2015 at 9:35
  • 1
    Per Thomas' comment, see what libs are needed are check the -l* options to add, just like you did with pthread.
    – schaiba
    Commented Nov 17, 2015 at 10:06
  • 1
    -lgnutls for asn1_der_coding .
    – schaiba
    Commented Nov 17, 2015 at 11:45

1 Answer 1

2

For static linking, ld will not do the job of automatic searching for dependencies for wget like it magically does for shared objects. Some scripts try to make such a list for you, others not. You hit the latter case.

You need to make the list of libraries and their dependencies yourself.

If you don't yet know what is complete list of deps for your static wget binary, you need first to build it as normal (shared) and then obtain such a list with ldd /path/to/wget, where /path/to/wget is your wget built binary (it can be found inside the source tree, usually something like src/wget)

You will need to obtain all the static versions of shared libraries. They usually contained in -dev or devel packages your distribution provides.

Each library in list must be rearranged or appended twice or more times because ld does not try to search all static archives for specific symbol and errors out. So, your libgnutls.a probably depends on libtasn1.a for asn1_* symbols. Then you need to append this to command line: -lgnutls -ltasn1. If one more library then depends on libtasn1.a, you'll need to append it again to the link command line.

Undefined symbols can be resolved by looking into symbol list of static library with nm /usr/lib/lib.a. Or finding that library in certain directory with fgrep -l symbol_name /usr/lib/*.a. Note such a search (with fgrep) will show both libraries that require and provide such a symbol, so it's only a quick test.

Libraries can be appended with LIBS= variable to configure script: LIBS="-lgnutls -ltasn1".

1
  • I might add that some of the -dev and -devel packages provided by the distributions sneakily leave out the static versions of the libraries, even if the source scripts explicitly include it. This is especially prevalent with RPM packages distributed for CentOS and Fedora. In many cases you can rebuild them by obtaining the appropriate .src.rpm packages of the libraries you need, in other cases you need to build the static versions manually.
    – CoolKoon
    Commented Aug 25, 2020 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .