CMake OpenCV 3.0.0 and building executables and libraries in Ubuntu - opencv

I have OpenCV 3.0.0 installed in /usr/local/opencv-3.0.0
I am trying to construct a CMakeLists file to build a library against this OpenCV 3.0.0. The CMakeLists.txt is as follows:
cmake_minimum_required(VERSION 2.8)
project(STT_People_Tracker)
cmake_policy(SET CMP0016 NEW)
# compilation mode setup
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
# set OpenCV directories - CHANGE DEPENDING ON SYSTEM
set(OpenCV_PATH "/usr/local/opencv-3.0.0")
set(OpenCV_INCLUDE_DIRS "${OpenCV_PATH}/include")
set(OpenCV_LIBS "${OpenCV_PATH}/lib/")
# set environment variables
set(SOURCES_PATH "${CMAKE_SOURCE_DIR}/Sources")
set(INCLUDES_PATH "${SOURCES_PATH}/include")
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(OUTPUT_PATH "../Debug")
message(STATUS "Compiling in DEBUG mode")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(OUTPUT_PATH "../Release")
message(STATUS "Compiling in RELEASE mode")
endif()
include_directories(${INCLUDES_PATH})
include_directories(${OpenCV_INCLUDE_DIRS})
# compilation of executables
message(STATUS "configuring executables...")
add_executable(${OUTPUT_PATH}/mainTest ${SOURCES_PATH}/mainTest.cpp)
# compilation of libraries
message(STATUS "configuring libraries...")
add_library(${OUTPUT_PATH}/background_substractor ${SOURCES_PATH}/background_substractor.cpp)
# set linker options
link_directories(${OpenCV_LIBS})
target_link_libraries(${OUTPUT_PATH}/mainTest opencv_core opencv_highgui)
target_link_libraries(${OUTPUT_PATH}/background_substractor opencv_core opencv_highgui)
message(STATUS "cmake configuration complete")
It is a fairly simple Cmake file, however, I have the following problems/doubts:
1.-How can I know I am using OpenCV 3, and not other versions of OpenCV present in the system?
2.- When compiling the file background_substractor, its associated header file can not be located, although I have checked the path and it is correctly assigned in the set(INCLUDES_PATH "${SOURCES_PATH}/include"):
/home/alberto/STT_People_Tracking/Sources/background_substractor.cpp:3:36: fatal error: background_substractor.h: No such file or directory
#include "background_substractor.h"
^
compilation terminated.
make[2]: *** [CMakeFiles/../Debug/background_substractor.dir/Sources/background_substractor.cpp.o] Error 1
make[1]: *** [CMakeFiles/../Debug/background_substractor.dir/all] Error 2
make: *** [all] Error 2
3.- Finally, and if I comment the header file, I have problems linking:
Linking CXX static library lib../Debug/background_substractor.a
/usr/bin/ar: lib../Debug/background_substractor.a: No such file or directory
make[2]: *** [lib../Debug/background_substractor.a] Error 1
make[1]: *** [CMakeFiles/../Debug/background_substractor.dir/all] Error 2
make: *** [all] Error 2
I have tried everything: Specifying the include files in the add_executable() and add_library() commands, I have checked paths and they are ok, etc etc.
Could anyone more experienced with CMake, give me a little hand?
Thank you very much in advance,
Alberto

