Undefined reference errors for OpenCV Hello World in Ubuntu - c++

I'm trying the following code:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char** argv) {
namedWindow("Output",1);
Mat output = Mat::zeros( 120, 350, CV_8UC3 );
putText(output,"Hello World",cvPoint(15,70),
FONT_HERSHEY_PLAIN,3,cvScalar(0,255,0),4);
imshow("Output", output);
waitKey(0);
return 0;
}
I then tried g++ -I/usr/local/include -L/usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_highgui opencv_hello.cpp -o opencv_hello
and g++pkg-config opencv cvblob --cflags --libsopencv_hello.cpp -o opencv_hello
But they both give the same undefined reference errors:
opencv_hello.cpp:(.text+0x132): undefined reference to `cv::namedWindow(cv::String const&, int)'
opencv_hello.cpp:(.text+0x15f): undefined reference to `cv::Mat::zeros(int, int, int)'
opencv_hello.cpp:(.text+0x26f): undefined reference to `cv::putText(cv::_InputOutputArray const&, cv::String const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
opencv_hello.cpp:(.text+0x2d7): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
opencv_hello.cpp:(.text+0x2ff): undefined reference to `cv::waitKey(int)'
/tmp/cctt8VGQ.o: In function `cv::String::String(char const*)':
opencv_hello.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/cctt8VGQ.o: In function `cv::String::~String()':
opencv_hello.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/cctt8VGQ.o: In function `cv::Mat::~Mat()':
opencv_hello.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/cctt8VGQ.o: In function `cv::Mat::release()':
opencv_hello.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
How can I fix this?

If pkg-config opencv --cflags --libs command finds OpenCV include files and libraries below compilation works wituout any errors.
g++ opencv_hello.cpp -o opencv_hello $(pkg-config opencv --cflags --libs)
or
g++ opencv_hello.cpp -o opencv_hello `pkg-config opencv --cflags --libs`

Related

C++ Boost: undefined reference to boost::system::generic_category() in Windows

I am trying to compile this code in CodeBlocks
#include <boost/filesystem.hpp>
#include <iostream>
using namespace boost::filesystem;
int main()
{
if ( !boost::filesystem::exists( "myfile.txt" ) )
{
std::cout << "Can't find my file!" << std::endl;
}
}
With this compile flags:
g++.exe -Wall -fexceptions -g -O3 -pedantic-errors -Wall -std=c++0x -lboost_system -IC:\Users\moe\Desktop\boost_1_67_0 -c C:\Users\moe\Desktop\oo\main.cpp -o obj\Debug\main.o
But I always receive this error:
boost::system::generic_category()
this is the error log, that i receive when i compile the code:
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:733: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:736: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:748: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:703: undefined reference to `boost::system::generic_category()'
C:/Users/moe/Desktop/boost_1_67_0/boost/system/error_code.hpp:706: undefined reference to `boost::system::generic_category()'
Untitled4.o: In function `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path_traits::convert(char const*, char const*, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >&, std::codecvt<wchar_t, char, int> const&)'
Untitled4.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/operations.hpp:446: undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
collect2.exe: error: ld returned 1 exit status
Put the libraries at the end of the linker command line:
Why does the order of '-l' option in gcc matter?
Since your erro log also contained these:
C:/Users/moe/Desktop/boost_1_67_0/boost/filesystem/path.hpp:981: undefined reference to `boost::filesystem::path::codecvt()'
It is clear that you also lack -lboost_filesystem.
So there is no use to just add -lboost_system to the g++ command.
You can add -lboost_system -lboost_filesystem to your g++ command.

Opencv linking libraries - Undefined Reference

