使用cmake构建项目时OpenCV 3.2.0链接器错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cmake构建项目时OpenCV 3.2.0链接器错误相关的知识,希望对你有一定的参考价值。
我错误地删除了我的/ usr / local文件夹,之后我不得不再次构建OpenCV和Caffe。现在我的项目没有编译。每次我尝试使用cmake构建项目时都会出现错误,停止进度。
[ 80%] Built target mygoturn
[ 80%] Linking CXX executable ../bin/runTracker
/usr/bin/ld: CMakeFiles/runTracker.dir/src/visualizer/runTracker.cpp.o: undefined reference to symbol '_ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi'
//usr/local/lib/libopencv_imgcodecs.so.4.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/runTracker.dir/build.make:200: recipe for target '../bin/runTracker' failed
make[2]: *** [../bin/runTracker] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/runTracker.dir/all' failed
make[1]: *** [CMakeFiles/runTracker.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
我检查了命令pkg-config --libs --cflags opencv并得到以下响应:
-I/usr/include/opencv -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_datasets -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_line_descriptor -lopencv_optflow -lopencv_video -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_viz -lopencv_surface_matching -lopencv_text -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
我包含然后包含构建序列的CMakeLists.txt文件。
cmake_minimum_required(VERSION 3.6)
project(mygoturn)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
# avoid "can not find -lopencv_dep_cudart"
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
find_package( OpenCV REQUIRED )
message("Open CV version is ${OpenCV_VERSION}")
set(Caffe_DIR ~/tracking/GOTURN/caffe)
find_package(Caffe REQUIRED)
include_directories(${Caffe_INCLUDE_DIRS})
add_definitions(${Caffe_DEFINITIONS}) # ex. -DCPU_ONLY
message("Caffe_DIR is ${Caffe_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
add_library (${PROJECT_NAME}
src/network/regressor.cpp
src/network/regressor_base.cpp
src/tracker/tracker.cpp
src/helper/helper.cpp
src/helper/bounding_box.cpp
src/helper/image_proc.cpp
src/network/regressor.h
src/network/regressor_base.h
src/tracker/tracker.h
src/helper/helper.h
src/helper/bounding_box.h
src/helper/image_proc.h
)
include_directories(src)
set(SOURCE_FILES src/visualizer/runTracker.cpp src/loader/loader_base.cpp src/loader/loader_base.h)
add_executable(runTracker ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${Caffe_LIBRARIES} ${Boost_LIBRARIES})
target_link_libraries (runTracker ${PROJECT_NAME})
答案
在建立新版本的OpenCV库(opencv4)之后,我发现对highgui模块进行的实现更改很少,其中highgui模块上的一些实现已经转移到imgcodecs(主要是imread和imwrite) 。
更具体地设置CMAKE_CXX_FLAGS以包括所有库或特别是-lopencv_imgcodecs就足够了。
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lopencv_imgcodecs")
这恰好解决了我一直面临的问题。 pkg-config --libs opencv列出了所有在opencv4中构建的库模块。
以上是关于使用cmake构建项目时OpenCV 3.2.0链接器错误的主要内容,如果未能解决你的问题,请参考以下文章
使用mingw和cmake构建opencv时出错:“windres.exe:invalid option - W”
OpenCV3.2.0+opencv_contrib-3.2.0+VS2015+cmake