C++ OpenCV 3.0 未定义的参考错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25876684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:27:34  来源:igfitidea点击:

OpenCV 3.0 undefined reference error?

c++opencvubuntucmake

提问by potatoprogrammer

I've just followed the instructions to install opencv 3 on my ubuntu computer (I've already installed and ran programs using opencv 3 on a different linux device), however, when I tried to run one of my test programs from my old computer I get the following errors:

我刚刚按照说明在我的 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

Any idea what's going on? I'm relatively new to opencv. Here's my test code for reference:

知道发生了什么吗?我对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;
}

Thanks in advance.

提前致谢。

Edit: My CMakeLists.txt:

编辑:我的 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})

回答by berak

VideoCapture and VideoWriter were moved to the videoio module in 3.0, so you have to additionally

VideoCapture 和 VideoWriter 已在 3.0 中移至 videoio 模块,因此您必须另外

#include "opencv2/videoio.hpp"

also, you don't seem to linkto any of the required opencv libs, those are:

此外,您似乎没有链接到任何所需的 opencv 库,它们是:

opencv_core, opencv_videoio, opencv_highgui

回答by potatoprogrammer

Alright, I've finally figured out what's going on. Something internally in the CMakeFiles folder went screwy when I moved the directory over to my new system. Removing all files/directories besides the project .cpp file and the CMakeLists.txt file, and running "cmake ." and "make" again seems to solve the problem.

好吧,我终于明白是怎么回事了。当我将目录移动到我的新系统时,CMakeFiles 文件夹内部的某些东西变得棘手。删除除项目 .cpp 文件和 CMakeLists.txt 文件之外的所有文件/目录,并运行“cmake”。而“制造”似乎再次解决了问题。

回答by user3378430

Below cmake file works for me.

下面的 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}")