【问题标题】:OPENCV error: undefined reference to "cvLoadImage"OPENCV 错误:未定义对“cvLoadImage”的引用
【发布时间】:2013-06-29 21:20:08
【问题描述】:

我有两个文件:

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

【问题讨论】:

    标签: c++ opencv makefile cmake


    【解决方案1】:

    检查库是否可用后,您必须告诉 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 了解 CMake 与 openCV。

    您可以通过键入“make VERBOSE=1”来检查您的链接器是如何被调用的。它有助于调试 cmake 文件。

    【讨论】:

    • 要查找库,请使用位于“/home/jinder1s/Documents/project/opencv/FindOpenCV.make”的 CMake 模块(这就是第一行所说的)。在此文件中,您可能会找到存储您请求的 OpenCV LIBS(使用 find_library)的变量的名称。也许它正是“OpenCV_LIBS”的注释。您可以检查 ${OpenCV_LIBS} 变量中的内容,您可以在 CMakeLists 中写入 message(${OpenCV_LIBS})。
    • 另外,你的set(CMAKE_MODULE_PATH path) 很奇怪。对于绝对路径,它应该是set(CMAKE_MODULE_PATH /home/jinder1s/Documents/project/opencv/FindOpenCV.make)。您想使用更便携的东西,例如set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/"),并将 FindOpenCV.cmake 复制到您的项目/cmake/Modules/ 目录中。但是由于 cmake 不抱怨,因此您的问题与这一点无关。
    【解决方案2】:

    我认为您需要在那里指定命名空间。所以要么在顶部做一个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 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 1970-01-01
      • 2016-11-13
      • 2015-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 2012-06-23
      • 2012-07-19
      相关资源
      最近更新 更多