Use user version of CMake Package

Viewed 47

I've been working on building projects a project with GoogleTest. Everything works fine when I fetch GoogleTest via FetchContent, but when I use the system version I get a linker error.

I have a user-level directory for CMake packages at /home/alecto/.local/cmake-packages/, and CMAKE_PREFIX_PATH is set to this directory. I installed GoogleTest inside this directory, but CMake still prefers the system version of the package, even though CMake finds other packages installed inside the cmake-packages directory (such as Catch2).

How can I instruct CMake to prefer the version of GoogleTest installed inside /home/alecto/.local/cmake-packages?

1 Answers

According to the doc:
ref: https://cmake.org/cmake/help/latest/command/find_package.html

  1. New in version 3.12: Search paths specified in the <PackageName>_ROOT CMake variable and the <PackageName>_ROOT environment variable, where <PackageName> is the package to be found. The package root variables are maintained as a stack so if called from within a find module, root paths from the parent's find module will also be searched after paths for the current package. This can be skipped if NO_PACKAGE_ROOT_PATH is passed or by setting the CMAKE_FIND_USE_PACKAGE_ROOT_PATH to FALSE. See policy CMP0074.
  1. Search paths specified in cmake-specific environment variables. These are intended to be set in the user's shell configuration, and therefore use the host's native path separator (; on Windows and : on UNIX). This can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed or by setting the CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH to FALSE:
    ° <PackageName>_DIR

So Try to set GTest_ROOT or GTest_DIR

Related