通过cmake链接作为外部项目包括在内的opencv库 [英] linking opencv libraries included as an external project via cmake

查看:194
本文介绍了通过cmake链接作为外部项目包括在内的opencv库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对cmake还是比较陌生,经过几天的苦苦挣扎后仍无法弄清以下事情:

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

我有一个依赖于opencv的项目(这本身就是一个cmake项目),我想静态链接opencv库.我正在做的是在我的项目中有一份opencv源代码的副本,并通过

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

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

所有构建都很好,而我有问题的地方是我无法可靠地确定opencv库的位置.例如.在linux/mac上,它们位于${MY_OPENCV_INSTALL_DIR}/lib中,并命名为libopencv_core.a;而在安装了VS 2012的32位Windows上,库位于${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticlib中,而对于Debug配置,库则名为opencv_core247d.lib.

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}/lib and are named as libopencv_core.a whereas on 32-bit Windows with VS 2012 installed the libs are in ${MY_OPENCV_INSTALL_DIR}/lib/x86/vc11/staticlib and for the Debug configuration the libs named like opencv_core247d.lib.

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

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源代码树,并以跨平台"的方式链接到其库.

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.

任何指针都受到高度赞赏!谢谢!

Any pointers are highly appreciated! Thanks!

推荐答案

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

The best solution to use cmake with OpenCV project is:

  1. 使用OpenCV的cmake构建系统将OpenCV编译为共享/静态库.
  2. 在项目的CMakeLists.txt中

例如(CMakeLists.txt):

For example (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.

这篇关于通过cmake链接作为外部项目包括在内的opencv库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