CMake链接器找不到opencv库 [英] CMake linker can not find opencv libraries
问题描述
我有一个安装在/ 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),但没有帮助。
还有一个名为POCO的库也安装在/ usr / local / lib下。我的程序还链接到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屋!