Linker cannot find OpenCV libraries when using CMake

I have a copy of OpenCV2.4.0 installed in / usr / local / lib

My program compiled properly, but when calling the linker, it generated errors such as:

/home/zhouw/moos-ivp-zhouw/trunk/src/pATRTest/mst.cpp:661: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat const&)'
CMakeFiles/pATR.dir/mst.cpp.o:/home/zhouw/moos-ivp-zhouw/trunk/src/pATRTest/mst.cpp:675: more undefined references to `cv::_OutputArray::_OutputArray(cv::Mat const&)' 
collect2: ld returned 1 exit status
make[2]: *** [../bin/pATR] Error 1
make[1]: *** [src/pATRTest/CMakeFiles/pATR.dir/all] Error 2
make: *** [all] Error 2

It is strange that my program uses opencv heavily, if CMake has problems finding libraries, he should have complained much more about undefined references than jsut.

I tried adding LINK_DIRECTORIES ("/ usr / local / lib") in my cmake file, but that didn't help. There, under / usr / local / lib, another library is installed called POCO. My program also links to POCO libraries, but CMake has no problem finding them.

If I manually contact -L / usr / local / lib, it will link correctly without errors.

CMakeLists.txt is as follows

PROJECT(pATR)

#what files are needed?
SET(SRCS
spline.hpp
utils.hpp utils.cpp
mst.hpp mst.cpp
cluster.hpp cluster.cpp
target.hpp target.cpp
detector.hpp detector.cpp
classifier.hpp classifier.cpp
atr.hpp atr.cpp
MOOSAtr.h MOOSAtr.cpp
main.cpp
)

ADD_EXECUTABLE(pATR ${SRCS})

# indicate how to link
#LINK_DIRECTORIES("/usr/local/lib")
TARGET_LINK_LIBRARIES(pATR opencv_core opencv_highgui opencv_imgproc MOOS)

INSTALL(TARGETS
pATR
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
)

, ? !

+3
3

CMake 2.8, find_package(OpenCV) .

: http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

CMake:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
+3

, , CMake . CMake Makefile, .

CMake, ? .

g++ -o myopencvapp `pkg-config --cflags --libs opencv` myopencvapp.cpp`

g++.

+1

:

find_package(OPENCV COMPONENTS core imgproc highgui REQUIRED)

From the docs:

Component Packages

Some libraries are not monolithic, but come with one or more dependent libraries or components. A notable example of this is the Qt library, which, in particular, comes with the QtOpenGL and QtXml components. To use both of these components, use the find_package command:

find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)

You can also check the following link for more information.

https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-To-Find-Libraries

+1
source

All Articles