[CMake] how to link a test with library in build directory, not with installed version?

Aaron Cohen aaron at assonance.org
Tue May 28 13:24:50 EDT 2019


For "modern cmake", you should:

1) When building libA:
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.
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.

2) In your test CmakeLists, link against A::A rather than just A.

I believe that will work the way you want.

--Aaron

On Mon, May 27, 2019 at 11:58 AM Joachim Wuttke <j.wuttke at fz-juelich.de>
wrote:

> How to link a test with a library as obtained in the build directory,
> not with the installed version, which may be outdated?
>
> Disclosure: cross-posted from https://stackoverflow.com/questions/56328326
> .
>
> == Use case ==
>
> Suppose
>
>    cmake; make; ctest; make install
>
> works fine:
> - generates libA,
> - runs testB,
> - installs libA to ${CMAKE_INSTALL_LIBDIR}.
>
> Now I modify libA, and rerun
>    make; ctest.
> No reaction to my modifications:
>    ldd testB
> shows that the installed version of libA is used.
> Which is plain nonsense for a test.
>
> The test should always use the local version of libA, never the installed
> one.
>
> == Towards a solution ==
>
> I saw a lot of advise that involves RPATH, but found no variant that helps.
>
> So I hard-coded the path by changing
>    target_link_libraries(testB PRIVATE A)
> into
>    target_link_libraries(testB PRIVATE ${CMAKE_BINARY_DIR}/lib/libA.so).
>
> This works, but is no longer platform independent.
> How to solve my problem in a way that also works under Windows?
>
> - Joachim
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190528/25517fb5/attachment.html>


More information about the CMake mailing list