Specifically, I want to include opencv from my/path/to/opencv/release
where my own opencv is built other than the system's opencv lib in /usr/local/include
. How can I set cmake
to achieve this? I'm using Ubuntu 14.04.

- 1,545
- 2
- 17
- 28
-
1you can modify cmake_install_prefix variable for a custom installation. syntax would be cmake -D CMAKE_INSTALL_PREFIX my/path/to/opencv/release – baci Aug 23 '16 at 06:45
-
I've built and installed my opencv to a custom path. But I don't know how to include them in a new c++ program using `cmake`. It uses `/usr/local/include` as default. – Meta Fan Aug 23 '16 at 06:52
-
most probably pkg-config default path stays same even for custom installation, you may need to modify it. another way for gcc your syntax would be myapp -I/path/to/the/folder/where/opencv.h (for any custom include) -L/path/to/the/folder/where/libopencv.so -lopencv (for any custom lib) – baci Aug 23 '16 at 07:02
-
You could delete the cmake cache, retry, and post the cmake variables related to OpenCV, maybe then we can help. – SpamBot Aug 24 '16 at 07:56
-
@SpamBot I agree the cmake cache entry needs to be deleted, or cmake is not going to search again. But baci is correct - see https://cmake.org/cmake/help/v3.0/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." – Bull Aug 24 '16 at 13:55
2 Answers
To provide an example, below is a Find-CMake file for the Luajit library. Your CMake project has one that's probably called something like "FindOpenCV.cmake". It most likely has a default installation path that was manually added, such as: "/usr/include/luajit-2.0 /usr/local/include/luajit-2.0" and you would change these DIR's to your desired install directory. If the default path is contained in a variable, you could either find the definition of that variable and change it (preferably through a configuration option), or override it with a string.
# Try to find Lua or LuaJIT depending on the variable ENABLE_LUAJIT.
# Sets the following variables:
# LUA_FOUND
# LUA_INCLUDE_DIR
# LUA_LIBRARY
#
SET (LUA_FOUND FALSE)
SET (LUA_INTERPRETER_TYPE "")
SET (LUA_INTERPRETER_TYPE "LuaJIT")
SET (LUA_LIBRARY_NAME luajit-5.1)
SET (LUA_INCLUDE_DIRS /usr/include/luajit-2.0 /usr/local/include/luajit-2.0)
FIND_PATH (LUA_INCLUDE_DIR lua.h ${LUA_INCLUDE_DIRS}
HINT ${LUAJIT_INCLUDE_DIR_HINT}/include)
FIND_LIBRARY (LUA_LIBRARY NAMES ${LUA_LIBRARY_NAME} PATHS /usr/lib /usr/local/lib
HINT ${LUAJIT_INCLUDE_DIR_HINT}/bin)
#...
MARK_AS_ADVANCED ( LUA_INCLUDE_DIR LUA_LIBRARY)

- 233
- 2
- 9
There are multiple ways to achieve this, you can modify a FindOpenCV.cmake file, you set
the cmake variable OpenCV_DIR
before the library is found https://stackoverflow.com/a/9835300/2079934, you can export the environment variable OpenCV_DIR
before you run CMake https://stackoverflow.com/a/16959771/2079934.
I would recommend not to hard-code any library paths into CMakeLists.txt, that would take away all the benefits of CMake. In Linux, I would use export to set OpenCV_DIR
, in other OSes CMake GUI is more common there you could edit the path variable in the GUI.
-
Actually I can use ccmake to set OpenCV_DIR, but when I build the program, it still uses the system's opencv include directory. I can't figure out how it works. – Meta Fan Aug 23 '16 at 08:49
-
Another thing is you need to use `target_link_libraries(MyApp ${OpenCV_LIBS})` instead of `opencv_core` – SpamBot Aug 23 '16 at 09:54
-