<div dir="ltr"><div>For "modern cmake", you should:</div><div><br></div><div>1) When building libA:</div><div>a) Create an alias to your library (add_library A::A ALIAS A). This will ensure that users are explicitly linking against a _target_ and not a file named "libA" which just happens to match the name of the target you are attempting to use.</div><div>b) Arrange that AConfig.cmake is installed to /usr/lib/cmake/A/AConfig.cmake, which also exports an A::A target. This is the "installed" version of the library, for consumers.</div><div><br></div><div>2) In your test CmakeLists, link against A::A rather than just A.</div><div><br></div><div>I believe that will work the way you want.</div><div><br></div><div>--Aaron</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, May 27, 2019 at 11:58 AM Joachim Wuttke <<a href="mailto:j.wuttke@fz-juelich.de">j.wuttke@fz-juelich.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">How to link a test with a library as obtained in the build directory,<br>
not with the installed version, which may be outdated?<br>
<br>
Disclosure: cross-posted from <a href="https://stackoverflow.com/questions/56328326" rel="noreferrer" target="_blank">https://stackoverflow.com/questions/56328326</a>.<br>
<br>
== Use case ==<br>
<br>
Suppose<br>
<br>
  cmake; make; ctest; make install<br>
<br>
works fine:<br>
- generates libA,<br>
- runs testB,<br>
- installs libA to ${CMAKE_INSTALL_LIBDIR}.<br>
<br>
Now I modify libA, and rerun<br>
  make; ctest.<br>
No reaction to my modifications:<br>
  ldd testB<br>
shows that the installed version of libA is used.<br>
Which is plain nonsense for a test.<br>
<br>
The test should always use the local version of libA, never the installed one.<br>
<br>
== Towards a solution ==<br>
<br>
I saw a lot of advise that involves RPATH, but found no variant that helps.<br>
<br>
So I hard-coded the path by changing<br>
  target_link_libraries(testB PRIVATE A)<br>
into<br>
  target_link_libraries(testB PRIVATE ${CMAKE_BINARY_DIR}/lib/libA.so).<br>
<br>
This works, but is no longer platform independent.<br>
How to solve my problem in a way that also works under Windows?<br>
<br>
- Joachim<br>
<br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div></div>