Not sure how to add opencv library to CMakeLists.txt - c++

I hope someone can help me.
I have a simple CMakeLists.txt in order to build my project on Ubuntu. I'm using CMake 2.8.1 and at the moment this is the code:
cmake_minimum_required(VERSION 2.4.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)
rosbuild_init()
find_package(OpenCV 2)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()
#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
rosbuild_add_executable (RosPub src/paste.cpp)
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
I need to add opencv libraries on my project. I have added them but i can't still get my code to work. its keeps posting me this error:
‘struct MyOpenNIExample::ImgContext’ has no member named ‘image’
there is a few of them.
after i added find_package(OpenCV REQUIRED to the CMakeLists.txt,
i get this error
Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
directory containing a CMake configuration file for OpenCV. The file will
have one of the following names:
OpenCVConfig.cmake
opencv-config.cmake
what shld i do? I am using Apple and using Ubuntu 10.04.
Since i need
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
i added
find_package(OpenCV 2),
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace) and
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})

#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
the include files above are included in the vision_opencv in ROS. so to include it, add the opencv dependency in the manifest file.
That would help.

Get FindOpenCV.cmake from above link and put it anywhere on your computer.
After cmake_minimum_required add line set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [path to folder where you put FindOpenCV.cmake])
Add find_package(OpenCV) to your CMakeLists.txt
On this step you can check for OpenCV_FOUND and other OpenCV variables in your CMakeLists.txt

Related

Unable to include android ndk in cmake visual studio

I was trying to make a cross platform imgui project in visual studio 2022, starting with android. In the beginning, I was not able to include some android libraries properly, so after following several tutorials, I ended up with this CMakeLists.txt:
# CMakeList.txt : CMake project for CMakeProject3 (imgui Cross-platform), include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject3 (imgui Cross-platform)")
list(APPEND CMAKE_PREFIX_PATH "C:/Users/HP/source/repos/CMakeProject3 (imgui Cross-platform)/build/vcpkg_installed/x64-windows/share/")
find_package(imgui REQUIRED)
# Add source to this project's executable.
add_executable (CMakeTarget "CMakeProject3 (imgui Cross-platform).cpp" "CMakeProject3 (imgui Cross-platform).h" "imgui_android.cpp" "imgui_impl_android.cpp" "imgui_impl_android.h")
target_include_directories(CMakeTarget PRIVATE "C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/android/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/sources/android/native_app_glue/")
target_link_libraries(CMakeTarget
PRIVATE
imgui::imgui
)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.
It works partially but according to example of android imgui example, it needs
#include <android/log.h>
#include <android_native_app_glue.h>
#include <android/asset_manager.h>
I changed to:
#include "log.h"
#include "android_native_app_glue.h"
#include "asset_manager.h"
It started to give several errors for cannot open source file. The errors were for poll.h, pthread.h, sched.h, android/configuration.h, etc. It seems to me that I need to inlcude android ndk completely but
include(AndroidNdkGdb)
include(AndroidNdkModules)
gives error in cmake even when I installed android ndk and sdk.
I want to be able to use android libraries in my cross-platform project in visual studio 2022.
I fixed the error by updating CMakeLists.txt to:
# CMakeList.txt : CMake project for CMakeProject3 (imgui Cross-platform), include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject3 (imgui Cross-platform)")
list(APPEND CMAKE_PREFIX_PATH "C:/Users/HP/source/repos/CMakeProject3 (imgui Cross-platform)/build/vcpkg_installed/x64-windows/share/")
find_package(imgui REQUIRED)
set(CMAKE_SYSROOT C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot)
# Add source to this project's executable.
add_executable (CMakeTarget "CMakeProject3 (imgui Cross-platform).cpp" "CMakeProject3 (imgui Cross-platform).h" "imgui_android.cpp" "imgui_impl_android.cpp" "imgui_impl_android.h")
target_include_directories(CMakeTarget PRIVATE "C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/sources/android/native_app_glue/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/asm/")
target_link_libraries(CMakeTarget
PRIVATE
imgui::imgui
)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.

How to use Caffe library in C++ project with CMakeLists.txt

