OPENCV错误:对“cvLoadImage”的未定义引用

时间:2013-06-29 21:20:08

标签: c++ opencv makefile cmake

我有两个文件:

test.cpp:

#include "highgui.h"
#include <cv.h>
int main( int argc, char** argv ) {
    IplImage* img = cvLoadImage( argv[1] );
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    cvShowImage( "Example1", img );
    cvWaitKey(0);
    cvReleaseImage( &img );
    cvDestroyWindow( "Example1" );
}

的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_MODULE_PATH ${/home/jinder1s/Documents/project/opencv/FindOpenCV.make} )
project(hello)
Find_package (OpenCV REQUIRED)
if(OpenCV_FOUND)
    add_executable (Hello test.cpp)
    find_library(Opencv_lib 
    NAMES opencv_core opencv_highgui opencv_imgproc
    PATHS /usr/local/lib)
endif()

这是我得到的模板。

jinder1s@jinder1s-lat-lap:~/Documents/project/opencv/tests$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jinder1s/Documents/project/opencv/tests
jinder1s@jinder1s-lat-lap:~/Documents/project/opencv/tests$ make
Linking CXX executable Hello
CMakeFiles/Hello.dir/test.cpp.o: In function `main':
test.cpp:(.text+0x1d): undefined reference to `cvLoadImage'
test.cpp:(.text+0x35): undefined reference to `cvNamedWindow'
test.cpp:(.text+0x49): undefined reference to `cvShowImage'
test.cpp:(.text+0x55): undefined reference to `cvWaitKey'
test.cpp:(.text+0x61): undefined reference to `cvReleaseImage'
test.cpp:(.text+0x6d): undefined reference to `cvDestroyWindow'
collect2: error: ld returned 1 exit status
make[2]: *** [Hello] Error 1
make[1]: *** [CMakeFiles/Hello.dir/all] Error 2
make: *** [all] Error 2

我刚开始学习opencv,因为这是我的第一个代码,我似乎无法让它工作。我真的可以在这里使用一些帮助。看不出我做错了什么。

2 个答案:

答案 0 :(得分:2)

在检查库是否可用后,您必须告诉cmake链接库。

在你的endif()

之前的target_link_libraries( Hello ${OpenCV_LIBS} )之类的东西

有关如何使用CMake的信息,请参阅http://www.cmake.org/cmake/help/cmake_tutorial.html;有关使用openCV的CMake,请参阅http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

您可以通过输入“make VERBOSE = 1”来检查链接器的调用方式。它有助于调试cmake文件。

答案 1 :(得分:0)

我认为您需要在那里指定命名空间。因此,要么为using namespace cv;向上,要么cv::function_name为每个函数调用。

此外,这看起来像OpenCV 1.x代码。您有没有理由这样做而不是使用OpenCV 2.x语法? 2.x更稳定,更直观。例如,该加载图像行在2.x中只是Mat img = imread(filename);。对于2.x。

,您需要#include "opencv2/core/core.hpp"using namespace cv;

cheatsheet可能会有所帮助。