25

I have a CMake project where I am using a library and now I want to test my code with a different version of that library. I can set INCLUDE_DIRECTORIES (and possibly later also linking) in the below example. But because I only want to do this temporarily, I'd like to manually set this path with ccmake/cmake-gui.

How do I do this?

project(min_example)
cmake_minimum_required(VERSION 2.8)

find_package(OpenCV REQUIRED)
# Without the following line please:
INCLUDE_DIRECTORIES("/home/me/src/opencv/install/include")
add_executable(min_example main.cpp)
target_link_libraries(min_example ${OpenCV_LIBS})
Unapiedra
  • 15,037
  • 12
  • 64
  • 93

2 Answers2

34

This should be possible by setting the CMAKE_PREFIX_PATH variable upon configuring your build. In your project directory generate a test_build directory and run:

mkdir test_build
cd test_build
cmake -DCMAKE_PREFIX_PATH=/home/me/src/opencv/install ..

Setting the CMAKE_PREFIX_PATH variable will make the find_package(OpenCV REQUIRED) command pick your OpenCV installation in /home/me/src/opencv and set the OpenCV_LIBS and OpenCV_INCLUDE_DIR variables accordingly.

Alternatively you can edit a CMakeCache.txt file of an existing build directory with the CMake GUI editor and add the CMAKE_PREFIX_PATH definition there. You have to re-configure your project then.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • It doesn't work. find_package(OpenCV) still picks up the libs+include in /usr/local/... I used your three lines as given above. Any idea what else I could try? – Unapiedra Aug 12 '11 at 09:08
  • 1
    @Unapiedra Set the CMAKE_PREFIX_PATH to the directory in you home that contains the OpenCVConfig.cmake file (probably `/home/me/src/opencv/install`). – sakra Aug 12 '11 at 09:15
  • 1
    Thanks. It's actually `.../install/share/OpenCV` but that does it! – Unapiedra Aug 12 '11 at 09:24
  • 1
    how do you list more than one directory on the cmake_prefix_path? – Andrew Hundt Jul 22 '15 at 16:58
  • 12
    @AndrewHundt set it to a CMake list, i.e. `cmake -D "CMAKE_PREFIX_PATH=/a/b/;/c/d"` – sakra Jul 22 '15 at 17:05
  • I had to use `CMAKE_MODULE_PATH` as mentioned in https://stackoverflow.com/questions/34795816/hinting-findname-cmake-files-with-a-custom-directory. – Hermann Apr 07 '20 at 12:26
1

Using config in find_package will restrict search path to OpenCV_DIR. This will use the cmake config that opencv generates at build time to setup paths to include and libs

set(OpenCV_DIR "<cusompath>" CACHE PATH '' ${SHOULD_FORCE_CACHE})
find_package(OpenCV REQUIRED CONFIG)
r11
  • 107
  • 1
  • 4
  • Both methods appear to work but isn't the xxx_DIR variable actually intended to just be used for internal? – Klypto Jan 11 '21 at 16:59