I'm trying to use Caffe in my C++ project which I compile with CMakeLists.txt, but it doesn't want to work. My only line in the code is
#include <caffe/caffe.hpp>
I compiled Caffe myself, it is installed in the directory "/home/tamas/caffe". My CMakeLists.txt looks like this so far:
cmake_minimum_required (VERSION 3.5)
include(FindPkgConfig)
project (main)
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED TRUE)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -std=c++11 -pthread")
set (source_dir "${PROJECT_SOURCE_DIR}/src/")
set (OpenCV_DIR "/home/tamas/opencv/include/opencv2")
set (Caffe_DIR "/home/tamas/caffe")
file (GLOB source_files "${source_dir}/ssd_video.cpp")
find_package(OpenCV 4.4.0 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
find_package(Caffe REQUIRED)
include_directories(${Caffe_INCLUDE_DIRS})
add_executable (main ${source_files})
target_link_libraries(main ${OpenCV_LIBS})
target_link_libraries(main ${Caffe_LIBRARIES})
The error is the following:
CMake Error at CMakeLists.txt:24 (find_package):
By not providing "FindCaffe.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Caffe", but
CMake did not find one.
Could not find a package configuration file provided by "Caffe" with any of
the following names:
CaffeConfig.cmake
caffe-config.cmake
Add the installation prefix of "Caffe" to CMAKE_PREFIX_PATH or set
"Caffe_DIR" to a directory containing one of the above files. If "Caffe"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
The problem is that I have searched and I don't have a FindCaffe.cmake file on my computer. I found an example for CaffeConfig.cmake, but I tried it and it doesn't work either.
Is there a way I can link Caffe with my C++ project? Thanks!
To fix this issue you may do the following:
Download this FindCAFFE.cmake file
Create cmake dir in your repo root directory and put the downloaded file there.
Modify your CMake file:
add set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
change set (Caffe_DIR "/home/tamas/caffe") to set (CAFFE_ROOT_DIR "/home/tamas/caffe")
change find_package(Caffe REQUIRED) to find_package(CAFFE REQUIRED)
use CAFFE_INCLUDE_DIRS and CAFFE_LIBRARIES for include directories and link libraries respectively
Clean up your build dir and run cmake command again
<library>_DIR should not be set manually in CMake code usually. There are better alternatives that should be used as setting these variable won't necessarily do what you want. It won't change where find_package finds its libraries.
The CaffeConfig.cmake file is generated when building Caffe. You should never download another one, these files are compatible only with a specific build configuration.
The Caffe library supports to be used with CMake, so FindCaffe.cmake is unnecessary.
For find_package to work, either set the <package>_ROOT variable (require CMake 3.12 minimum) or you must append the install path in CMAKE_PREFIX_PATH. Here's a CMake example that uses the prefix path:
# If you only built the library
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/build-dir")
# If you installed the library there
list (APPEND CMAKE_PREFIX_PATH "/home/tamas/caffe/")
find_package(Caffe REQUIRED)
Note that the Caffe_LIBRARIES and Caffe_INCLUDE_DIRS won't be set. This is old CMake style and the Caffe library uses the new style. This is what you should do:
target_link_libraries(main PUBLIC caffe caffeproto)
This line add both include directory and adds linking to the libraries too.

How do I include the Eigen library in a CMakelist.txt on windows

I am trying to include the Eigen library to my CMakelist.txt. I have followed the CMake instructions on the Eigen Docs but I am using Jetbrain's Clion and not CMake directly. So I do not know how to use the Cmake commands provided. I have researched around but I don't have have a very good understanding of CMake to write Cmakelists, so I haven't been able to get anything to work yet.
this is what I have been using just to test the serup of the library:
cmake_minimum_required(VERSION 3.17)
project(Eigen_Test)
set(CMAKE_CXX_STANDARD 20)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
add_executable (example example.cpp)
target_link_libraries (example eigen)
add_executable(Eigen_Test main.cpp)
this is the error I have been receiving:
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a package configuration file provided by "Eigen3" (requested
version 3.3) with any of the following names:
Eigen3Config.cmake
eigen3-config.cmake
Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been installed.
I Have researched many ways to include the library but most methods use command lines which I am unfamiliar with. Also I do not have an Eigen3Config.cmake the only file I have Eigen3Config.cmake.in. I assume there is some install trick that I must not be aware of. If anyone has a way to include clion strictly using a CMakelist.txt, I would be greatly appreciative.
Here a working example with CMake on Windows using the MinGW environment with mingw32-make.exe and g++.exe compiler.
CMakeLists.txt :
# The following lines depends on your project :
cmake_minimum_required(VERSION 3.19)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)
# You have to set these variables as Windows environment variables:
# EIGEN3_INCLUDE_DIR <- %EIGEN3_ROOT%
# EIGEN3_DIR <- %EIGEN3_ROOT%\cmake
#
# EIGEN3_INCLUDE_DIR: variable needed for file %EIGEN3_ROOT%/cmake/FindEigen3.cmake
#
# CMAKE_MODULE_PATH: Search path for the module Eigen3 to be loaded by find_package
#
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
SET( CMAKE_MODULE_PATH "$ENV{EIGEN3_DIR}" )
find_package( Eigen3 3.3 REQUIRED )
# include_directories is needed for the compiler to know where looking for Eigen3 header files to be included
include_directories( ${EIGEN3_INCLUDE_DIR} )
add_executable(PROJECT_NAME FILES...)
You can then call the Eigen3 libraries, such as:
#include <Eigen/Core>
Eigen is a header only library, so you don't have to add it to target_link_library, and you don't need a CMake Macro to detect it.
Instead just add the header file into your include path and you should be set.

