How to use my own functions with CMake for OpenCV - c++

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 )

Related

OpenCV c++: Undefined symbols for architecture arm64: [duplicate]

I consider this a fundamental step for creating projects that use OpenCV libraries so you don't need to manually include all the libraries. There is not detailed information on this topic, at least for a newbie that just wants to use OpenCV as soon as posible, so:
Which is the easiest and scalable way to create a multiplatform c++ OpenCV with Cmake?
First: create a folder Project containing two subfolders src and include, and a file called CMakeLists.txt.
Second: Put your cpp inside the src folder and your headers in the include folders.
Third: Your CMakeLists.txt should look like this:
cmake_minimum_required(VERSION 2.8)
PROJECT (name)
find_package(OpenCV REQUIRED )
set( NAME_SRC
src/main.cpp
)
set( NAME_HEADERS
include/header.h
)
INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR}/include )
link_directories( ${CMAKE_BINARY_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
add_executable( name ${NAME_SRC} ${NAME_HEADERS} )
target_link_libraries( sample_pcTest ${OpenCV_LIBS} )
Fourth: Open CMake GUI and select the root folder as input and create a build folder for the output. Click configure, then generate, and choose the generator (VisualStudio, Eclipse, ...)
I am using opencv3.0 and cmake3.8,
config below work for me!
######## A simple cmakelists.txt file for OpenCV() #############
cmake_minimum_required(VERSION 2.8)
PROJECT(word)
FIND_PACKAGE( OpenCV REQUIRED )
INCLUDE_DIRECTORIES( ${OpenCV_INCLUDE_DIRS} )
ADD_EXECUTABLE(word main.c)
TARGET_LINK_LIBRARIES (word ${OpenCV_LIBS})
########### end ####################################

C++ OpenCV include error 'file not found'

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.

Building a MacOS executable for distribution using cmake including OpenCV

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 )

Cmake find header files matching cpp code

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})

Undefined reference to SDL with CMake

I'm working on a project using the SDL (v1.2.15-7) and CMake (3.2.1). In the *.h files I added the #include <SDL.h> and when I compile it, I have a bunch of errors : undefined reference to SDL_...
I think the mistake comes from the CMakeLists.txt but I really don't know where.
Here's the file content :
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )
SET( PROJ_NAME "Project" )
SET( PROJ_PATH ${CMAKE_SOURCE_DIR} )
SET( PROJ_OUT_PATH ${CMAKE_BINARY_DIR} )
SET( PROJ_INCLUDES "include" )
FILE( GLOB_RECURSE PROJ_SOURCES src/*cpp test/*cpp doc/*)
FILE( GLOB_RECURSE PROJ_HEADERS ${PROJ_INCLUDES}/${PROJ_NAME}/*.h )
PROJECT( ${PROJ_NAME} )
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
FIND_PACKAGE( SDL REQUIRED )
SET( PROJ_LIBRARIES ${SDL_LIBS} )
INCLUDE_DIRECTORIES( ${PROJ_INCLUDES} ${SDL_INCLUDE_DIR} )
ADD_EXECUTABLE( ${PROJ_NAME} ${PROJ_SOURCES} ${PROJ_HEADERS} )
TARGET_LINK_LIBRARIES( ${PROJ_NAME} ${PROJ_LIBRARIES} )
I also tried #include SDL/SDL.h
The error message is coming from the linker, in which case it means you're not linking against the SDL libraries.
The CMake documentation specifies that the FindSDL module defines a variable named SDL_LIBRARY, but you're using SDL_LIBS. So, SET(PROJ_LIBRARIES ${SDL_LIBRARY}) instead.
When using a standard module for finding a package, try browsing the documentation first to take a look at the variables it defines. The names aren't always standard.
Try using SDL_LIBRARY instead of SDL_LIBS
You forgot to link your target to SDL_LIBRARIES in the last line of your CMakeLists.txt.
The linker produces the error, so it is unrelated to your includes.