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()
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).
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
/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
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})
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
Additional Q/A References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With