[CMake] What is the recommended ways to find libraries
Ondřej Čertík
ondrej.certik at gmail.com
Wed Jun 17 15:00:08 EDT 2015
Hi Oto,
On Wed, Jun 17, 2015 at 11:18 AM, Oto Petřík <oto.petrik at gmail.com> wrote:
> Hi,
>
>> Let's say my project wants to depend on a library PKG1 (here PKG1 can
>> be MPFR, MPC, PTHREAD, GMP, BZIP2, you name it). Let's say the library
>> is installed in $PKG1/include, $PKG1/lib, alternatively, all my
>> dependencies are installed in $HASHSTACK/include and $HASHSTACK/lib,
>> or perhaps they are installed system wide.
>
>> Perhaps I am missing the official recommendation regarding the above,
>> so that's why I am asking here. But if there is none, then I think the
>> current state is unsatisfactory.
>
> you may be looking for CMAKE_PREFIX_PATH
> (http://www.cmake.org/cmake/help/v3.2/variable/CMAKE_PREFIX_PATH.html)
>
> cmake -DCMAKE_PREFIX_PATH=$HOME/built-libs/gmp;$HOME/built-libs/arb;$HOME/built-libs/otherdependencies
> (or have a script that sets CMAKE_PREFIX_PATH environment variable)
>
> works with find_module, find_file, find_library,... expects
> directories to have standard structure (include,lib,...), see
> documentation of find_* commands for details.
> it is usually enough to set CMAKE_PREFIX_PATH to DESTDIRs used in
> dependencies' "make install DESTDIR=/foo"
>
> you may want to remove NO_DEFAULT_PATH as used in linked
> LibFindMacros.cmake, it disables this functionality.
Yes, indeed CMAKE_PREFIX_PATH is a replacement for our COMMON_DIR,
i.e. the case 3. above. So one can apply the following patch to our
macros:
--- a/cmake/LibFindMacros.cmake
+++ b/cmake/LibFindMacros.cmake
@@ -42,7 +42,6 @@ macro (libfind_library libname pkg)
${libname}
PATHS
${${PKG}_DIR}
- ${COMMON_DIR}
PATH_SUFFIXES
lib
lib64
@@ -73,7 +72,6 @@ macro (libfind_include HEADER pkg)
${HEADER}
PATHS
${${PKG}_DIR}
- ${COMMON_DIR}
PATH_SUFFIXES
include
NO_DEFAULT_PATH
and just use CMAKE_PREFIX_PATH instead of COMMON_DIR. I tried it in
our code and it works.
However, for 1. and 2. there is no solution in CMake. There is
CMAKE_INCLUDE_PATH and CMAKE_LIBRARY_PATH, but Isuru just tested it
and CMAKE_PREFIX_PATH takes precedence over CMAKE_INCLUDE_PATH, which
means that if you have a stack of packages installed in
CMAKE_PREFIX_PATH, you cannot specify PKG1 using CMAKE_INCLUDE_PATH.
And even if cmake changes the order, then if CMAKE_INCLUDE_PATH
besides PKG1 also contains PKG2, but you want PKG2 from
CMAKE_PREFIX_PATH, it won't work. The true solution is per package
include/lib paths, just like we implemented in our macros.
We tried many things, but we haven't figured out a solution other than
our macros. So it looks like CMake itself should have our macros (or
an equivalent functionality).
Ondrej
More information about the CMake
mailing list