C++ “未在此范围内声明函数”编译 openCV 代码时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17878160/
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-27 21:30:27  来源:igfitidea点击:

"Function not declared in this scope" Error in compiling openCV code

c++opencvimage-processing

提问by HymanStinger

I'm trying to write some code which uses openCV functions. I started by taking some of the example code available in the documentation:

我正在尝试编写一些使用 openCV 函数的代码。我从文档中提供的一些示例代码开始:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1]);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.

    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

When I try to build it in Eclipse-CDT, I get this:

当我尝试在 Eclipse-CDT 中构建它时,我得到了这个:

**** Build of configuration Debug for project openCV1 ****

make all 
Building target: openCV1
Invoking: Cross G++ Linker
g++ -L/usr/local/lib -o "openCV1"  ./src/displayImage.o   
./src/displayImage.o: In function `main':
/home/Hymanstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:25: undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/Hymanstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:33: undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/Hymanstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/Hymanstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:34: undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/Hymanstinger/workspace1/openCV1/Debug/../src/displayImage.cpp:36: undefined reference to `cv::waitKey(int)'
./src/displayImage.o: In function `~Mat':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
./src/displayImage.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
./src/displayImage.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [openCV1] Error 1

**** Build Finished ****

The same code, when I build with g++ (g++ -o displayImageInput displayImageInput.cpppkg-config opencv --cflags --libs) works.

相同的代码,当我使用 g++ ( g++ -o displayImageInput displayImageInput.cpppkg-config opencv --cflags --libs )构建时有效。

I then changed the code to make the image greyscale,

然后我更改了代码以使图像灰度化,

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1]);   // Read the file

    if(! image.data )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    Mat grey;
    cvtColor(image, grey, CV_BGR2GRAY);

    namedWindow("image", CV_WINDOW_AUTOSIZE);
    imshow("image", grey);
    waitKey();                                         // Wait for a keystroke in the window
    return 0;
}

It gave the following error when building with g++:

使用 g++ 构建时出现以下错误:

dispImgSobel.cpp: In function ‘int main(int, char**)':
dispImgSobel.cpp:34:27: error: ‘CV_BGR2GRAY' was not declared in this scope
dispImgSobel.cpp:34:38: error: ‘cvtColor' was not declared in this scope

I need help with two things, How to get it working in Eclipse, and second, how to resolve this scope error, for this and future use cases.

我需要两件事的帮助,如何让它在 Eclipse 中工作,其次,如何解决这个范围错误,对于这个和未来的用例。

回答by berak

  • the first err is a linker problem. you did not link against opencv_core.a and opencv_highgui.a

    rule of thumb: for every module header you include, you'll need to link the appropriate library.

  • the 2nd is a compiler problem, you forgot a header, #include <opencv2/imgproc/imgproc.hpp>

    and, ofc. need to link opencv_imgproc

  • 第一个错误是链接器问题。你没有链接 opencv_core.a 和 opencv_highgui.a

    经验法则:对于您包含的每个模块头,您都需要链接相应的库。

  • 第二个是编译器问题,你忘记了标题, #include <opencv2/imgproc/imgproc.hpp>

    并且,ofc。需要链接opencv_imgproc

回答by jam

Make sure that everything in the output of pkg-config opencv --cflags --libsis in your search path for the linker.

确保输出中的所有内容都在pkg-config opencv --cflags --libs链接器的搜索路径中。

There's also a pkg-config add-on for Eclipsewhich will allow you to put the exact string above in directly rather than manually adding each item of the output.

还有一个用于 Eclipsepkg-config 附加组件,它允许您直接将上面的确切字符串放入,而不是手动添加输出的每个项目。