CMake user built libraries; cannot specify link libraries for target

I'm building a project in Cpp that will communicate with my Java apps via rabbitmq and post updates to twitter. I'm using a few libraries from github
rabbitmq-c
Rabbit installed to /usr/local/lib64
jansson - json library
I installed this a while back for another project, went to /usr/local/lib
twitcurl - C lib for Twitter API
Got installed to /usr/local/lib
If it matters, I'm using CLion as my IDE, which displays jansson and rabbit under auto-complete when defining includes - so that's picking the libs off my system somehow
e.g.
#include <jansson.h>
#include <amqp.h>
I link them using the target_link_libraries(name libs...) and I see output saying
build$ cmake ..
CMake Error at CMakeLists.txt:30 (target_link_libraries):
Cannot specify link libraries for target "twitcurl" which is not built by
this project.
I set LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64
I try to set the CMAKE_LIBRARY_PATH to include usr/local/lib and lib64 but doesn't seem to have any effect. Here's my CMakeLists.txt file
#
# This is a CMake makefile. You can find the cmake utility and
# information about it at http://www.cmake.org
#
cmake_minimum_required(VERSION 2.6)
set(PROJECT_NAME twitterUpdater)
set(SOURCE_FILES main.cpp)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/usr/local/lib"
"/usr/local/lib64")
project(${PROJECT_NAME})
find_package(X11 REQUIRED)
find_package(OpenCV REQUIRED)
IF (X11_FOUND)
INCLUDE_DIRECTORIES(${X11_INCLUDE_DIR})
LINK_LIBRARIES(${X11_LIBRARIES})
ENDIF ( X11_FOUND )
IF (OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
link_libraries(${OpenCV_LIBS})
ENDIF(OpenCV_FOUND)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${project_name} twitcurl jansson rabbitmq)
What's confusing me is another project I have uses jansson by simply adding it here TARGET_LINK_LIBRARIES(${project_name} dlib jansson)
What did I miss?? Thanks
CMake variables are case sensitive, thus the variable ${project_name} results in an empty string. Use ${PROJECT_NAME} instead, i.e.:
target_link_libraries(${PROJECT_NAME} twitcurl jansson rabbitmq)
Running CMake with the flag --warn-uninitialized helps you detect mistakes like this.

Include Eigen library for Xcode project via CMake/CMakeLists.txt

I've got the following CMakeLists.txt (in my "project" folder) file for my project.
# define new project
PROJECT(SETUPMARKERTEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif(UNIX)
# Set static build for GLFW
SET(BUILD_SHED_LIBS OFF)
ADD_SUBDIRECTORY(ext/glfw-3.1.1)
# Set shared lib build for the rest
SET(BUILD_SHARED_LIBS ON)
# Find dependencies
SET(EIGEN_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/ext/Eigen-3.1.2")
FIND_PACKAGE(OpenCV REQUIRED)
# Set header and source files
SET(MAR_Test_SOURCES
src/main.cpp
src/MarkerTracker.h src/MarkerTracker.cpp
src/PoseEstimation.h src/PoseEstimation.cpp
)
# define executable
ADD_EXECUTABLE(${PROJECT_NAME} ${MAR_Test_SOURCES})
# define additional include directories and linking targets
INCLUDE_DIRECTORIES("ext/glfw-3.1.1/include" ${EIGEN_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
And my Eigen folder is in "project/ext/Eigen/3.1.2/Eigen/".
Somehow when I created my project for Xcode (in "project/buildXcode/" with Cmake .. -G "Xcode") and run it, Xcode throws me the error:
So I guess there is some error in my CMakeLists.txt, unfortunately I received that file and I'm new to CMake and thus didn't write it on my own nor am I very skilled with CMake.
Do you know what causes the error and can you fix the CMakeLists.txt that my project runs with the Eigen library?
Unfortunately it looks like windows is having no problem with this, whereas mac is bleating.
You just have to use
#include <Eigen/Dense>
instead of
#include <Eigen\Dense>
...pretty stupid error.