CMake OpenCV 3.0.0 and building executables and libraries in Ubuntu

时间:2015-11-12 10:47:54

标签: opencv cmake

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

1 个答案:

答案 0 :(得分:1)

  
      
  1. 我怎么知道我正在使用OpenCV 3,而不是系统中存在其他版本的OpenCV?
  2.   

你的项目应该检查一下。

但通常项目只使用find_package命令来填充与3d-party库相关的填充变量。此命令执行所有必需的检查。在你的情况下,它可能是

find_package(OpenCV 3 REQUIRED)

调用,自动填充OpenCV_LIBSOpenCV_INCLUDE_DIRS个变量。默认情况下,此命令在默认路径中搜索OpenCV安装,但您可以使用cmake的参数调整搜索算法(因此,您无需更改{{1}当你在其他机器上构建项目时)。例如,这样

CMakeLists.txt

您可以指定OpenCV的精确安装路径。

  
      
  1. 编译文件background_substractor时,无法找到其关联的头文件...
  2.   

除了检查文件存在外,不能提出任何建议

cmake -DOpenCV_DIR=/usr/local/opencv-3.0.0 <source-dir>

但这也可能是3d问题的结果(见下文)。

  
      
  1. 最后,如果我评论头文件,我会遇到链接问题...
  2.   

您对 CMake目标的使用不正确。与制作目标(通常是文件)不同, CMake目标是简单名称。默认情况下,库/可执行目标的名称确定由此目标生成的库/可执行文件的文件名,但这可以更改。找到结果文件的目录可以使用/home/alberto/STT_People_Tracking/Sources/include/background_substractor.h 变量进行调整,其中CMAKE_<TYPE>_OUTPUT_DIRECTORY可以是<TYPE>ARCHIVELIBRARY,具体取决于目标类型。

正确的CMake脚本将是:

RUNTIME