autotools and latest gcc version

Running an old autotools build I just found it didn’t work no more. The command in error was:
# gcc -lpthread -o foo foo.o

Manually putting -lpthread at the end was ok but as I was using autotools I couldn’t just change the order and – above all – it was fine for gcc to expect the library *after* the object file!

The solution – suggested on #gcc by ngladitz – was to use the right autotools variable for adding libraries.

After RTFM I just:
– foo_LDFLAGS=-lpthread # autotools put flags before …
+ foo_LDADD=-lpthread # … and libraries after

cleaning up and rebuilding fixed everything!

Mixing static and dynamic linking

Due to some limitation of profiling software, it may be useful to link statically some libs into our programs. The following command links statically only lcrypto – while using dynamic linking on all other libs.

# gcc test.c -o test -Wl,-Bstatic -lcrypto -Wl,-Bdynamic

If you forget the ending -Wl,-Bdynamic you’ll get the following error:

 /usr/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status

This because gcc tries to link statically libgcc too – but using the wrong file: libgcc_s.a. To link statically libgcc you have to add the -static-libgcc flag

# gcc test.c -o test -Wl,-Bstatic -lcrypto  -static-libgcc