Ask Your Question
0

OpenCV DisplayImage Example Errors

asked 2018-12-15 10:14:45 -0600

updated 2018-12-15 10:38:11 -0600

Hello OpenCV Community,

I have been an OpenCV python user for a while, but I just started to use OpenCV C++ recently. I installed and tested OpenCV 4.0 following the protocols on https://docs.opencv.org/4.0.0/d7/d9f/.... The program passed the tests after installation and it seems that the installation was successful.

[----------] Global test environment tear-down
[==========] 10797 tests from 228 test cases ran. (385130 ms total)
[  PASSED  ] 10797 tests.

  YOU HAVE 10 DISABLED TESTS

However, when I started to do the DisplayImage example on https://docs.opencv.org/4.0.0/db/df5/..., I got errors.

The "DisplayImage.cpp" is exactly the same to the one in the tutorial. My "CMakeLists.txt file" is this:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( /usr/local/include/opencv4/ )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage /usr/local/lib )

The last few lines of the error is like this:

CMakeFiles/DisplayImage.dir/build.make:62: recipe for target 'CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o' failed
make[2]: *** [CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/DisplayImage.dir/all' failed
make[1]: *** [CMakeFiles/DisplayImage.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Please provide some suggestions. Thank you very much.

Follow-up

I just output the stderror (~5000 lines) to file so I could see all the errors. In the first several lines of the error, it said:

[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
In file included from /usr/local/include/opencv4/opencv2/core.hpp:52:0,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:52,
                 from /home/marine/Workspace/OpenCV_4_CPP/project/DisplayImage.cpp:2:
/usr/local/include/opencv4/opencv2/core/cvdef.h:654:4: error: #error "OpenCV 4.x+ requires enabled C++11 support"
 #  error "OpenCV 4.x+ requires enabled C++11 support"
    ^
In file included from /usr/include/c++/5/array:35:0,
                 from /usr/local/include/opencv4/opencv2/core/cvdef.h:659,
                 from /usr/local/include/opencv4/opencv2/core.hpp:52,
                 from /usr/local/include/opencv4/opencv2/opencv.hpp:52,
                 from /home/marine/Workspace/OpenCV_4_CPP/project/DisplayImage.cpp:2:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \

It seems that I don't have C++ 11 support.

I added "set (CMAKE_CXX_STANDARD 11)" to "CMakeLists.txt file". It seems that this is helpful. However, I got the following erros.

`` marine@Marine-Dell:~/Workspace/OpenCV_4_CPP/project$ make [ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o [100%] Linking CXX executable DisplayImage CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In functionmain': DisplayImage.cpp:(.text+0xa1): undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' DisplayImage.cpp:(.text+0x13f): undefined reference tocv::namedWindow ... (more)

edit retag flag offensive close merge delete

Comments

yea, it failed, but why ?

the real error must be a few lines up in the make output. can you take another look ?

berak gravatar imageberak ( 2018-12-15 10:17:55 -0600 )edit
1

Thank you for your response. I just made an update.

Lei Mao gravatar imageLei Mao ( 2018-12-15 10:22:54 -0600 )edit

which gcc --version is it ?

/usr/include/c++/5 <-- that looks quite outdated

berak gravatar imageberak ( 2018-12-15 10:27:28 -0600 )edit

marine@Marine-Dell:~/Workspace/OpenCV_4_CPP/project$ gcc --version gcc (Ubuntu 5.5.0-12ubuntu1~16.04) 5.5.0 20171010 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Lei Mao gravatar imageLei Mao ( 2018-12-15 10:28:17 -0600 )edit
1

Even if I passed all the tests after installation, will it still likely to be the gcc problem?

Lei Mao gravatar imageLei Mao ( 2018-12-15 10:29:08 -0600 )edit

so, gcc5.5.0 does not support c++11 "out-of-the-box".

while compiling the opencv libs a -std=c++11 flag was added automatically to the g++ cmdline (so that worked), we now have to find out, how to get that into your cmake script

berak gravatar imageberak ( 2018-12-15 10:37:20 -0600 )edit

Thank you for your quick response. I made an update to my post on how to add C++11 support to cmake. I got much fewer errors, but the error still exists.

Lei Mao gravatar imageLei Mao ( 2018-12-15 10:39:30 -0600 )edit

imho, your opencv libs built all fine, and the only remaining problem is: "how to compile an example program".

what about a simple:

g++ -std=c++11 DisplayImage.cpp -I/usr/local/include/opencv4 -lopencv_core -lopencv_imgcodecs -lopencv_highgui -o DisplayImage
berak gravatar imageberak ( 2018-12-15 10:56:20 -0600 )edit
1

Thanks for the followup. There is no problem compile the program using the command you suggested. This means that my old gcc is fine. We have to figure out a way to do it using cmake, eventually probably. However, when I try to display an image using the executable file, I got errors.

marine@Marine-Dell:~/Workspace/OpenCV_4_CPP/project$ ./DisplayImage gta5.jpg ./DisplayImage: error while loading shared libraries: libopencv_core.so.4.0: cannot open shared object file: No such file or directory

I guess we have to tell the program where the lib is.

Lei Mao gravatar imageLei Mao ( 2018-12-15 11:03:09 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2018-12-15 11:38:31 -0600

I finally solve the above problem by running the below magic:

sudo apt-get update sudo apt-get upgrade

It would be good if you include the lib path as well:

g++ -std=c++11 DisplayImage.cpp -I/usr/local/include/opencv4 -L/usr/local/lib/ -lopencv_core -lopencv_imgcodecs -lopencv_highgui -o DisplayImage

Now I could open the image successfully. However, if we want a perfect answer, we still need to figure out how to do it in CMAKE.

edit flag offensive delete link more

Comments

yea, we need a better answer, but for now ..

berak gravatar imageberak ( 2018-12-15 11:39:41 -0600 )edit

Very appreciated. Hopefully the tutorial documentation will also be updated for OpenCV 4.0 soon.

Lei Mao gravatar imageLei Mao ( 2018-12-15 11:41:18 -0600 )edit

just curious, what exactly did the mentioned apt-get update change on your box ?

(it seems to be a somewhat local (on your box only) problem)

berak gravatar imageberak ( 2018-12-15 11:43:27 -0600 )edit
1

I actually really don't know. A guy on StackOverflow mentioned this. I simply tried and it worked. See the magic trick

Lei Mao gravatar imageLei Mao ( 2018-12-15 12:07:24 -0600 )edit
3

answered 2018-12-16 11:32:29 -0600

updated 2018-12-16 11:36:39 -0600

berak gravatar image

To use CMAKE:

cmake_minimum_required(VERSION 2.8)
set (CMAKE_CXX_STANDARD 11)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( /usr/local/include/opencv4/ )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage /usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui )

Compiles the program without error.

edit flag offensive delete link more

Comments

2

This might help the users who use lower versions of g++ :)

Lei Mao gravatar imageLei Mao ( 2018-12-16 11:38:51 -0600 )edit

thanks for coming back at this, and i hope, you don't mind making this another answer

(as it will sincerely be helpful to others in the future ..)

berak gravatar imageberak ( 2018-12-16 11:45:11 -0600 )edit
1

@berak. There is no need to explained about sudo apt-get update sudo apt-get upgrade Merely, if you wanted to install opencv or anything, you will have to do update and upgrade before processed opencv. Otherwise you will get an error such as no module named cv2. It was dating back to 2012, when Raspberry pi Foundation informed users to used update and upgrade, when using raspberry pi using linux.

supra56 gravatar imagesupra56 ( 2018-12-17 09:08:56 -0600 )edit

I don't understand include_directories( /usr/local/include/opencv4/ ). Shouldn't it be include_directories( ${OpenCV_INCLUDE_DIRS} ) or without any lines as in tutorial ?

LBerger gravatar imageLBerger ( 2018-12-18 08:55:41 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-12-15 10:12:48 -0600

Seen: 2,655 times

Last updated: Dec 16 '18