How can I know I am using OpenCV 3, and not other versions of OpenCV present in the system?
Your project should check that.
But usually projects just use find_package command for fill variables related to 3d-party library. This command perform all needed checks. In your case it could be
find_package(OpenCV 3 REQUIRED)
call, which fills OpenCV_LIBS and OpenCV_INCLUDE_DIRS variables automatically. This command, by default, search OpenCV installation in default paths, but you can adjust searching algorithm by using parameters to cmake (so, you needn't to change CMakeLists.txt when you build the project on other machine). E.g., this way
cmake -DOpenCV_DIR=/usr/local/opencv-3.0.0 <source-dir>
you can specify precise installation path of OpenCV.
When compiling the file background_substractor, its associated header file can not be located...
Cannot suggest anything aside from checking file existence
/home/alberto/STT_People_Tracking/Sources/include/background_substractor.h
But this also can be a result of the 3d issue(see below).
Finally, and if I comment the header file, I have problems linking...
Your usage of CMake targets is incorrect. Unlike to make targets, which usually are files, CMake targets are simple names. By default, name of library/executable target determines filename of the library/executable file, produced by this target, but this can be changed. Directory, where resulted file will be located, can be adjusted using CMAKE_<TYPE>_OUTPUT_DIRECTORY variables, where <TYPE> can be ARCHIVE, LIBRARY or RUNTIME depending on target type.
Correct CMake script would be:
cmake_minimum_required(VERSION 2.8)
project(STT_People_Tracker)
cmake_policy(SET CMP0016 NEW)
# compilation mode setup
#set(CMAKE_BUILD_TYPE Release)
set(CMAKE_BUILD_TYPE Debug)
# set OpenCV directories using find_package.
find_package(OpenCV 3 REQUIRED)
# set environment variables
set(SOURCES_PATH "${CMAKE_SOURCE_DIR}/Sources")
set(INCLUDES_PATH "${SOURCES_PATH}/include")
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Debug") # Use absolute path
message(STATUS "Compiling in DEBUG mode")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(OUTPUT_PATH "${CMAKE_BINARY_DIR}/Release")
message(STATUS "Compiling in RELEASE mode")
endif()
# Set output directory for STATIC libraries and executables
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${OUTPUT_PATH})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${OUTPUT_PATH})
include_directories(${INCLUDES_PATH})
include_directories(${OpenCV_INCLUDE_DIRS})
# compilation of executables
message(STATUS "configuring executables...")
add_executable(mainTest ${SOURCES_PATH}/mainTest.cpp) # Use simple name as a target
# compilation of libraries
message(STATUS "configuring libraries...")
add_library(background_substractor ${SOURCES_PATH}/background_substractor.cpp) # Use simple name as a target
# set linker options
# Command below is no-op: OpenCV libraries enumerated using absolute paths
# link_directories(${OpenCV_LINK_DIRECTORIES})
target_link_libraries(mainTest ${OpenCV_LIBS}) # Variable OpenCV_LIBS contains OpenCV libraries needed to link with
target_link_libraries(background_substractor ${OpenCV_LIBS})
message(STATUS "cmake configuration complete")

Related

CMake could not find OpenCV library like cv.h

