我正在寫一個使用OpenCV的程序(安裝在本地目錄中,因爲我沒有該機器上的root權限),並且我寫了相應的CMakeLists.txt文件。我的問題是編譯在鏈接階段以不同的方式失敗(我花了三個小時嘗試網絡上提出的所有不同的解決方案,所以我看到了很多結果)。 給你的配置/結果這使我更有意義,即使它們導致了失敗: [在project_root /的CMakeLists.txt]:OpenCV和CMake鏈接問題
cmake_minimum_required(VERSION 2.8)
project(CUDA_learning)
set(OpenCV_INCLUDE_DIR "path/to/opencv_CUDA/include")
include_directories(${OpenCV_INCLUDE_DIR})
set(OpenCV_LIBS_DIR "path/to/opencv_CUDA/lib")
link_directories(${OpenCV_LIBS_DIR})
set(OpenCV_LIBS "opencv_core opencv_imgproc opencv_calib3d opencv_video opencv_features2d opencv_ml opencv_highgui opencv_objdetect opencv_contrib opencv_legacy opencv_gpu")
find_package(Boost COMPONENTS system filesystem program_options regex REQUIRED)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
else(Boost_FOUND)
message(FATAL_ERROR "Cannot build application without Boost. Please set Boost_INCLUDE_DIR.")
endif(Boost_FOUND)
set(CMAKE_BUILD_TYPE debug)
add_definitions("-Wall")
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/../bin)
subdirs (
../src
)
[中project_root/src目錄/的CMakeLists.txt] :
FILE(GLOB dir_source *.cc 2D/*.cc)
FILE(GLOB dir_header *.hh 2D/*.hh)
add_executable(${PROJECT_NAME} ${dir_source} ${dir_header})
target_link_libraries(${PROJECT_NAME} ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${OpenCV_LIBS})
結果:
Linking CXX executable ../../bin/CUDA_learning
c++: opencv_imgproc: No such file or directory
c++: opencv_calib3d: No such file or directory
c++: opencv_video: No such file or directory
c++: opencv_features2d: No such file or directory
c++: opencv_ml: No such file or directory
c++: opencv_highgui: No such file or directory
c++: opencv_objdetect: No such file or directory
c++: opencv_contrib: No such file or directory
c++: opencv_legacy: No such file or directory
c++: opencv_gpu: No such file or directory
如果相反,在網絡上給出的建議,我把 「-l」 OpenCV庫的名字,我之前得到:
Linking CXX executable ../../bin/CUDA_learning
/usr/bin/ld: cannot find -lopencv_core
collect2: ld returned 1 exit status
make[2]: *** [../bin/CUDA_learning] Error 1
make[1]: *** [src/CMakeFiles/CUDA_learning.dir/all] Error 2
make: *** [all] Error 2
有沒有人知道如何解決這件事?我從字面上瘋狂在這... 非常感謝提前! 乾杯, 羅布
嗨,感謝您的回答,我已經嘗試過,但仍然得到「無法找到-lopencv_core」錯誤... – Rob
嗯......如果將「subdirs」更改爲「add_subdirectory」 ? – DLRdave
我已經改變了它作爲 add_subdirectory( $ {} CMAKE_BINARY_DIR /../ SRC ) 但還是在結果沒有變化:( – Rob