OpenCV 3.0未定义引用错误?

14

我刚按照指示在我的Ubuntu电脑上安装了OpenCV 3(在另一台Linux设备上已经安装并运行过使用OpenCV 3的程序),但是,当我尝试从旧电脑上运行其中一个测试程序时,出现以下错误:

CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `main':
VideoTest.cpp:(.text+0x6f): undefined reference to `cv::VideoWriter::fourcc(char,char,char, char)'
VideoTest.cpp:(.text+0xc3): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_<int>, bool)'
VideoTest.cpp:(.text+0x103): undefined reference to `cv::namedWindow(cv::String const&, int)'
VideoTest.cpp:(.text+0x146): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
VideoTest.cpp:(.text+0x1b1): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::String(char const*)':
VideoTest.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x3b): undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::~String()':
VideoTest.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0xd): undefined reference to `cv::String::deallocate()'
collect2: ld returned 1 exit status
make[2]: *** [VideoTest] Error 1
make[1]: *** [CMakeFiles/VideoTest.dir/all] Error 2
make: *** [all] Error 2

你知道发生了什么吗?我对opencv相对较新。这是我的测试代码作为参考:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[]){
    VideoCapture vCap(0);
    VideoWriter vWrite;
    vWrite.open("test.avi", vWrite.fourcc('M','J','P','G'), 20, Size(640, 480), true);
    while (1) {
        namedWindow("VideoFeed", WINDOW_AUTOSIZE);
        Mat frame;
        vCap.read(frame);
        vWrite.write(frame);
        imshow("VideoFeed", frame);
        char c = waitKey(50);
        if (c == 27) {
            break;
        }
    }
    return 0;
}

提前致谢。

编辑:我的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(VideoTest)
find_package(OpenCV REQUIRED)
add_executable(VideoTest VideoTest.cpp)
target_link_libraries(VideoTest ${OpenCV_LIBS})

1
类似问题【已解决】https://dev59.com/Pmsz5IYBdhLWcg3wj4kc - IEEE754
3个回答

27

VideoCapture 和 VideoWriter 已经在 3.0 版本中移动到 videoio 模块,所以您需要额外导入。

#include "opencv2/videoio.hpp"

此外,您似乎没有链接到所需的OpenCV库,这些库包括:

opencv_core, opencv_videoio, opencv_highgui

我不确定你所说的链接到所需的opencv库是什么意思。你能具体告诉我如何做吗? - potatoprogrammer
你没有告诉我们你使用的操作系统和编译器,还有你似乎使用了CMake来生成东西,所以请在你的问题中添加CMakeLists.txt文件,然后让我们重新迭代。 - berak
我的操作系统版本是Ubuntu 12.04.5 LTS,我正在使用cmake 2.8.7和gcc 4.6.3。已将我的CMakeLists.txt添加到我的问题中。 - potatoprogrammer
抱歉,我没有看到标签。很遗憾,我现在不知道哪里出了问题,你的CMake文件看起来还不错。 - berak
哎呀,奇怪了。不管怎样,感谢你尝试帮助我。 - potatoprogrammer
1
你仍然可以回退到手动编译命令 g++ VideoTest.cpp -lopencv_core -lopencv_videoio -lopencv_highgui -o VideoTest - berak

5

好的,我终于明白了发生了什么。当我把目录移动到我的新系统上时,CMakeFiles文件夹内部的某些东西出现了问题。删除除项目cpp文件和CMakeLists.txt文件之外的所有文件/目录,然后再次运行“cmake。”和“make”似乎可以解决该问题。


4

以下的CMake文件适用于我。

cmake_minimum_required(VERSION 3.5)
project(ImageProcessing)

set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(SOURCE_FILES main.cpp)
add_executable(ImageProcessing ${SOURCE_FILES})
target_link_libraries( ImageProcessing ${OpenCV_LIBS} )
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS "    version: ${OpenCV_VERSION}")
message(STATUS "    libraries: ${OpenCV_LIBS}")
message(STATUS "    include path: ${OpenCV_INCLUDE_DIRS}")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接