I'd wanted to run the project in Ubuntu PC, but I received an error.
When I ran the following code;
make
And, There is a error in following;
asiye#asiye:~/Desktop/cmfd_framework/build$ make
Consolidate compiler generated dependencies of target common
[ 8%] Built target common
[ 10%] Building CXX object ground_truth_db/CMakeFiles/cmfdgt.dir/command_ground_truth.cpp.o
/home/asiye/Desktop/cmfd_framework/ground_truth_db/command_ground_truth.cpp:10:10: fatal error: cv.h: No such file or directory
10 | #include "cv.h"
| ^~~~
compilation terminated.
make[2]: * [ground_truth_db/CMakeFiles/cmfdgt.dir/build.make:76: ground_truth_db/CMakeFiles/cmfdgt.dir/command_ground_truth.cpp.o] Error 1
make[1]: * [CMakeFiles/Makefile2:205: ground_truth_db/CMakeFiles/cmfdgt.dir/all] Error 2
make: * [Makefile:91: all] Error 2
I think Cmake could not find OpenCV library but why ?
Could you explain to me if there is another way to address this problem?
Best Regards,
Cmake Dosyası burada Yunus Temurlenk
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(Vole)
set(Vole_CMFD ON)
set(Vole_CMFD_Ground_Truth ON)
set(Vole_Shell ON)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include(VoleHelperMacros)
include(VoleModuleMacros)
if(WIN32)
add_definitions(-DNOMINMAX) # prevents min- and max-macro brainf*ck in VS 2003
endif(WIN32)
# Global options
option(VOLE_CMAKE_DEBUG_OUTPUT "Show additonal cmake debug output." OFF)
mark_as_advanced(VOLE_CMAKE_DEBUG_OUTPUT)
option(VOLE_CHECK_DEPENDENCIES "Do additional dependency check with nice error messages." ON)
mark_as_advanced(VOLE_CHECK_DEPENDENCIES)
option(VOLE_SINGLE_TARGETS "Build single targets for each command." OFF)
set(VOLE_MODULE_DIRECTORIES "" CACHE STRING "Semicolon-separated list of module directories for vole, e.g. the forensics or the color code.")
# maybe there is a better place for these two options:
option(Boost_NO_SYSTEM_PATHS "Suppress searching in system paths or other locations outside of BOOST_ROOT." OFF)
set(BOOST_ROOT "" CACHE STRING "The preferred installation prefix for searching for Boost. Set this if the module has problems finding the proper Boost installation.")
# Global variables
set(VOLE_PACKAGE_LIST "" CACHE INTERNAL "Package list" FORCE)
set(VOLE_EXTERNAL_SUBDIRECTORIES "" CACHE INTERNAL "External subdirectories" FORCE)
set(VOLE_MODULE_LIST "" CACHE INTERNAL "Module list" FORCE)
include(VoleFindPackages)
# include(VoleFindExternalModules)
add_subdirectory(common core/common)
include_directories(common shell cmfd ground_truth_db)
add_subdirectory(cmfd cmfd)
add_subdirectory(ground_truth_db ground_truth_db)
add_subdirectory(shell core/shell)
include(VoleCheckDependencies)
INCLUDE("CMakeModules/TargetDoc.cmake" OPTIONAL)

using vcpkg+cmake+opencv generate a project, but encounter fatal error: “opencv2/core/core.hpp”: No such file or directory

I download OpencV using VCPKG:
./vcpkg install opencv2:x64-windows
Computing installation plan...
The following packages are already installed:
opencv2[core,eigen,jpeg,png,tiff]:x64-windows -> 2.4.13.7#5
Package opencv2:x64-windows is already installed
Total elapsed time: 2.225 ms
The package opencv2:x64-windows provides CMake targets:
find_package(OpenCV CONFIG REQUIRED)
# Note: 15 target(s) were omitted.
target_link_libraries(main PRIVATE opencv_ml opencv_ts opencv_gpu opencv_ocl)
and write cmakelists like this:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_TOOLCHAIN_FILE E:/vcpkg/scripts/buildsystems/vcpkg.cmake
CACHE STRING "Vcpkg toolchain file")
# set the project name
project(QRCODE)
# add the executable
add_executable(qrcode main.cpp)
find_package(OpenCV COMPONENTS REQUIRED)
# Additional Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Additional Library Directories
link_directories( ${OpenCV_LIB_DIR} )
# Additional Dependencies
target_link_libraries(qrcode ${OpenCV_LIBS})
message("${OpenCV_DIR}")
message("${OpenCV_INCLUDE_DIRS}")
and what puzzles me is that cmake output looks like this:
E:/vcpkg/installed/x64-windows/share/opencv
/include/opencv;/include
it seems like opencv was not installed successfully, or cmake couldn't find opencv install path.
When I built the project using cmake tool, encountered with fatal error: “opencv2/core/core.hpp”: No such file or directory.

Receiving error "include could not find load file" for cmake

