Search code examples
cmakebuilddependencieslibraries

Pointing cmake to a directory that contains a dependency


I am battling a "A required package was not found" error from cmake.

Let's say I have software foo that depends on the bar library.

And libbar.a is installed in $HOME/lib with bar.h installed in $HOME/include directory.

With automake tools, you typically can point the build system to bar by using:

$ cd foo
$ ./configure --with-bar=$HOME

But how can I achieve the same with the cmake tool?

I want cmake to be smart enough to not only look in /usr/lib and /usr/local/lib for a library, but also in my $HOME/lib directory.


Solution

  • This is trivial:

    $ cmake ... -DCMAKE_PREFIX_PATH=$HOME
    

    If you want this to happen for bar specifically, you can set bar_ROOT instead.

    See the documentation:

    Semicolon-separated list of directories specifying installation prefixes to be searched by the find_package(), find_program(), find_library(), find_file(), and find_path() commands. Each command will add appropriate subdirectories (like bin, lib, or include) as specified in its own documentation.

    https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html https://cmake.org/cmake/help/latest/variable/PackageName_ROOT.html