OpenCV教程之使用cmake生成MakeFile时下载文件

biersibao 2017-02-28

在编译OpenCV以及其附加模块时,有时会需要一些第三方的库,如果本地没有,会自动下载,下载地址一般为GitHub,结果当然就是卡死在那里,根本无法下载,下面教大家如何解决这种问题。

问题重现

比如我在使用cmake配置的过程中出现以下情况:

Call Stack (most recent call first):
  /home/ryan/build/opencv_contrib-master/modules/dnn/cmake/download_protobuf.cmake:23 (ocv_download)
  /home/ryan/build/opencv_contrib-master/modules/dnn/cmake/download_protobuf.cmake:51 (ocv_protobuf_download)
  /home/ryan/build/opencv_contrib-master/modules/dnn/cmake/OpenCVFindLibProtobuf.cmake:30 (include)
  /home/ryan/build/opencv_contrib-master/modules/dnn/CMakeLists.txt:38 (include)


-- Downloading v3.1.0/protobuf-cpp-3.1.0.tar.gz...

解决思路

从上面的日志中可以得到以下信息:

  1. 需要下载protobuf-cpp-3.1.0.tar.gz这个文件
  2. 最先报错的是download_protobuf.cmake这个文件

一般情况下,最先报错的那个文件能获得一些与错误相关的信息,于是,我们打开这个文件,如下

set(PROTOBUF_CPP_NAME "libprotobuf")
set(PROTOBUF_CPP_DOWNLOAD_HASH "bd5e3eed635a8d32e2b99658633815ef")
set(PROTOBUF_CPP_PATH "${CMAKE_CURRENT_BINARY_DIR}/3rdparty/protobuf/sources") # /protobuf-3.1.0 subdirectory

set(OPENCV_PROTOBUF_CPP_DOWNLOAD_URL ${OPENCV_PROTOBUF_URL};$ENV{OPENCV_PROTOBUF_URL};https://github.com/google/protobuf/releases/download/)

function(ocv_protobuf_download file ID)
  if(DEFINED ${ID}_DOWNLOADED_HASH
       AND ${ID}_DOWNLOADED_HASH STREQUAL ${ID}_DOWNLOAD_HASH
       AND EXISTS ${${ID}_PATH})
    # Files have been downloaded and checked by the previous CMake run
    return()
  else()
    if(EXISTS ${${ID}_PATH})
      message(STATUS "${${ID}_NAME}: Removing previous unpacked files: ${${ID}_PATH}")
      file(REMOVE_RECURSE ${${ID}_PATH})
    endif()
  endif()
  unset(${ID}_DOWNLOADED_HASH CACHE)

  file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/.download)
  file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/.download/.gitignore" "*\n")
  ocv_download(PACKAGE ${file}
               HASH ${${ID}_DOWNLOAD_HASH}
               URL ${OPENCV_${ID}_DOWNLOAD_URL}
               DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/.download)
  set(${ID}_ARCHIVE "${DOWNLOAD_PACKAGE_LOCATION}")

  ocv_assert(EXISTS "${${ID}_ARCHIVE}")
  ocv_assert(NOT EXISTS "${${ID}_PATH}")
  file(MAKE_DIRECTORY ${${ID}_PATH})
  ocv_assert(EXISTS "${${ID}_PATH}")
  file(WRITE "${${ID}_PATH}/.gitignore" "*\n")

  message(STATUS "${${ID}_NAME}: Unpacking ${file} to ${${ID}_PATH}...")
  execute_process(COMMAND ${CMAKE_COMMAND} -E tar xz "${${ID}_ARCHIVE}"
                  WORKING_DIRECTORY "${${ID}_PATH}"
                  RESULT_VARIABLE __result)

  if(NOT __result EQUAL 0)
    message(FATAL_ERROR "${${ID}_NAME}: Failed to unpack ${ID} archive from ${${ID}_ARCHIVE} to ${${ID}_PATH} with error ${__result}")
  endif()

  ocv_assert(EXISTS "${${ID}_PATH}")

  set(${ID}_DOWNLOADED_HASH "${${ID}_DOWNLOAD_HASH}" CACHE INTERNAL "${ID} hash")

  #message(STATUS "${${ID}_NAME}: Successfully downloaded")
endfunction()

ocv_protobuf_download(v3.1.0/protobuf-cpp-3.1.0.tar.gz PROTOBUF_CPP)

可以看出,他需要libprotobuf这个库,并且需要下载到 ${CMAKE_CURRENT_BINARY_DIR}/3rdparty/protobuf/sources 目录下,而下载地址就是 https://github.com/google/protobuf/releases/download/ ,于是我们只要手动下载,然后把文件放到这个目录下即可。

OpenCV的详细介绍:请点这里
OpenCV的下载地址:请点这里

相关推荐

greent00 / 0评论 2020-05-29