Setting up swift-corelibs-xctest for Swift - Windows 10. When file CMakeLists.txt is called find_package is ran to look for FoundationConfig.cmake file
if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
find_package(dispatch CONFIG REQUIRED)
find_package(Foundation CONFIG REQUIRED)
endif()
FoundationConfig.cmake is found but error "include could not find load file: #Foundation_EXPORTS_FILE#"
I'm pretty sure this is due to fact that Foundation file is in a different directory. I only need clarification as to what # Foundation_EXPORTS_FILE# means. I have been unable to find any reference to _EXPORTS_FILE.
Believe I found part of what I was lookin for here.
https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/ExportInterface
The other half of the problem was found here -
default search paths for CMake include() vs. find_package()
"include(#Foundation_EXPORTS_FILE#)" Was in reference to the two config files, which are not in the same location, also include() searches for files in CMAKE_MODULE_PATH --- message("Path- ${CMAKE_MODULE_PATH}")

PCL can't find header file

/home/ubuntu/emvs_ws/src/rpg_emvs/mapper_emvs/src/realtime.cpp:25:62: fatal error: pcl/registration/impl/incremental_registration.hpp: No such file or directory
compilation terminated.
rpg_emvs/mapper_emvs/CMakeFiles/realtime_emvs.dir/build.make:62: recipe for target 'rpg_emvs/mapper_emvs/CMakeFiles/realtime_emvs.dir/src/realtime.cpp.o' failed
make[2]: *** [rpg_emvs/mapper_emvs/CMakeFiles/realtime_emvs.dir/src/realtime.cpp.o] Error 1
I'm getting this error after catkin_make
Theses are the includes in realtime.cpp
#include <pcl/registration/incremental_registration.h>
#include <pcl/registration/icp.h>
CMakeLists.txt file
project(mapper_emvs)
cmake_minimum_required(VERSION 2.8.3)
find_package(catkin_simple REQUIRED)
catkin_simple(ALL_DEPS_REQUIRED)
set(CMAKE_BUILD_TYPE RelWithDebInfo) # Release, RelWithDebInfo
set(CMAKE_CXX_FLAGS "-O3 -fopenmp -std=c++11 ${CMAKE_CXX_FLAGS}")
## Generate dynamic reconfigure parameters in the 'cfg' folder
##generate_dynamic_reconfigure_options(
## cfg/EMVSCfg.cfg
##)
set(HEADERS
include/mapper_emvs/mapper_emvs.hpp
include/mapper_emvs/data_loading.hpp
include/mapper_emvs/depth_vector.hpp
include/mapper_emvs/trajectory.hpp
include/mapper_emvs/geometry_utils.hpp
include/mapper_emvs/median_filtering.hpp
)
set(SOURCES
src/mapper_emvs.cpp
src/data_loading.cpp
src/median_filtering.cpp
)
option(DEFINE_USE_INVERSE_DEPTH "Use linear spacing in inverse depth (if OFF, will use linear spacing in depth)" ON)
if(DEFINE_USE_INVERSE_DEPTH)
add_definitions(-DUSE_INVERSE_DEPTH)
endif(DEFINE_USE_INVERSE_DEPTH)
cs_add_library(${PROJECT_NAME} ${SOURCES} ${HEADERS})
# Executables
################################################################################
cs_add_executable(run_emvs src/main.cpp)
target_link_libraries(run_emvs ${PROJECT_NAME})
cs_add_executable(realtime_emvs src/realtime.cpp)
add_dependencies(realtime_emvs ${PROJECT_NAME}_gencfg)
target_link_libraries(realtime_emvs ${PROJECT_NAME})
################################################################################
cs_install()
cs_export()
Note that I did Catkin_make not catkin build, I don;t know if this makes a difference.
I'm asked to add more details, so please ignore this line :)
You need to add PCL library in your CMakeLists.txt file as follows:
project(mapper_emvs)
cmake_minimum_required(VERSION 2.8.3)
find_package(catkin_simple REQUIRED)
# Adding PCL lib
find_package(PCL REQUIRED)
# Setting include, lib directories and definitions
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS} )
add_definitions(${PCL_DEFINITIONS} )
catkin_simple(ALL_DEPS_REQUIRED)
set(CMAKE_BUILD_TYPE RelWithDebInfo) # Release, RelWithDebInfo
set(CMAKE_CXX_FLAGS "-O3 -fopenmp -std=c++11 ${CMAKE_CXX_FLAGS}")
## Generate dynamic reconfigure parameters in the 'cfg' folder
##generate_dynamic_reconfigure_options(
## cfg/EMVSCfg.cfg
##)
set(HEADERS
include/mapper_emvs/mapper_emvs.hpp
include/mapper_emvs/data_loading.hpp
include/mapper_emvs/depth_vector.hpp
include/mapper_emvs/trajectory.hpp
include/mapper_emvs/geometry_utils.hpp
include/mapper_emvs/median_filtering.hpp
)
set(SOURCES
src/mapper_emvs.cpp
src/data_loading.cpp
src/median_filtering.cpp
)
option(DEFINE_USE_INVERSE_DEPTH "Use linear spacing in inverse depth (if OFF, will use linear spacing in depth)" ON)
if(DEFINE_USE_INVERSE_DEPTH)
add_definitions(-DUSE_INVERSE_DEPTH)
endif(DEFINE_USE_INVERSE_DEPTH)
cs_add_library(${PROJECT_NAME} ${SOURCES} ${HEADERS})
# Executables
################################################################################
cs_add_executable(run_emvs src/main.cpp)
target_link_libraries(run_emvs ${PROJECT_NAME} ${PCL_LIBRARIES})
cs_add_executable(realtime_emvs src/realtime.cpp)
add_dependencies(realtime_emvs ${PROJECT_NAME}_gencfg)
target_link_libraries(realtime_emvs ${PROJECT_NAME} ${PCL_LIBRARIES})
################################################################################
cs_install()
cs_export()
Note: Hope you installed PCL lib. Besides,
I added ${PCL_LIBRARIES} into target_link_libraries. I hope it will work this way, if you are getting some errors, try adding ${PCL_LIBRARIES} into cs_add_library(${PROJECT_NAME} ${SOURCES} ${HEADERS} ${PCL_LIBRARIES}). Please let us know if get errors after applying these changes.

