My computer is running Ubuntu-16.04-LTS and OpenCV-2.4.13 is already installed on it. However, I would like to use the functionalities of newer versions of OpenCV, such as OpenC
While you are setting up the include files to use the newly installed OpenCV headers, you are linking against the opencv on your system path. The version is just stored in the headers, which is why it's outputting the expected version but failing to link. Try setting the variable "OpenCV_DIR" to the location of the OpenCVConfig.cmake file you want to use before running the find module.
See the documentation for your version of cmake here. Note the two modes that find_package can be run in (Module or Config mode).
Unless you are linking to static libraries, you will still have problems running your library / application. To fix that, you'll need to add the libraries to your LD_LIBRARY_PATH.
I have a working CMakelists.txt
for almost the same configuration as you describe except that I am running a dauntingly old Ubuntu 12.04 (its not my own computer).
I believe your problem comes from this line:
find_package(OpenCV REQUIRED)
Which gives you access to your distribution's OpenCV 2.4. Then you are linking against the manually installed 3.2.x version. So problems arise as soon as the interface of a function you use has changed between the two version. Your first piece of code run by chance I think.
Here is my CMakeList.txt
:
cmake_minimum_required(VERSION 2.8)
project(demo)
find_package(OpenCV 3.2 REQUIRED PATHS "/path/to/OCV3.2/install/dir/")
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)
target_link_libraries(main ${OpenCV_LIBS})
If you do not want to commit to your repository the hardcoded path to your install of OpenCV 3.2 you can refine this CMakeList.txt
by changing the find_package
line to:
if(DEFINED ENV{OPENCV_INSTALL_DIR})
find_package(OpenCV 3.2 REQUIRED PATHS $ENV{OPENCV_INSTALL_DIR})
else()
message("OPENCV_INSTALL_DIR not set, searching in default location(s)")
find_package(OpenCV 3.2 REQUIRED)
endif(DEFINED ENV{OPENCV_INSTALL_DIR})
Then you just have to define the variable OPENCV_INSTALL_DIR
before running cmake. I do that by exporting it from my .bashrc
Indeed, you're linking against system-default version of OpenCV.
Reconfigure your project by calling CMake with right path to file OpenCVConfig.cmake.
cmake /path/to/your/sources -DOpenCV_DIR=/home/ubuntu/opencv-3.2.0
Find file CMakeCache.txt
in your build directory. It contains internal CMake variables, OpenCV paths are also there.
Also, it is incorrect to hardcode paths. Use include_directories(${OPENCV_INCLUDE_DIRS})
And quotation from OpenCVConfig.cmake
- OpenCV_LIB_DIR : The directory(es) where lib files are. Calling LINK_DIRECTORIES with this path is NOT needed.