Mzk
Mzk

Reputation: 1120

Sample Program From Terminal Could Not Run

I'm new to opencv in ubuntu. I've follow the tutorial from here for setting up the opencv in linux. However, I have failed to make the sample program runs. After I type the following,

$ g++ DisplayImage.cpp

These error appears,

/tmp/cc3GTOtQ.o: In function `main':
DisplayImage.cpp:(.text+0x53): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0xe5): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text+0x113): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text+0x147): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
DisplayImage.cpp:(.text+0x169): undefined reference to `cv::waitKey(int)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::~Mat()':
DisplayImage.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::operator=(cv::Mat const&)':
DisplayImage.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
/tmp/cc3GTOtQ.o: In function `cv::Mat::release()':
DisplayImage.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status

Upvotes: 0

Views: 1672

Answers (2)

Haris
Haris

Reputation: 14053

Here is the full command line build C++ program with OpenCV Libraries.

g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g -o binaryName  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy

If you installed OpenCV in different location, you should change the path of 'include' and lib 'directory' to appropriate location.

Edit:-

Instead of running this command every time just create a file build.sh in your project directory, change it's permission to executable by running chmod 777 build.sh and for building the project just run this file.

Upvotes: 2

sinelaw
sinelaw

Reputation: 16553

You need to link with the opencv libraries: probably you'll need to add at least -lcv and maybe -lcxcore and -lhighgui to your command line. See this similar question.

Upvotes: 0

Related Questions