Does `find_package` find in `CMAKE_INSTALL_PREFIX`?

Env

  • OS: Windows
  • cmake: 3.21.2

Case

wxWidgets installed in a custom folder like C:\Users\abc\AppData\Local\Programs\installed_libs.
CMAKE_PREFIX_PATH not set
only -DCMAKE_INSTALL_PREFIX=C:\Users\abc\AppData\Local\Programs\installed_libs
As a result, wxWidgets is found, C:\Users\abc\AppData\Local\Programs\installed_libs has not been referenced anywhere else

Question

Does find_package find in CMAKE_INSTALL_PREFIX?

Short answer: no.
Long answer: https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure
There are a lot of paths searched, so maybe your path IS accidentally mentioned somewhere or somehow already cached.

If
-DCMAKE_INSTALL_PREFIX=C:\Users\abc\AppData\Local\Programs\installed_libs
changes to
-DCMAKE_INSTALL_PREFIX=C:\Users\abc\AppData\Local\Programs\installed_libs111,
it fails to find wxWidgets, so find_package finds in CMAKE_INSTALL_PREFIX definitely, but not documented.

You’ve forced me to dig deeper, aaaand…
https://cmake.org/cmake/help/latest/variable/CMAKE_INSTALL_PREFIX.html

The installation prefix is also added to CMAKE_SYSTEM_PREFIX_PATH so that find_package(), find_program(), find_library(), find_path(), and find_file() will search the prefix for other software.

So it IS documented but not where I expected it to be (and yeah, I was wrong about how it works).

1 Like

The --prefix argument to cmake --install is generally the best way to set an installation prefix because it lets you decouple install-time requirements from your configuration logic.

I think the fact that CMAKE_INSTALL_PREFIX affects configure-time logic just reinforces that position.

Usually you (or at least consumers of your project!) really want to directly manipulate CMAKE_PREFIX_PATH or CMAKE_SYSTEM_PREFIX_PATH to alter configure-time logic. Ideally in a mechanism like a CMake toolchain file of maybe a CMake preset to keep your logic consistent, reusable, and tidy. As an aside, pretty much every time CMakeLists.txt or reusable CMake modules start manipulating these variables, they lose portability, gain complexity, or both.

Anyway, CMAKE_INSTALL_PREFIX seems to mostly work a lot of the time, but I’m currently convinced that it has been sufficiently surpassed by other mechanisms at this point to just stop using it full-stop. I’d be curious if there are edge cases that challenge that advice.

2 Likes

Thanks for your advice, it helps.