I tried to compile opencv codes on raspberry pi 3.However, it gives always "undefined reference ... " error, i think there is a problem with linking.
Compile:
g++ `pkg-config --libs --cflags opencv` Face_Detection.cpp
pkg-config opencv --libs:
-L/opt/lib/ -L/opt/Deneme/opencv/a/ -opencv_calib3d -opencv_core
-opencv_features2d -opencv_flann -opencv_highgui -opencv_imgcodecs
-opencv_imgproc -opencv_ml -opencv_objdetect -opencv_photo -opencv_shape
-opencv_stitching -opencv_superres -opencv_videoio -opencv_video
-opencv_calib3d_pch_dephelp -opencv_contrib_pch_dephelp
-opencv_core_pch_dephelp -opencv_features2d_pch_dephelp
-opencv_flann_pch_dephelp -opencv_gpu_pch_dephelp
-opencv_haartraining_engine -opencv_highgui_pch_dephelp
-opencv_imgproc_pch_dephelp -opencv_legacy_pch_dephelp
-opencv_ml_pch_dephelp -opencv_objdetect_pch_dephelp
-opencv_video_pch_dephelp
pkg-config opencv --cflags:
-I/opt/Deneme/Include/opencv -I/opt/Deneme/Include/
opencv.pc file:
includedir=/opt/Deneme/Include/opencv
dir2=/opt/Deneme/Include/
libdir=/opt/lib/
libdir2=/opt/Deneme/opencv/a/
Name: opencv
Description: The opencv library
Version: 3.x.x
Cflags: -I${includedir} -I${dir2}
Libs: -L${libdir} -opencv_calib3d -opencv_core -opencv_features2d - opencv_flann -opencv_highgui -opencv_imgcodecs -opencv_imgproc -opencv_ml -opencv_objdetect -opencv_photo -opencv_shape -opencv_stitching -opencv_superres -opencv_videoio -opencv_video -L${libdir2} -opencv_calib3d_pch_dephelp -opencv_contrib_pch_dephelp -opencv_core_pch_dephelp -opencv_features2d_pch_dephelp -opencv_flann_pch_dephelp -opencv_gpu_pch_dephelp -opencv_haartraining_engine -opencv_highgui_pch_dephelp -opencv_imgproc_pch_dephelp -opencv_legacy_pch_dephelp -opencv_ml_pch_dephelp -opencv_objdetect_pch_dephelp -opencv_video_pch_dephelp
error:
Face_Detection.cpp:(.text+0x94): undefined reference to `cv::imread(std::string const&, int)'
Face_Detection.cpp:(.text+0xd8): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
Face_Detection.cpp:(.text+0xec): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
Face_Detection.cpp:(.text+0x128): undefined reference to `cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
Face_Detection.cpp:(.text+0x1e8): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
Face_Detection.cpp:(.text+0x218): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
Face_Detection.cpp:(.text+0x250): undefined reference to `cv::_OutputArray::_OutputArray(cv::Mat&)'
Face_Detection.cpp:(.text+0x26c): undefined reference to `cv::hconcat(cv::_InputArray const&, cv::_InputArray const&, cv::_OutputArray const&)'
Face_Detection.cpp:(.text+0x298): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
Face_Detection.cpp:(.text+0x2c8): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
Thanks in advance.
It was linker order error. Try to change
g++ `pkg-config --libs --cflags opencv` Face_Detection.cpp
to
g++ Face_Detection.cpp `pkg-config --libs --cflags opencv`

error using already compiled version of openCV

I've used the already compiled version of openCV for Raspberry Pi. link for anyone who is interested
After trying to compile using this command line
g++ test3.cpp -o test3 -I/usr/local/include/ -lraspicam -lraspicam_cv -L/opt/vc/lib -lmmal -lmmal_core -lmmal_util -I/usr/include -lopencv_core -lopencv_highgui -lopencv_imgproc -lwiringPi -lpthread
I get the following error lines.
//usr/local/lib/libopencv_stitching.so.2.4: undefined reference to `cv::gpu::ensureSizeIsEnough(int, int, int, cv::gpu::GpuMat&)'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::parallel_for_(cv::Range const&, cv::ParallelLoopBody const&, double)'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `typeinfo for cv::ParallelLoopBody'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::Mutex::unlock()'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::Mutex::lock()'
//usr/local/lib/libopencv_ocl.so.2.4: undefined reference to cv::TLSDataContainer::getData() const
//usr/local/lib/libopencv_features2d.so.2.4: undefined reference to cv::AlgorithmInfo::addParam(cv::Algorithm&, char const*, unsigned char&, bool, unsigned char (cv::Algorithm::)(), void (cv::Algorithm::)(unsigned char), std::basic_string, std::allocator > const&)
//usr/local/lib/libopencv_features2d.so.2.4: undefined reference to `cv::AlgorithmInfo::addParam(cv::Algorithm&, char const*, float&, bool, float (cv::Algorithm::)(), void (cv::Algorithm::)(float), std::basic_string, std::allocator > const&)'
//usr/local/lib/libopencv_features2d.so.2.4: undefined reference to `cv::AlgorithmInfo::addParam(cv::Algorithm&, char const*, short&, bool, int (cv::Algorithm::)(), void (cv::Algorithm::)(int), std::basic_string, std::allocator > const&)'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::Mutex::Mutex()'
//usr/local/lib/libopencv_ocl.so.2.4: undefined reference to `cv::TLSDataContainer::TLSDataContainer()'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::ParallelLoopBody::~ParallelLoopBody()'
//usr/local/lib/libopencv_calib3d.so.2.4: undefined reference to `cv::Mutex::~Mutex()'
//usr/local/lib/libopencv_ocl.so.2.4: undefined reference to `cv::TLSDataContainer::~TLSDataContainer()'
collect2: ld returned 1 exit status
You have not linked the executable against several libraries that are required by the program
Try using this:
g++ -lpthread `pkg-config opencv --libs` -I/usr/local/include/ -lraspicam -lraspicam_cv -L/opt/vc/lib -lmmal -lmmal_core -lmmal_util -I/usr/include -lwiringPi test3.cpp -o test3

undefined reference error in g++

