I want to use some third-party headers (or a library) in a project that uses CMake. But it does not find the headers (the library). Why does CMake not find it?
问题:
回答1:
CMake\'s find routines look for headers and libraries at some specific places. This includes the PATH
variable, and the default locations for installed software, e.g., for many Linuces /usr/bin
. Additionally, it evaluates the CMake variable CMAKE_PREFIX_PATH
.
You have two possibilities to help CMake finding the required files:
Check whether your software is properly installed. For self-compiled software, that\'s usually done by
make install
or similar. If you use packages (RPM or deb), they are in general installed and can be found with thePATH
variable.If you don\'t want or can install the software, add its path to the
CMAKE_PREFIX_PATH
variable. Either pass it to the CMake callcmake -DCMAKE_PREFIX_PATH=/path/to/software ..
or edit/add the according field in the CMake-GUI.
You have to delete the CMakeCache.txt
, otherwise CMake will not find the library, because it does not check but use the cached result. Re-run CMake and it should work.
Evaluation order
If you have multiple versions of a library on your system, add the one you want to use to the CMAKE_PREFIX_PATH
as the variables gets evaluated prior to the system path variables.
Module-specific variables
Some modules offer specific variables like mylib_DIR
or mylib_ROOT
to indicate the search path. Its use is discouraged and they are only left for backwards-compatibility. New modules don\'t have these modules and commits adding such variables are rejected by the CMake developers.
Documentation
More details on how CMake searches files and in which order can be found in the documentation: https://cmake.org/cmake/help/latest/command/find_library.html