gpt4 book ai didi

opencv - CMake OpenCV 3.0.0 和在 Ubuntu 中构建可执行文件和库

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:13 24 4
gpt4 key购买 nike

我在/usr/local/opencv-3.0.0 中安装了 OpenCV 3.0.0

我正在尝试构建一个 CMakeLists 文件来针对此 OpenCV 3.0.0 构建一个库。 CMakeLists.txt如下:

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

这是一个相当简单的 Cmake 文件,但是,我有以下问题/疑问:1.-我怎么知道我使用的是 OpenCV 3,而不是系统中存在的其他版本的 OpenCV?2.- 编译文件 background_substractor 时,无法找到其关联的头文件,尽管我已经检查了路径并且它在 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.- 最后,如果我注释头文件,我在链接时遇到问题:

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

我已经尝试了所有方法:在 add_executable() 和 add_library() 命令中指定包含文件,我检查了路径并且它们没问题,等等。

谁能对 CMake 更有经验,帮帮我?非常感谢您,

阿尔贝托

最佳答案

  1. How can I know I am using OpenCV 3, and not other versions of OpenCV present in the system?

您的项目应该检查这一点。

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

find_package(OpenCV 3 REQUIRED)

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

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

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

  1. When compiling the file background_substractor, its associated header file can not be located...

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

/home/alberto/STT_People_Tracking/Sources/include/background_substractor.h

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

  1. Finally, and if I comment the header file, I have problems linking...

您对CMake 目标 的使用不正确。与通常是文件的 make 目标 不同,CMake 目标是简单的名称。默认情况下,库/可执行目标的名称 确定由此目标生成的库/可执行文件的文件名,但这可以更改。结果文件所在的目录可以使用 CMAKE_<TYPE>_OUTPUT_DIRECTORY 进行调整。变量,其中 <TYPE>可以是ARCHIVE , LIBRARYRUNTIME取决于目标类型。

正确的 CMake 脚本应该是:

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

关于opencv - CMake OpenCV 3.0.0 和在 Ubuntu 中构建可执行文件和库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com