I have a problem using OpenCV 4.1.2 in C++. I have this CMakelists.txt:
cmake_minimum_required(VERSION 2.8.12)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
add_compile_options(-std=c++11)
add_library( src
src/VideoVeed.h
src/VideoVeed.cpp
)
add_executable( program
program/main.cpp
)
target_link_libraries( program
src
${OpenCV_LIBS}
)
As you can see I have two folders with source code:
program contains main.cpp
src contains VideoVeed.h & VideoVeed.cpp
When I include OpenCV in main.cpp like this: <opencv2/opencv.hpp>, it works fine. But when I include OpenCV (the same way) it gives the error fatal error: 'opencv2/opencv.hpp' file not found.
I think I'm doing something wrong in my CMakelists.txt, but I can't figure out what exactly.
I hope someone is able to help me. Thanks in advance!
You should add the line, target_include_directories(), so that the OpenCV include directories are included in your executable:
add_executable( program
program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIR})
Depending on the version of OpenCV you are using, you may need
to use OpenCV_INCLUDE_DIRS instead:
add_executable( program
program/main.cpp
)
target_include_directories(program PRIVATE ${OpenCV_INCLUDE_DIRS})
EDIT: OpenCV 4.1.2 populates the variable OpenCV_INCLUDE_DIRS, so this is the variable you should use. See this tutorial.
Related
I have a cmakelists.txt file that compiles my MacOS application using OpenCV, C++, OpenGL, based on information I found on SO. When I try copying the folder of the executable to another Mac, I get an error due to missing OpenCV libs, expected in a different folder from my executable.
Is there a change I can make to the file so that it will include the OpenCV compiled code in the executable, or its folder for distribution? For what it's worth I am using CMake because I could not figure out a simpler way to get it to build.
cmake_minimum_required( VERSION 3.1 )
project( go)
set (CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED )
find_library( COCOA_FW Cocoa )
find_library( OPENGL_FW OpenGL )
find_library( IOKIT_FW IOKit )
add_executable( go main.cpp gl_utils.cpp user_interface.cpp geometry_2d.cpp)
target_include_directories( go PRIVATE ${CMAKE_SOURCE_DIR}/include )
target_link_libraries( go ${OpenCV_LIBS} ${COCOA_FW} ${OPENGL_FW} ${IOKIT_FW} ${CMAKE_SOURCE_DIR}/common/libGLEW.a ${CMAKE_SOURCE_DIR}/common/libglfw3.a)
On another question related: Static OpenCV compile
set(OpenCV_STATIC ON)
find_package(OpenCV REQUIRED)
Now for the find_library( OPENGL_FW OpenGL ), you will need to be more explicit:
# This could change based on the OpenGL library you have installed.
find_library( OPENGL_FW libGL.a )
I know there are a thousand questions like this but I'm struggling even after looking at those.
My directory tree
./src/main/cpp/main.cpp
./src/main/cpp/CascadeLoader.cpp
./resources/headers/CascadeLoader.h
If in main.cpp I use
#include "CascadeLoader.cpp"
my code works but if I do a
#include "CascadeLoader.h"
I get a build error of
undefined reference to CascadeLoader::CascadeLoader()
my CMakeLists.txt I added
cmake_minimum_required(VERSION 2.8)
project( ASLInterpreter )
find_package ( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( ASLInterpreter src/main/cpp/main.cpp )
target_link_libraries( ASLInterpreter ${OpenCV_LIBS} )
You don't link your CascadeLoader.cpp file, this is why you get an undefined reference error.
Try
add_executable( ASLInterpreter src/main/cpp/main.cpp /src/main/cpp/CascadeLoader.cpp)
You can also group your .cpp files together which is useful if you have many of them.
set(include /src/main/cpp/CascadeLoader.cpp
/src/main/cpp/example.cpp)
add_executable( ASLInterpreter src/main/cpp/main.cpp ${include})
This is a total cmake noob question I'm sure.
I'm working on an OpenCV project and wish to test something using the latest beta release. How can I specify the beta libraries without installing them onto my system? My beta opencv has been successfully built in:
/Users/paul/hacking/robotics/opencv/build/lib/
My current cmake has basically been lifted from the opencv samples and looks like this:
# cmake for Stereo Vision App
# your opencv/build directory should be in your system PATH
# set minimum required version for cmake
cmake_minimum_required(VERSION 2.8)
# define the project name
set(project_name "Stereo")
# set the project namee
project("${project_name}")
# add opencv package to the project
find_package( OpenCV REQUIRED )
MESSAGE("OpenCV version : ${OpenCV_VERSION}")
# add opencv include directories to the project
include_directories( ${OpenCV_INCLUDE_DIRS} )
# add include directory
include_directories (${Stereo_SOURCE_DIR})
# add library
add_library( CameraCalibrator CameraCalibrator.cpp)
# add executable
#add_executable( videoprocessing videoprocessing.cpp)
#add_executable( tracking tracking.cpp)
#add_executable( foreground foreground.cpp)
add_executable( calibrate calibrate.cpp)
add_executable( rightsideup rightsideup.cpp)
add_executable( live live.cpp)
add_executable( live2 live2.cpp)
add_executable( stereo-tune stereo-tune.cpp)
add_executable( project project.cpp)
add_executable( stereo_calibrate stereo_calibrate.cpp)
add_executable( capture_two_camera_chessboards capture_two_camera_chessboards.cpp)
add_executable( capture_stereo_chessboards capture_stereo_chessboards.cpp)
# link libraries
#target_link_libraries( videoprocessing ${OpenCV_LIBS})
#target_link_libraries( tracking ${OpenCV_LIBS})
#target_link_libraries( foreground ${OpenCV_LIBS})
target_link_libraries( rightsideup ${OpenCV_LIBS})
target_link_libraries( live ${OpenCV_LIBS})
target_link_libraries( live2 ${OpenCV_LIBS})
target_link_libraries( stereo-tune ${OpenCV_LIBS})
target_link_libraries( project ${OpenCV_LIBS})
target_link_libraries( stereo_calibrate ${OpenCV_LIBS})
target_link_libraries( capture_two_camera_chessboards ${OpenCV_LIBS})
target_link_libraries( capture_stereo_chessboards ${OpenCV_LIBS})
target_link_libraries( calibrate ${OpenCV_LIBS} CameraCalibrator)
I've tried using link_libraries and setting the version on the find package line to 3.0, however it always finds the system installed libraries 2.4.10
Edit 1:
cmake -DPCL_DIR:PATH="../../pcl/build" -DOpenCV_DIR:PATH="../../opencv/build" ..
Is not working for me for some reason. Likewise when I try to set these variables inside the CMake script it also does not work.
set(PCL_DIR "../../pcl/build" CACHE PATH "")
set(OpenCV_DIR "../../opencv/build" CACHE PATH "")
Many thanks!
Paul
You're going in the right direction. cmake's Find scripts look in the standard system paths first unless told otherwise. Each FIND script has its own set of cmake variables which you can set to alter the behaviour.
For FindOpenCV.cmake it seems to be OPENCV_BASE_DIR.
Here's a link to some source code:
https://github.com/rpavlik/cmake-modules/blob/master/FindOpenCV.cmake
Edit:
I don't fully understand, but the following seems to work with absolute paths
set(OpenCV_DIR "/Users/paul/hacking/robotics/opencv/build" CACHE PATH "")
set(PCL_DIR "/Users/paul/hacking/robotics/pcl/build" CACHE PATH "")
set(VTK_DIR "/Users/paul/hacking/robotics/VTK/build" CACHE PATH "")
At least less things to go wrong if I get the command line wrong, I no longer need to remember to do the following:
OpenCV_DIR=../../opencv/build cmake ..
In the PCL documentation it says to use:
set(PCL_DIR "/path/to/PCLConfig.cmake")
But this doesn't work for me.
i wanted to run this c++/OpenCV code that i found online:
http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
I have a problem with CMake, it returned this error:
CMake Error at CMakeLists.txt:5 (target_link_libraries):
Cannot specify link libraries for target "SURF_Homography" which is not
built by this project.
I searched on the web the solution, and i found this
CMake OpenCV Cannot Specify Link Libraries
but i can't understand why mine don't work because it looks like the same, this is my CMakeLists file:
cmake_minimum_required(VERSION 2.8)
project( SURF_Homography )
find_package( OpenCV REQUIRED )
add_executable( SURF_Homography.cpp SURF_Homography.cpp )
target_link_libraries( SURF_Homography ${OpenCV_LIBS} )
hope you can help me
thanks
Luca
Change the add_executable line:
add_executable( SURF_Homography SURF_Homography.cpp )
Otherwise, the target you are specifying in the target_link_libraries line won't exist :)
I am using OpenCV 2.3 on Windows 7 32 bits with Visual C++ 2010.
My CMakeLists file looks like that:
SET( PROJECT_NAME Tennis_tracking )
PROJECT( ${PROJECT_NAME} )
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
FIND_PACKAGE( OpenCV REQUIRED )
ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} ${OpenCV_LIBS} )
ADD_EXECUTABLE( histogram histogram.cpp )
TARGET_LINK_LIBRARIES( histogram ${OpenCV_LIBS} )
For example I'd like to create a custom function "getImageHistogram" for example (which already exists in OpenCV) that will be used by main.cpp. The header and main files are done, how do I link those in my CMakeLists?
Thank you very much.
As long as you are using C++ you can define this function within another (custom) namespace and call it as custom::getImageHistogram().
And for the CMake part, there's nothing special to be done since you are already adding that file to the build process:
ADD_EXECUTABLE( ${PROJECT_NAME} main.cpp )