在OpenCV-DisplayImage.cpp中编译c++代码时出现问题(使用gcc和CMake)

Trouble with compiling c++ code in OpenCV - DisplayImage.cpp (using gcc and CMake)

本文关键字:使用 问题 gcc CMake cpp OpenCV-DisplayImage 编译 c++ 代码      更新时间:2023-10-16

我遵循这里给出的代码http://docs.opencv.org/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html#linux-gcc使用这是使用GCC和CMAKE进行编译。

我保存了一个DisplayImage.cpp文件和一个CMakeLists.txt文件,其中包含代码。lena.jpeg也保存在同一目录中。

DisplayImage.cpp 代码

#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data n");
        return -1;
    }
    namedWindow("Display Image", CV_WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

CmakeLists.txt 代码

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

然后我尝试使用cmake。制作

但我得到以下错误。。。

clive@clive-Aspire-4755:~/Visual_Odometry/cpp/DisplayImage$ cmake .
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/clive/Visual_Odometry/cpp/DisplayImage
clive@clive-Aspire-4755:~/Visual_Odometry/cpp/DisplayImage$ make
Scanning dependencies of target DisplayImage
[100%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
/home/clive/Visual_Odometry/cpp/DisplayImage/DisplayImage.cpp: In function ‘int main(int, char**)’:
/home/clive/Visual_Odometry/cpp/DisplayImage/DisplayImage.cpp:8:5: error: ‘arg’ was not declared in this scope
/home/clive/Visual_Odometry/cpp/DisplayImage/DisplayImage.cpp:8:5: note: suggested alternative:
/usr/include/c++/4.6/complex:619:5: note:   ‘std::arg’
/home/clive/Visual_Odometry/cpp/DisplayImage/DisplayImage.cpp:25:11: error: ‘waitkey’ was not declared in this scope
make[2]: *** [CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o] Error 1
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
make: *** [all] Error 2

由于某些错误,无法创建DisplayImage可执行文件。有人能帮我吗?提前谢谢。

对于那些面临此问题的人,请尝试添加

#include <opencv2/highgui/highgui_c.h>

到DisplayImage.cpp代码的顶部。然后编译并再次执行。希望它能起作用!我只是试着自己验证了一下。