How to conveniently force CMAKE to use a particular build of static library file?

2.2k Views Asked by At

I current have an open source 3rd party library A installed in \usr\local. Now I want to experiment some code change on this library but I want to do this test in a local folder so it won't affect the system library. Let us call the local build A'.

I have an executable B which uses library A as static library. In CMakelists.txt, it used find_pacakge(A) to find the one installed in \usr\local. What is the easiest way to make it link to A' in the local folder ?

I figured out if I INSTALL A' to a local folder (or any folder different from usr\local), we can force CMAKE to find package in that folder by using HINTS. However, I hope there is a better way without even installing. After all, A and A' are using the same header files and just located in different locations. So I assume the include_directories doesn't need to be changed. But I don't know how to link the library to a specified file in a specified folder instead of the default system folder found from find_package.

Really a newbie in Cmake, I hope I made myself clear. Thanks a lot

1

There are 1 best solutions below

1
On

1) Usually you take everything i.e. not include here and lib there, also in Modern CMake you can use target_include_directory() so include and library are "coupled" together i.e. you get everything with one find_package() call.
cf: https://cmake.org/cmake/help/latest/command/target_include_directories.html

2) Take a look at CMAKE_PREFIX_PATH this is the correct way to modify default search path if you want to find A' instead of A>
cf: https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html

3) if you want to use the build directory of A' then the CMake project A' must use "export()" so you can use it from buildir directly.
note: few projects support it and the correct way is to install first (in a local folder) then use it.
cf: https://cmake.org/cmake/help/latest/command/export.html