Q:

Building wget with clang/llvm

I downloaded the source files for wget and tried to compile it with Clang.  I want to configure with the link time optimization (-flto).  At first, I simply tried:
./configure CC=clang CFLAGS=-flto
make

The configuration was successful, but I could not run make which output: 

/tmp/lto-llvm-4f0f02.o:ld-temp.o:function _do_handshake: error: undefined reference to 'rpl_fcntl'

and other numerous similar errors, all of the same format (error: undefined reference to xxx). 
So I thought I needed to link the files correctly.  After searching for answers, I then tried all of the following commands:
./configure CC=clang CFLAGS=-flto LDFLAGS=-flto
make
-----------------------------------------------
./configure CC=clang CFLAGS=-flto LDFLAGS="-fuse-ld=lld-4.0"
make
-----------------------------------------------
./configure CC=clang CFLAGS=-flto LDFLAGS="-fuse-ld=gold"
make

But I am still getting the same error.  
I am running this on Debian-9.5.  What am I missing? 

A:

You need to also specify the specific ar utility to use - the default ar and ranlib produce .a files that are unusable for linking (no entries to use), so for me this worked:
./configure CC=clang-4.0 CFLAGS=-flto AR=llvm-ar-4.0 RANLIB=llvm-ranlib-4.0

You may get away with just llvm-ar, without the version postfix, but you need to ensure that the clang, ar, and ranlib versions match, otherwise you encounter other errors.