[cmake-developers] Wrong linker flags generation when the library filename is not prefixed with "lib"

Brad King brad.king at kitware.com
Mon Dec 2 09:48:15 EST 2013


On 11/28/2013 08:58 AM, Samuel Martin wrote:
> When I try to link against it, "find_libray" correctly found it:
> ---
> CMakeCache.txt:
> FOO_LIBRARIES:INTERNAL=general;/usr/lib/foo.so
> ---
> 
> But the generated linker flags are wrong:
> ---
> link.txt:
> /usr/bin/c++   -g    CMakeFiles/bar.dir/main.cpp.o  -o bin/bar -rdynamic -lfoo

Unfortunately we currently convert libraries in implicit directories
(like /usr/lib) to the "-lx" form because some toolchains substitute
per-architecture alternatives under the hood so "/usr/lib/libx.a"
actually needs to be "/usr/lib/$arch/libx.a".  I've posted similar
explanations on the CMake mailing lists a few times before.  It cannot
be fixed without teaching find_library how to parse binaries of all
forms on all platforms to detect compatible architectures.  That is
very difficult.

To force CMake to use a given path, create an imported target and
set the location to the desired path.  For example:

 add_library(foo SHARED IMPORTED)
 set_property(TARGET foo IMPORTED_LOCATION /usr/lib/foo.so)
 target_link_libraries(bar foo)

-Brad



More information about the cmake-developers mailing list