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

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

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

j1wuttke
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

smime.p7s (6K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: how to link a test with library in build directory, not with installed version?

remleduff
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 <[hidden email]> 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

--

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
Reply | Threaded
Open this post in threaded view
|

Re: how to link a test with library in build directory, not with installed version?

remleduff
On Tue, May 28, 2019 at 1:24 PM Aaron Cohen <[hidden email]> wrote:
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.

I was originally writing a longer answer and forgot to remove this part. You don't actually need to create/install this file if you are just concerned with testing within your project. (It's good to do in general, it's just un-related to your question).

--

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