OpenCV Libraries in CPP (Specifically, file letter_recog.cpp)

1.2k views Asked by At

I am just trying to compile/build/run a sample code from the opencv libraries for cpp, and I am having an issue when I try to build it.

I have installed opencv correctly as per http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html as well as moving the opencv include files from the framework file into the include for /usr/local/include/opencv2 Moving the opencv include files from the framework into that folder helped most of my problems (the opencv2 files not being found), but the problem of building the file letter_recog.cpp is not working. By not working, I mean that it is having a lot of build errors.

The code for the file is as follows: https://github.com/opencv/opencv/blob/master/samples/cpp/letter_recog.cpp

The Build errors are as follows:

g++ -g -std=c++14 -Wall -Wextra -pedantic -lpthread  -o "letter_recog"  "letter_recog.cpp" (in directory: /home/courtneymaroney/opencv/samples/cpp)
/tmp/ccUbgW02.o: In function `read_num_class_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, cv::Mat*, cv::Mat*)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:74: undefined reference to `cv::Mat::push_back(cv::Mat const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:77: undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
/tmp/ccUbgW02.o: In function `prepare_train_data(cv::Mat const&, cv::Mat const&, int)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:100: undefined reference to `cv::Mat::zeros(int, int, int)'
/tmp/ccUbgW02.o: In function `prepare_train_data(cv::Mat const&, cv::Mat const&, int)':
/usr/local/include/opencv2/core/mat.hpp:1110: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `prepare_train_data(cv::Mat const&, cv::Mat const&, int)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:102: undefined reference to `cv::Mat::setTo(cv::_InputArray const&, cv::_InputArray const&)'
/tmp/ccUbgW02.o: In function `prepare_train_data(cv::Mat const&, cv::Mat const&, int)':
/usr/local/include/opencv2/core/mat.hpp:1110: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `prepare_train_data(cv::Mat const&, cv::Mat const&, int)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:106: undefined reference to `cv::Mat::setTo(cv::_InputArray const&, cv::_InputArray const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:110: undefined reference to `cv::noArray()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:110: undefined reference to `cv::noArray()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:110: undefined reference to `cv::ml::TrainData::create(cv::_InputArray const&, int, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&)'
/tmp/ccUbgW02.o: In function `test_and_save_classifier(cv::Ptr<cv::ml::StatModel> const&, cv::Mat const&, cv::Mat const&, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/opencv2/ml.hpp:349: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `build_rtrees_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:187: undefined reference to `cv::ml::RTrees::create()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:208: undefined reference to `cv::sum(cv::_InputArray const&)'
/tmp/ccUbgW02.o: In function `build_boost_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/opencv2/core/mat.hpp:1110: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `build_boost_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:277: undefined reference to `cv::Mat::setTo(cv::_InputArray const&, cv::_InputArray const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:281: undefined reference to `cv::noArray()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:281: undefined reference to `cv::noArray()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:281: undefined reference to `cv::noArray()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:281: undefined reference to `cv::ml::TrainData::create(cv::_InputArray const&, int, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:287: undefined reference to `cv::ml::Boost::create()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:314: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `build_mlp_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:383: undefined reference to `cv::Mat::zeros(int, int, int)'
/tmp/ccUbgW02.o: In function `build_mlp_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/opencv2/ml.hpp:285: undefined reference to `cv::noArray()'
/usr/local/include/opencv2/ml.hpp:285: undefined reference to `cv::noArray()'
/usr/local/include/opencv2/ml.hpp:284: undefined reference to `cv::noArray()'
/usr/local/include/opencv2/ml.hpp:284: undefined reference to `cv::noArray()'
/tmp/ccUbgW02.o: In function `build_mlp_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:408: undefined reference to `cv::ml::TrainData::create(cv::_InputArray const&, int, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&, cv::_InputArray const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:411: undefined reference to `cv::ml::ANN_MLP::create()'
/tmp/ccUbgW02.o: In function `build_knearest_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:440: undefined reference to `cv::ml::KNearest::create()'
/tmp/ccUbgW02.o: In function `build_nbayes_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:467: undefined reference to `cv::ml::NormalBayesClassifier::create()'
/tmp/ccUbgW02.o: In function `build_svm_classifier(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:504: undefined reference to `cv::ml::SVM::create()'
/tmp/ccUbgW02.o: In function `main':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:524: undefined reference to `cv::CommandLineParser::CommandLineParser(int, char const* const*, cv::String const&)'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:526: undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:528: undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:530: undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:532: undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:534: undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
/tmp/ccUbgW02.o:/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:536: more undefined references to `cv::CommandLineParser::has(cv::String const&) const' follow
/tmp/ccUbgW02.o: In function `main':
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:557: undefined reference to `cv::CommandLineParser::~CommandLineParser()'
/home/courtneymaroney/opencv/samples/cpp/letter_recog.cpp:524: undefined reference to `cv::CommandLineParser::~CommandLineParser()'
/tmp/ccUbgW02.o: In function `cv::String::String(char const*)':
/usr/local/include/opencv2/core/cvstd.hpp:618: undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccUbgW02.o: In function `cv::String::~String()':
/usr/local/include/opencv2/core/cvstd.hpp:660: undefined reference to `cv::String::deallocate()'
/tmp/ccUbgW02.o: In function `cv::Mat::Mat(int, int, int, void*, unsigned long)':
/usr/local/include/opencv2/core/mat.inl.hpp:413: undefined reference to `cv::error(int, cv::String const&, char const*, char const*, int)'
/tmp/ccUbgW02.o: In function `cv::Mat::~Mat()':
/usr/local/include/opencv2/core/mat.inl.hpp:556: undefined reference to `cv::fastFree(void*)'
/tmp/ccUbgW02.o: In function `cv::Mat::row(int) const':
/usr/local/include/opencv2/core/mat.inl.hpp:591: undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)'
/tmp/ccUbgW02.o: In function `cv::Mat::rowRange(int, int) const':
/usr/local/include/opencv2/core/mat.inl.hpp:603: undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)'
/tmp/ccUbgW02.o: In function `cv::Mat::colRange(int, int) const':
/usr/local/include/opencv2/core/mat.inl.hpp:615: undefined reference to `cv::Mat::Mat(cv::Mat const&, cv::Range const&, cv::Range const&)'
/tmp/ccUbgW02.o: In function `cv::Mat::create(int, int, int)':
/usr/local/include/opencv2/core/mat.inl.hpp:648: undefined reference to `cv::Mat::create(int, int const*, int)'
/tmp/ccUbgW02.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.inl.hpp:667: undefined reference to `cv::Mat::deallocate()'
/tmp/ccUbgW02.o: In function `cv::FileStorage::getFirstTopLevelNode() const':
/usr/local/include/opencv2/core/persistence.hpp:1167: undefined reference to `cv::FileStorage::root(int) const'
/tmp/ccUbgW02.o: In function `cv::FileNode::begin() const':
/usr/local/include/opencv2/core/persistence.hpp:1184: undefined reference to `cv::FileNodeIterator::FileNodeIterator(CvFileStorage const*, CvFileNode const*, unsigned long)'
/tmp/ccUbgW02.o: In function `cv::FileNode::end() const':
/usr/local/include/opencv2/core/persistence.hpp:1185: undefined reference to `cv::FileNode::size() const'
/usr/local/include/opencv2/core/persistence.hpp:1185: undefined reference to `cv::FileNodeIterator::FileNodeIterator(CvFileStorage const*, CvFileNode const*, unsigned long)'
/tmp/ccUbgW02.o: In function `cv::String::String(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/local/include/opencv2/core/cvstd.inl.hpp:81: undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccUbgW02.o: In function `cv::String cv::CommandLineParser::get<cv::String>(cv::String const&, bool) const':
/usr/local/include/opencv2/core/utility.hpp:682: undefined reference to `cv::CommandLineParser::getByName(cv::String const&, bool, int, void*) const'
/tmp/ccUbgW02.o: In function `cv::Mat::Mat<int>(std::vector<int, std::allocator<int> > const&, bool)':
/usr/local/include/opencv2/core/mat.inl.hpp:469: undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
/tmp/ccUbgW02.o: In function `cv::Mat::Mat<double>(std::vector<double, std::allocator<double> > const&, bool)':
/usr/local/include/opencv2/core/mat.inl.hpp:469: undefined reference to `cv::Mat::copyTo(cv::_OutputArray const&) const'
/tmp/ccUbgW02.o: In function `cv::Ptr<cv::ml::RTrees> cv::Algorithm::load<cv::ml::RTrees>(cv::String const&, cv::String const&)':
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::FileStorage(cv::String const&, int, cv::String const&)'
/usr/local/include/opencv2/core.hpp:2973: undefined reference to `cv::FileStorage::operator[](cv::String const&) const'
/usr/local/include/opencv2/core.hpp:2974: undefined reference to `cv::ml::RTrees::create()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/tmp/ccUbgW02.o: In function `cv::Ptr<cv::ml::Boost> cv::Algorithm::load<cv::ml::Boost>(cv::String const&, cv::String const&)':
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::FileStorage(cv::String const&, int, cv::String const&)'
/usr/local/include/opencv2/core.hpp:2973: undefined reference to `cv::FileStorage::operator[](cv::String const&) const'
/usr/local/include/opencv2/core.hpp:2974: undefined reference to `cv::ml::Boost::create()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/tmp/ccUbgW02.o: In function `cv::Ptr<cv::ml::ANN_MLP> cv::Algorithm::load<cv::ml::ANN_MLP>(cv::String const&, cv::String const&)':
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::FileStorage(cv::String const&, int, cv::String const&)'
/usr/local/include/opencv2/core.hpp:2973: undefined reference to `cv::FileStorage::operator[](cv::String const&) const'
/usr/local/include/opencv2/core.hpp:2974: undefined reference to `cv::ml::ANN_MLP::create()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/tmp/ccUbgW02.o: In function `cv::Ptr<cv::ml::SVM> cv::Algorithm::load<cv::ml::SVM>(cv::String const&, cv::String const&)':
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::FileStorage(cv::String const&, int, cv::String const&)'
/usr/local/include/opencv2/core.hpp:2973: undefined reference to `cv::FileStorage::operator[](cv::String const&) const'
/usr/local/include/opencv2/core.hpp:2974: undefined reference to `cv::ml::SVM::create()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
/usr/local/include/opencv2/core.hpp:2972: undefined reference to `cv::FileStorage::~FileStorage()'
collect2: error: ld returned 1 exit status
Compilation failed.

I am using an IDE called "Geany" for Ubuntu 16.04.

The compile/build commands for granny are as follows: Compile: g++ -std=c++14 -Wall -c "%f" Build: g++ -g -std=c++14 -Wall -Wextra -pedantic -lpthread -o "%e" "%f"

I was looking on the internet for possible solutions. It seems like some people have this problem but no one has found a solution to it yet, or at least no one has shared one for this.

Output of echo $(pkg-config --libs opencv) [I formatted it to make it more readable]:

echo $(pkg-config --libs opencv)

/usr/lib/x86_64-linux-gnu/libopencv_calib3d.so

 -lopencv_calib3d

/usr/lib/x86_64-linux-gnu/libopencv_contrib.so

 -lopencv_contrib 

/usr/lib/x86_64-linux-gnu/libopencv_core.so 

-lopencv_core

 /usr/lib/x86_64-linux-gnu/libopencv_features2d.so 

-lopencv_features2d 

/usr/lib/x86_64-linux-gnu/libopencv_flann.so

 -lopencv_flann

 /usr/lib/x86_64-linux-gnu/libopencv_gpu.so 

-lopencv_gpu 

/usr/lib/x86_64-linux-gnu/libopencv_highgui.so

 -lopencv_highgui

 /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so 

-lopencv_imgproc 

/usr/lib/x86_64-linux-gnu/libopencv_legacy.so

 -lopencv_legacy 

/usr/lib/x86_64-linux-gnu/libopencv_ml.so

 -lopencv_ml 

/usr/lib/x86_64-linux-gnu/libopencv_objdetect.so

 -lopencv_objdetect 

/usr/lib/x86_64-linux-gnu/libopencv_ocl.so

-lopencv_ocl 

/usr/lib/x86_64-linux-gnu/libopencv_photo.so

 -lopencv_photo 

/usr/lib/x86_64-linux-gnu/libopencv_stitching.so 

-lopencv_stitching 

/usr/lib/x86_64-linux-gnu/libopencv_superres.so 

-lopencv_superres

 /usr/lib/x86_64-linux-gnu/libopencv_ts.so 

-lopencv_ts 

/usr/lib/x86_64-linux-gnu/libopencv_video.so

 -lopencv_video

 /usr/lib/x86_64-linux-gnu/libopencv_videostab.so

 -lopencv_videostab
2

There are 2 answers

9
Matteo Ragni On

I don't understand how you installed opencv on your system, and if you really copied manually the headers in your system path then probably I don't want to know. Also I'm allergic to IDE, so let's use the more friendly terminal.

Usually the best option is by installing the one in your repository. In your case (from the tags of your question it appears an ubuntu 16.10):

sudo apt update
sudo apt install libopencv-dev

this will fix the pkg-config files that will help you in compilation. (Disclaimer the presence of manually copied headers may disturb the installation process. I suggest you to remove them prior to issue these two commands, if you put them in /usr/include)

Now for the compilation of letter_recog.cpp it is sufficient to call the compiler in this way:

g++ $(pkg-config --cflags --libs opencv) letter_recog.cpp -o letter_recog

If you want you may also add your flags (-Wall -pedantic etc etc), but I suggest you to check what pkg-config does with:

echo $(pkg-config --cflags opencv)
echo $(pkg-config --libs opencv)

the second in particular is what you apparently missed (the libraries to link against).

Finally, for fixing in Geany I suspect you have to put somewhere something like:

g++ -std=c++14 -Wall $(pkg-config --cflags opencv) -c "%f"

and

g++ -g -std=c++14 -Wall -Wextra -pedantic -lpthread $(pkg-config --libs opencv) -o "%e" "%f"

but this solution is questionable (like the -lpthread instead of -pthread; and also you don't need them for that code).

Long story short: Listen to the first comment of Dan MaĊĦek: he is right.

(sorry for the typos it is very late in my fuse)

0
Courtney Maroney On

Fixed: Reinstalled it using this guide:

https://medium.com/@debugvn/installing-opencv-3-3-0-on-ubuntu-16-04-lts-7db376f93961

Didn't work with only putting g++ $(pkg-config --cflags --libs opencv) letter_recog.cpp -o letter_recog so I had to add some flags (-lopencv_name_of_library)

I'm a bit of a noob with makefiles. Once I find a way to substitute filenames for variables with Makefiles, I will give a custom makefile here (as an edit) to compile/build the projects, but so far, since I am using an IDE called Geany, I am using the build command (for all of the projects, doesn't matter which one)

g++ $(pkg-config --cflags --libs opencv) %f -o %e -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_photo -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -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_ml -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_flann -lopencv_xobjdetect -lopencv_imgcodecs -lopencv_objdetect -lopencv_xphoto -lopencv_imgproc -lopencv_core

Thanks guys for the help. Most helpful comment was the one suggesting that I installed it incorrectly. Hope my findings help other people. (Trust me, it was hell, after trying a different installation guide, tried to install an nvidia driver and it corrupted my whole OS, so I had to uninstall that OS, reinstall the OS with a clean slate, and started over (lost all my files in the process))

Hope you guys don't have to go through the trouble I did. It was hell, but I think opencv is pretty worth it.