Linker can not find OpenCV libraries when using CM

2020-03-04 04:23发布

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

My program compiled properly but when the linker is evoked, it gave 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

The strange thing is my program uses opencv intensively, if CMake has trouble finding the libraries, it should have complained a lot more undefined references than jsut a few.

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

If I manually link with -L/usr/local/lib, it would link properly without error.

The CMakeLists.txt looks like this

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
)

Any idea what's going on? Many thanks!

标签: opencv cmake
3条回答
We Are One
2楼-- · 2020-03-04 04:55

If you have CMake 2.8, I recommend using find_package(OpenCV) to load the libraries.

There is an example at http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

The CMake file:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
查看更多
forever°为你锁心
3楼-- · 2020-03-04 04:57

You're after:

find_package(OPENCV COMPONENTS core imgproc highgui REQUIRED)

From the docs:

Packages with components

Some libraries are not monolithic, but come with one or more dependent libraries or components. A notable example for this is the Qt library, which ships (among others) with the components QtOpenGL and QtXml. To use both of these components, use the following the find_package command:

find_package(Qt COMPONENTS QtOpenGL QtXml REQUIRED)

also, for more info you may check out the following link.

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

查看更多
等我变得足够好
4楼-- · 2020-03-04 05:17

I am not sure if it makes sense that CMake can't find the linking libraries. CMake finds your dependencies and generates the Makefile, but it doesn't actually compile and link for you.

Your error are not from CMake, right? They are from make.

I always link manually with this

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

when invoking g++.

查看更多
登录 后发表回答