CMake Generated Eclipse CDT Project Does Not Have System Includes

My problem is similar with this: http://www.eclipse.org/forums/index.php/m/649323/
I created a cmake project, and used
cmake .. -G "Eclipse CDT4 - Unix Makefiles"
to create a Eclipse CDT4 project.
But in the CDT IDE, the standard include paths are not listed, and all STL or system build-in header files include directives are marked as "cannot be resolved", so the "Open Declaration" or other a lot of operation cannot be done.
However, I could compile it without any problems.
My co-worker also has a cmake project, but it's very complicated. The CDT project generated from his cmake project DOES have the system includes. But his cmake is way too complicated, and he told me that he didn't do anything special to include the system paths.
Can anyone help me out? Thanks.
My Main CMakeLists.txt:
CMake_Minimum_Required(VERSION 2.8)
# Some settings
Set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
CMake_Policy(SET CMP0015 NEW)
#Include(CMakeProcedures.cmake)
#CheckEnvironment()
# Set the compiler and its version if needed
# Create the project
Project(MyProjectName CXX)
# Set the compiler
Set(CMAKE_CXX_COMPILER /usr/bin/g++)
# Detect whether we are in-source
If (CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
Message(FATAL_ERROR "In-source building is not allowed! Please create a 'build' folder and then do 'cd build; cmake ..'")
EndIf()
# Set the output dirs
Set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
Set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# Add source subdirs to the build
Add_Subdirectory(src)
# Add_Subdirectory(test EXCLUDE_FROM_ALL)
Peter
One workaround is to manually add these to the CDT IDE:
/usr/include/c++/4.5
/usr/include/c++/4.5/backward
/usr/include/c++/4.5/i686-linux-gnu
/usr/include/i386-linux-gnu
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/include-fixed
/usr/local/include
But it's not the solution.
I finally figured out that this line is causing the problem:
Project(MyProjectName CXX)
If we remove the optional paramter CXX, life is good.
Can anyone tell me why?
Peter

Resources