如何配置cmake链接到预构建的共享库? [英] How do I configure cmake to link to prebuilt shared libraries?

查看:106
本文介绍了如何配置cmake链接到预构建的共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,该项目在子目录中包含opencv的预构建版本。例如:

I have a project that includes a prebuilt version of opencv in a subdirectory. For example:

MyProject

* CMakeLists.txt

* src

* third_party

** CMakeLists.txt

** opencv

**** include

**** lib

MyProject
* CMakeLists.txt
* src
* third_party
** CMakeLists.txt
** opencv
**** include
**** lib

我想链接到third_party目录中的opencv版本。我的问题是,如何通知CMake链接到lib中的预构建dylib文件,并将标头包含在相关的opencv目录中?

I would like to link against the version of opencv located in the third_party directory. My question is, how do I inform CMake to link to the prebuilt dylib files in lib, and include the headers in the relevant opencv directory?

cmake_minimum_required(VERSION 2.8.9)
project (myproject)

include_directories(${CMAKE_SOURCE_DIR}/third_party/opencv/include)
link_directories(${CMAKE_SOURCE_DIR}/third_party/opencv/lib)

file(GLOB SOURCES "*.cpp")

add_executable(myproject ${SOURCES})
target_link_libraries(myproject opencv_calib3d opencv_contrib opencv_core opencv_highgui opencv_features2d opencv_highgui opencv_imgproc)


推荐答案

我给出了您的示例在具有XCode 7.0.1的OS X 10.11上尝试使用CMake 3.3.2。

I've given your example a try with CMake 3.3.2 on OS X 10.11 having XCode 7.0.1.

使用 link_directories() target_link_libraries() 方法似乎可以在不引发任何链接程序警告或错误的情况下工作(它找到。 dylib 库放置在 third_party 目录中。)

Using the link_directories() and target_link_libraries() approach suggested by @Tsyvarev seems to work without raising any linker warnings or errors (it finds the .dylib libraries I placed in the third_party directory).

只是一个视图提示,希望可以为您提供一个为什么Mac上无法使用它的开始。

Just a view hints, that hopefully could get you a start why it's not working on your Mac.


  • 使用您的代码,我得到了以下命令行链接文件(在CMake的二进制输出目录中):

  • With your code I get the following command line linker file (inside CMake's binary output directory):

CMakeFiles / myproject.dir / src / link.txt

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++    
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk 
-Wl,-search_paths_first 
-Wl,-headerpad_max_install_names  
CMakeFiles/myproject.dir/src/main.cpp.o  -o myproject  
-L[...CMAKE_SOURCE_DIR...]/third_party/opencv/lib  
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_highgui -lopencv_features2d -lopencv_highgui -lopencv_imgproc -lopencv_features2d -lopencv_imgproc 
-Wl,-rpath,[...CMAKE_SOURCE_DIR...]/third_party/opencv/lib 


  • 您可以尝试提供完整的库路径,因为这些由CMake本身及其附加检查更明显地体现了我所针对的内容。这是示例的修改版本:

  • You can try to give full library paths, because those are additionally checked by CMake itself and it gets more obvious what I link against. Here is a modified version of your example:

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.8.9)
    project (myproject)
    
    include_directories(${CMAKE_SOURCE_DIR}/third_party/opencv/include)
    
    file(GLOB SOURCES "src/*.cpp")
    file(GLOB LIBRARIES "third_party/opencv/lib/*.dylib")
    message("LIBRARIES = ${LIBRARIES}")
    
    add_executable(myproject ${SOURCES})
    target_link_libraries(myproject ${LIBRARIES})  
    

    使用此CMake仅将完全限定的路径(相对于我的二进制输出目录)添加到链接器文件中。 -L -l <​​/ code>选项消失了,您会看到 line,例如:

    With this CMake just adds fully qualified paths (relative to my binary output directory) into the linker file. The -L and -l options are gone and you get "lines" like:

    ../third_party/opencv/lib/libopencv_calib3d.dylib 
    


  • 其他Q / A参考

    • OpenCV installation on Mac OS X
    • How to use dylib file in application?
    • CMake link_directories from library
    • Force CMake to use the full library path

    这篇关于如何配置cmake链接到预构建的共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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