[CMake] How do you handle recursive dependencies in CMake

Baars, S. s.baars at rug.nl
Tue Jun 7 16:07:13 EDT 2016


Hey everyone,

I posted a question on StackOverflow a while ago, but got no real answer
to my question there. I posted an answer there by myself, but this
solution does not work on every system. Since it also involves some
possible inconsistencies in the CMake documentation, I decided to ask
the question here as well.

Say I have packages A, B, and C. Package B uses package A and package C
uses package B. I create shared libraries.

So in package B I do something like

    find_package(A)
    ...
    if(${A_FOUND})
    target_link_libraries(B ${A_LIBRARIES})
    endif()

and in package C I do

    find_package(B)
    ...
    if(${B_FOUND})
    target_link_libraries(C ${B_LIBRARIES})
    endif()

    add_executable(main main.cpp)
    target_link_libraries(main C)

where ${B_LIBRARIES} contains only B. The compiler will now complain

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

as long as A is installed in a place that is not in the default
directories. I was wondering what is the correct way of handling this.
For me using find_package(A) in C (which would work) doesn't seem to be
the nice. For me especially, because I don't know in advance if B
depends on A or not. It might also depend on a different package.

According to the documentation

https://cmake.org/cmake/help/v3.0/command/link_directories.html

"Library locations returned by find_package() and find_library() are
absolute paths.". This is true for system libraries like
/usr/lib/libmpi.so. However, this seems not to be true for most other
packages that I use. A reason for this might be that I use the
FooBarConfig.cmake file for finding the package (not a FindFooBar.cmake
file, since that's not always available). I myself create those
following this example:

https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file

Here we have the line

set(FOOBAR_LIBRARIES foo)

which in the end will not use absolute paths. You can, however, use this
file for the find_package function, so according to the documentation it
should return absolute paths, which it apparently does not. So this
results in the error I described above when I compile C (where it links
to -lA instead of /custompath/A.so). So what is the correct way of
handling this?

Thanks in advance and best regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20160607/f9f37de8/attachment.html>


More information about the CMake mailing list