i have the following code:
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
int main(int argc, char** argv)
{
Poco::Net::SocketAddress sa("127.0.0.1", 8080);
Poco::Net::DatagramSocket dgs(sa);
std::string syslogMsg;
Poco::Timestamp now;
syslogMsg = Poco::DateTimeFormatter::format(now,
"<14>%w %f %H:%M:%S Hello, world!");
return 0;
}
and after compiling i get the following errors:
/tmp/cc38RdWw.o: In function `main':
pocoSender.cpp:(.text+0x4d): undefined reference to `Poco::Net::SocketAddress::SocketAddress(std::string const&, unsigned short)'
pocoSender.cpp:(.text+0x7d): undefined reference to `Poco::Net::DatagramSocket::DatagramSocket(Poco::Net::SocketAddress const&, bool)'
pocoSender.cpp:(.text+0x93): undefined reference to `Poco::Timestamp::Timestamp()'
pocoSender.cpp:(.text+0x11f): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x135): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x140): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x163): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
pocoSender.cpp:(.text+0x1ac): undefined reference to `Poco::Timestamp::~Timestamp()'
pocoSender.cpp:(.text+0x1ca): undefined reference to `Poco::Net::DatagramSocket::~DatagramSocket()'
pocoSender.cpp:(.text+0x1d9): undefined reference to `Poco::Net::SocketAddress::~SocketAddress()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::Timestamp const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x15): undefined reference to `Poco::DateTime::DateTime(Poco::Timestamp const&)'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x43): undefined reference to `Poco::DateTime::~DateTime()'
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_9TimestampERKSsi]+0x52): undefined reference to `Poco::DateTime::~DateTime()'
/tmp/cc38RdWw.o: In function `Poco::DateTimeFormatter::format(Poco::DateTime const&, std::string const&, int)':
pocoSender.cpp:(.text._ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi[_ZN4Poco17DateTimeFormatter6formatERKNS_8DateTimeERKSsi]+0x41): undefined reference to `Poco::DateTimeFormatter::append(std::string&, Poco::DateTime const&, std::string const&, int)'
collect2: error: ld returned 1 exit status
i use the following commandline to compile:
g++ -L/usr/local/lib -lPocoUtil -lPocoFoundation -lPocoNet pocoSender.cpp -o hello
please do not mark the question as duplicate as i have included the required libraries using g++ flags, but i am still getting these errors.
It means that the compiler or linker did not find implementations of these functions. It seems that the corresponding libraries or object modules were not included in building of the project.

Simple SIFT Detector ISSUE on OpenCV

I'm implementing this simple example just to get familiar with OpenCv and SIFT.
1 #include <opencv2/core/core.hpp>
2 #include <opencv2/features2d/features2d.hpp>
3 #include <opencv2/highgui/highgui.hpp>
4
5 using namespace std;
6
7 int main(int argc, char** argv) {
8
9 const cv::Mat input = cv::imread("/tmp/image.jpg", 0); //Load as grayscale
10
11 cv::SiftFeatureDetector detector;
12 std::vector<cv::KeyPoint> keypoints;
13 detector.detect(input, keypoints);
14
15 // Add results to image and save.
16 cv::Mat output;
17 cv::drawKeypoints(input, keypoints, output);
18 cv::imwrite("/tmp/SIFT_RESULT.jpg", output);
19
20 return 0;
21
22 }
I was expecting it to be quite straightforward, but it's throwing the following errors:
undefined reference to 'cv::SIFT::CommonParams::CommonParams()' at line 11
undefined reference to 'cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const' at line 13
undefined reference to 'cv::drawKeypoints(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, cv::Mat&, cv::Scalar_<double> const&, int)' at line 17
Can you please tell me what's going wrong?
Is it a problem in the code, or am I missing some headers?
Complete Build Output:
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/opencv -I/usr/include/c++/4.5.2 -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: BioInfo
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "BioInfo" ./imageProcessingClass.o ./main.o -lopencv_core -lopencv_highgui
./main.o: In function `main':
/.../Debug/../main.cpp:38: undefined reference to `cv::SIFT::CommonParams::CommonParams()'
/.../Debug/../main.cpp:38: undefined reference to `cv::SIFT::DetectorParams::DetectorParams()'
/.../Debug/../main.cpp:38: undefined reference to `cv::SiftFeatureDetector::SiftFeatureDetector(cv::SIFT::DetectorParams const&, cv::SIFT::CommonParams const&)'
/.../Debug/../main.cpp:40: undefined reference to `cv::FeatureDetector::detect(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> >&, cv::Mat const&) const'
/.../Debug/../main.cpp:44: undefined reference to `cv::drawKeypoints(cv::Mat const&, std::vector<cv::KeyPoint, std::allocator<cv::KeyPoint> > const&, cv::Mat&, cv::Scalar_<double> const&, int)'
./main.o: In function `~SiftFeatureDetector':
/usr/local/include/opencv2/features2d/features2d.hpp:1502: undefined reference to `vtable for cv::SiftFeatureDetector'
/usr/local/include/opencv2/features2d/features2d.hpp:1502: undefined reference to `cv::FeatureDetector::~FeatureDetector()'
collect2: ld returned 1 exit status
make: *** [BioInfo] Error 1
Build Finished
It seems you have forgotten to link with the OpenCV libraries. Also remember if you're linking against OpenCV 2.4 there is a new library for "non-free" algorithms named opencv_nonfree that you need to link against for SIFT.