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

OPENCV error: undefined reference to "cvLoadImage"

本文关键字:cvLoadImage 引用 错误 未定义 OPENCV      更新时间:2023-10-16

我有两个文件:

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,因为这是我的第一个代码,但我似乎无法让它发挥作用。我真的需要一些帮助。看不出我做错了什么。

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

类似target_link_libraries( Hello ${OpenCV_LIBS} )的东西就在您的endif()之前

请参阅http://www.cmake.org/cmake/help/cmake_tutorial.html了解如何使用CMake,以及http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html用于带有openCV的CMake。

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

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

此外,这看起来像OpenCV 1.x代码。你为什么不使用OpenCV2.x语法,而这么做是有原因的吗?2.x更加稳定和直观。例如,加载图像行在2.x中只是Mat img = imread(filename);。对于2.x,您需要顶部的#include "opencv2/core/core.hpp"using namespace cv;

这份备忘单可能会有所帮助。