Ask Your Question
0

[problem]:Using OpenCV with gcc and CMake

asked 2018-07-10 23:46:48 -0600

updated 2018-07-10 23:52:58 -0600

berak gravatar image

i study opencv toturials,have a problem

first:i create file DisplayImage.cpp and CMakeLists.txt file

DisplayImage.cpp content :

#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", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}

CMakeLists.txt file content:

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

second i input .make on my terminal

(tensorflow) chentao@thinkPad:~/opencv$ cmake .
-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenCV: /home/chentao/anaconda3/envs/tensorflow (found version "3.4.1") 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chentao/opencv

Third i input make on terminal

(tensorflow) chentao@thinkPad:~/opencv$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `VTT for std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_core.so.3.4.1: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long)@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_core.so.3.4.1: undefined reference to `std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream(std::_Ios_Openmode)@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::resize(unsigned long, char)@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `vtable for std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_imgcodecs.so.3.4.1: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned long)@GLIBCXX_3.4.21'
/home/chentao/anaconda3/envs/tensorflow/lib/libopencv_core.so.3.4.1: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const@GLIBCXX_3.4.21'
/home/chentao ...
(more)
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2018-07-19 00:24:11 -0600

Just out of curiosity, what type of system are you using? Because this:

-- The C compiler identification is GNU 5.5.0
-- The CXX compiler identification is GNU 4.8.5

Indicates that your C and C++ compiler are quite old. I believe that v4.8 was the first G++ to support C++11. When I try your input and cmake files as posted above on Ubuntu 18.04, it builds and links without any issue. But your C++ compiler is v4.8.5, while mine says:

-- The C compiler identification is GNU 7.3.0
-- The CXX compiler identification is GNU 7.3.0

I also find it suspicious that your C and C++ compilers are not the same version number, but I'm only familiar with Ubuntu, where gcc and g++ are typically kept in sync.

Sorry, not much of an answer, other than check your compiler version.

edit flag offensive delete link more
0

answered 2018-07-11 03:16:42 -0600

darioc85 gravatar image

It seems like a linker problem. Do you see OpenCV if you do pkg-config --libs opencv ? Moreover, you are on VirtualEnv or something like that and it could happen that it is seeing another OpenCV version.

edit flag offensive delete link more

Comments

thank, i input pkg-config --libs opencv,But I don't know what to do next

(tensorflow) chentao@thinkPad:~/opencv/DisplayImage$ pkg-config --libs opencv -L/home/chentao/anaconda3/envs/tensorflow/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_modern -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_photo -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lop

hamburgerBear gravatar imagehamburgerBear ( 2018-07-11 03:55:08 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-07-10 23:46:48 -0600

Seen: 2,239 times

Last updated: Jul 19 '18