LIBRARY_PATH handling differences with GCC(?)


I ran into an issue linking a library with a specific libpng today.

The cmake build system found the desired library and set PNG_LIBRARIES="/opt/local/lib/;/opt/local/lib/" . However, my build script also sets LIBRARY_PATH=/opt/local/lib so that I don't have to add -L/opt/local/lib to each and every linker call.

Because of this, cmake rewrites the PNG_LIBRARIES value to "-lpng -lz" when I use it in a link command.

That works fine with GCC but for some reason not with clang (4.0.1, installed via the Ubuntu .deb from With clang I get errors about missing symbols, which indicate that the system libpng is being pulled in.

I did a very simple test comparing

%> env LIBRARY_PATH=/opt/local/lib g++ -v test.cpp -lpng
%> env LIBRARY_PATH=/opt/local/lib clang++ -v test.cpp -lpng

With g++ (7.1.0) the resulting a.out is linked to /opt/local/lib/ while with clang++ it links to Ubuntu's . Comparing the actual link commands I notice that g++ invokes collect2 with -L/opt/local/lib BEFORE the first -lfoo library spec, while clang++ invokes /usr/bin/ld with -L/opt/local/lib set AFTER the user-specified -lfoo library specs.

Is there a reason for that, or is this a bug?