使用CMake时,链接器找不到OpenCV库 [英] Linker can not find OpenCV libraries when using CMake

查看:175
本文介绍了使用CMake时,链接器找不到OpenCV库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在/usr/local/lib中安装了OpenCV2.4.0的副本

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

奇怪的是,我的程序大量使用opencv,如果CMake在查找库时遇到麻烦,它应该抱怨比jsut更多的未定义引用.

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.

我尝试添加 我的cmake文件中的LINK_DIRECTORIES("/usr/local/lib"),但没有帮助. /usr/local/lib下还安装了另一个名为POCO的库.我的程序还链接到POCO库,但是CMake似乎可以轻松找到它们.

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.

如果我手动链接-L/usr/local/lib,它将正确链接而不会出现错误.

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

CMakeLists.txt看起来像这样

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!

推荐答案

如果您拥有CMake 2.8,建议使用find_package(OpenCV)加载库.

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

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} )

这篇关于使用CMake时,链接器找不到OpenCV库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