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
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)
I've given your example a try with CMake 3.3.2 on OS X 10.11 having XCode 7.0.1.
Using the
link_directories()
andtarget_link_libraries()
approach suggested by @Tsyvarev seems to work without raising any linker warnings or errors (it finds the.dylib
libraries I placed in thethird_party
directory).Just a view hints, that hopefully could get you a start why it's not working on your Mac.
With your code I get the following command line linker file (inside CMake's binary output directory):
CMakeFiles/myproject.dir/src/link.txt
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
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:Additional Q/A References