我有一个项目,在子目录中包含opencv的预构建版本。例如:
MyProject的
* CMakeLists.txt
* src
* third_party
** CMakeLists.txt
** opencv
****包括
**** lib
我想链接到位于third_party目录中的opencv版本。我的问题是,如何通知CMake链接到lib中预先构建的dylib文件,并在相关的opencv目录中包含标题?
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)
答案 0 :(得分:4)
我已经在具有XCode 7.0.1的OS X 10.11上尝试了CMake 3.3.2。
使用@Tsyvarev建议的link_directories()
和target_link_libraries()
方法似乎可以在不引发任何链接器警告或错误的情况下工作(它找到我放在.dylib
中的third_party
库目录)。
只是一个视图提示,希望可以帮助您了解为什么它不适用于您的Mac。
使用您的代码,我得到以下命令行链接器文件(在CMake的二进制输出目录中):
<强> 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本身会另外检查这些路径,并且它会更明显地链接到我的链接。以下是您的示例的修改版本:
<强>的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
选项已消失,您将获得“行”,如:
../third_party/opencv/lib/libopencv_calib3d.dylib
其他Q / A参考