<div dir="ltr">Hey everyone,<br><br>I posted a question on StackOverflow a while ago, but got no real answer<br>to my question there. I posted an answer there by myself, but this<br>solution does not work on every system. Since it also involves some<br>possible inconsistencies in the CMake documentation, I decided to ask<br>the question here as well.<br><br>Say I have packages A, B, and C. Package B uses package A and package C<br>uses package B. I create shared libraries.<br><br>So in package B I do something like<br><br>    find_package(A)<br>    ...<br>    if(${A_FOUND})<br>    target_link_libraries(B ${A_LIBRARIES})<br>    endif()<br><br>and in package C I do<br><br>    find_package(B)<br>    ...<br>    if(${B_FOUND})<br>    target_link_libraries(C ${B_LIBRARIES})<br>    endif()<br><br>    add_executable(main main.cpp)<br>    target_link_libraries(main C)<br><br>where ${B_LIBRARIES} contains only B. The compiler will now complain<br><br>    /usr/bin/ld: cannot find -lA<br>    collect2: error: ld returned 1 exit status<br><br>as long as A is installed in a place that is not in the default<br>directories. I was wondering what is the correct way of handling this.<br>For me using find_package(A) in C (which would work) doesn't seem to be<br>the nice. For me especially, because I don't know in advance if B<br>depends on A or not. It might also depend on a different package.<br><br>According to the documentation<br><br><a href="https://cmake.org/cmake/help/v3.0/command/link_directories.html">https://cmake.org/cmake/help/v3.0/command/link_directories.html</a><br><br>"Library locations returned by find_package() and find_library() are<br>absolute paths.". This is true for system libraries like<br>/usr/lib/libmpi.so. However, this seems not to be true for most other<br>packages that I use. A reason for this might be that I use the<br>FooBarConfig.cmake file for finding the package (not a FindFooBar.cmake<br>file, since that's not always available). I myself create those<br>following this example:<br><br><a href="https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file">https://cmake.org/Wiki/CMake/Tutorials/How_to_create_a_ProjectConfig.cmake_file</a><br><br>Here we have the line<br><br>set(FOOBAR_LIBRARIES foo)<br><br>which in the end will not use absolute paths. You can, however, use this<br>file for the find_package function, so according to the documentation it<br>should return absolute paths, which it apparently does not. So this<br>results in the error I described above when I compile C (where it links<br>to -lA instead of /custompath/A.so). So what is the correct way of<br>handling this?<br><br>Thanks in advance and best regards,<br>Sven</div>