C++ 通过 cmake 链接作为外部项目包含的 opencv 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22598208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:08:24  来源:igfitidea点击:

linking opencv libraries included as an external project via cmake

c++opencvcmakestatic-linking

提问by Vyacheslav

I'm relatively new to cmake and after days of struggling couldn't figure out the following thing:

我对 cmake 比较陌生,经过几天的努力,我无法弄清楚以下事情:

I have a project that depends on opencv (which is a cmake project on its own) and I want to statically link opencv libraries. What I'm doing is I have a copy of opencv source code in my project and include it into my CMakeLists.txt via

我有一个依赖于 opencv 的项目(它本身就是一个 cmake 项目),我想静态链接 opencv 库。我正在做的是我的项目中有一份 opencv 源代码的副本,并通过以下方式将其包含到我的 CMakeLists.txt 中

ExternalProject_Add(my_copy_of_opencv_project
   CMAKE_ARGS -D BUILD_SHARED_LIBS=NO ...
   CMAKE_INSTALL_PREFIX=${MY_OPENCV_INSTALL_DIR} 
   SOURCE_DIR ${PATH_TO_OPENCV_SRCS} 
)

All built nicely and where I have problem is that I cannot reliably determine where the opencv libraries will be. E.g. on linux/mac there are in ${MY_OPENCV_INSTALL_DIR}/liband are named as libopencv_core.awhereas on 32-bit Windows with VS 2012 installed the libs are in ${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticliband for the Debug configuration the libs named like opencv_core247d.lib.

一切都很好,我遇到的问题是我无法可靠地确定 opencv 库的位置。例如,在 linux/mac 上有 in${MY_OPENCV_INSTALL_DIR}/lib并命名为 aslibopencv_core.a而在安装了 VS 2012 的 32 位 Windows 上,libs 位于${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticlibDebug 配置中,libs 命名为opencv_core247d.lib.

So the question is can I somehow obtain a list of all libraries produced by the OpenCV build (and the root lib folder) and link them via something like target_link_libraries(mylib opencv_core ...)?

所以问题是我能否以某种方式获得 OpenCV 构建(和根 lib 文件夹)生成的所有库的列表,并通过类似的方式链接它们target_link_libraries(mylib opencv_core ...)

Maybe I'm doing something wrong or overcomplicated. So what I basically want is to compile my embedded opencv source tree statically and link against its libraries in a "cross-plaformish" way.

也许我做错了什么或过于复杂。所以我基本上想要的是静态编译我的嵌入式 opencv 源代码树,并以“跨平台”的方式链接到它的库。

Any pointers are highly appreciated! Thanks!

任何指针都非常感谢!谢谢!

回答by maxint

The best solution to use cmake with OpenCV project is:

在 OpenCV 项目中使用 cmake 的最佳解决方案是:

  1. Compile OpenCV as shared / static libraries with OpenCV's cmake build system.
  2. In your project's CMakeLists.txt
  1. 使用 OpenCV 的 cmake 构建系统将 OpenCV 编译为共享/静态库。
  2. 在您项目的 CMakeLists.txt 中

For example (CMakeLists.txt):

例如(CMakeLists.txt):

cmake_minimum_required(VERSION 2.8.12)
project(test_project)

# OpenCV_DIR could also be read from system environment variable.
if(WIN32)
  set(OpenCV_DIR "d:/libs/opencv-2.4.8/build")
else()
  set(OpenCV_DIR "/usr/lib/opencv")
endif()
find_package(OpenCV REQUIRED COMPONENTS core imgproc highgui)

include_directories(${OpenCV_INCLUDE_DIRS}) # not needed for opencv>=4.0
add_executable(test main.cpp)
target_link_libraries(test ${OpenCV_LIBS})

It works in cross-platforms.

它适用于跨平台。