libcv-dev install
10.04
Any ideas as to where the following might be defined?
ahcarpenter#ahcarpenter-laptop:~$ g++ objectmarker.o -o objectmarker
objectmarker.o: In function `on_mouse(int, int, int, int, void*)':
objectmarker.cpp:(.text+0x12f): undefined reference to `cvCloneImage'
objectmarker.cpp:(.text+0x1d1): undefined reference to `cvRectangle'
objectmarker.cpp:(.text+0x1ea): undefined reference to `cvShowImage'
objectmarker.cpp:(.text+0x1f4): undefined reference to `cvReleaseImage'
objectmarker.o: In function `main':
objectmarker.cpp:(.text+0x391): undefined reference to `cvNamedWindow'
objectmarker.cpp:(.text+0x3aa): undefined reference to `cvSetMouseCallback'
objectmarker.cpp:(.text+0x4da): undefined reference to `cvLoadImage'
objectmarker.cpp:(.text+0x50f): undefined reference to `cvShowImage'
objectmarker.cpp:(.text+0x519): undefined reference to `cvWaitKey'
objectmarker.cpp:(.text+0x53f): undefined reference to `cvReleaseImage'
objectmarker.cpp:(.text+0x54e): undefined reference to `cvDestroyWindow'
objectmarker.cpp:(.text+0xd7f): undefined reference to `cvReleaseImage'
objectmarker.cpp:(.text+0xdf3): undefined reference to `cvDestroyWindow'
collect2: ld returned 1 exit status
I tried this and it worked for me:
sudo g++ -I/usr/include/opencv main.cpp -o main -lopencv_core -lopencv_imgproc
-lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d
-lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -I /lib/
-I /home/ubuntu/micros/opencv_directory/include/opencv
I got it from: https://askubuntu.com/questions/239891/opencv-program-wont-compile-quantal
It seems you're not linking against the OpenCV libraries; according to this guide, one way to compile an OpenCV program (after OpenCV has been properly configured) is
g++ `pkg-config opencv --cflags` my_code.cpp -o my_code `pkg-config opencv --libs`
If your C++ code is compiled in a separate step, you can probably drop the --cflags part.
You need to include libraries using pkg-config and cflags.
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename filename.cpp .cpp` filename.cpp `pkg-config --libs opencv
see this link for more details http://www.jayrambhia.com/blog/2012/05/08/beginning-opencv/
Related
There are a couple similar problems to this but they all seem to have answers that don't work.
I've installed opencv4 onto an image of Ubuntu and I know the opencv files are install correctly
$ pkg-config --cflags opencv
-I/usr/include/opencv
and have a proper .cp file
/usr/local/lib/pkgconfig$ ls
gpr.pc grpc++.pc grpc++_unsecure.pc protobuf-lite.pc
grpc.pc grpc_unsecure.pc opencv4.pc protobuf.pc
But even though it's all installed correctly it's giving me this error.
~/neuralink/neuralink-image-service-prompt/proto$ make g++ -std=c++11 `pkg-config --cflags opencv protobuf grpc` -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl foo.cpp -o foo
/tmp/ccnfoOEl.o: In function `main':
foo.cpp:(.text+0x48): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccnfoOEl.o: In function `cv::String::String(char const*)':
foo.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccnfoOEl.o: In function `cv::String::~String()':
foo.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::String::operator=(cv::String const&)':
foo.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccnfoOEl.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'foo' failed
make: *** [foo] Error 1
Some people have already tried answering similar questions (such as Undefined reference to cv::imread(cv::String const&, int) and linking opencv libraries with g++) however these don't help, as the user didn't add opencv to their makefile and I have.
Here is my make makefile
LDFLAGS = -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++`\
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
-ldl
CXX = g++
CPPFLAGS += `pkg-config --cflags opencv protobuf grpc`
CXXFLAGS += -std=c++11
GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
all: foo
client: image.pb.o image.grpc.pb.o client.o
$(CXX) $^ $(LDFLAGS) -o $#
server: image.pb.o image.grpc.pb.o server.o
$(CXX) $^ $(LDFLAGS) -o $#
%.grpc.pb.cc: %.proto
protoc --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
%.pb.cc: %.proto
protoc --cpp_out=. $<
clean:
rm -f *.o *.pb.cc *.pb.h client server
And I wrote a simple program that still throws the error
#include <opencv2/opencv.hpp>
#include <stdio.h>
int main() {
cv::Mat frame = cv::imread("sylvania.png", cv::IMREAD_UNCHANGED);
printf("Image size is %i x %i\n", frame.rows, frame.cols);
}
Everything seems to compile fine but I keep getting this linker issue. I don't seem to get the issue when I compile it on another computer (my mac) for what it's worth. Also I have the gRPC and Protoc files because I need them for the project, I just didn't use those file as an example since they're quite long.
I agree with Micka, here is a Makefile of one of my projects:
CPPFLAGS=-g -Wall -I. -DDEBUG
LDFLAGS=-g
LDLIBS=-lopencv_core -lopencv_calib3d -lopencv_highgui -lopencv_imgproc -lopencv_stitching -lopencv_video
main: main.o AffineKalmanLP.o
g++ $(LDFLAGS) -o main main.o AffineKalmanLP.o $(LDLIBS)
main.o: main.cpp main.h
g++ $(CPPFLAGS) -c main.cpp
AffineKalmanLP.o: AffineKalmanLP.cpp AffineKalmanLP.h
g++ $(CPPFLAGS) -c AffineKalmanLP.cpp
clean:
rm main main.o AffineKalmanLP.o
You need to add it to LDLIBS.
If I remove that line, I get undefined reference for various things as well.
When I compile my code that uses OpenCV and caffe, I get the following linker errors:
../libs/libopencv_highgui.so: undefined reference to `g_mutex_lock'
../libs/libgstbase-0.10.so.0: undefined reference to `g_cond_init'
../libs/libopencv_highgui.so: undefined reference to `g_mutex_unlock'
../libs/libopencv_highgui.so: undefined reference to `g_cond_broadcast'
../libs/libgstreamer-0.10.so.0: undefined reference to `g_cond_wait_until'
../libs/libopencv_highgui.so: undefined reference to `g_mutex_new'
...
collect2: ld returned 1 exit status
This my g+ commmand:
g++ -DCPU_ONLY=1 test.cpp -o test -I../include -I../include/openblas -L../libs
-Wl,-rpath=../libs -lcaffe -lglog -lboost_system -lopencv_core -lopencv_highgui
-lopencv_imgproc
What am I missing?
Yep, this is a simple case of not linking against glib.
In general, to fix this kind of thing, google one of the symbol names(g_mutex_lock, for example) to figure out what library it comes from. In this case, it is glib. Then, look in their documentation for help compiling applications that use their library(if you can't figure it out on your own). In this case, you end up here.
Once you have made sure you have all required packages installed, you augment your compiler options as needed.
In your case, try g++ -DCPU_ONLY=1 test.cpp -o test `pkg-config --cflags glib-2.0` -I../include -I../include/openblas `pkconfig --libs glib-2.0` -L../libs -Wl,-rpath=../libs -lcaffe -lglog -lboost_system -lopencv_core -lopencv_highgui -lopencv_imgproc
Note the use of pkg-config. For reference, compiling a sample application that uses only glib might look like this: gcc hello.c `pkg-config --cflags --libs glib-2.0`
I am trying to build a sample from OpenCV. Unfortunately I always get these errors while building.
I made an installation with opencv_contrib. OpenCV is installed in ~/lib/pokus/installed (contains bin include lib share). I am buiding example ~/lib/pokus/installed/share/OpenCV/samples/cpp/opencv_version.cpp (which came with OpenCV).
My g++ command:
g++ $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) opencv_version.cpp -o test
Result:
opencv_version.cpp:(.text+0x78): undefined reference to `cv::CommandLineParser::CommandLineParser(int, char const* const*, cv::String const&)'
opencv_version.cpp:(.text+0xa8): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0xc6): undefined reference to `cv::CommandLineParser::printMessage() const'
opencv_version.cpp:(.text+0xd7): undefined reference to `cv::CommandLineParser::check() const'
opencv_version.cpp:(.text+0xea): undefined reference to `cv::CommandLineParser::printErrors() const'
opencv_version.cpp:(.text+0x113): undefined reference to `cv::CommandLineParser::has(cv::String const&) const'
opencv_version.cpp:(.text+0x12a): undefined reference to `cv::getBuildInformation()'
opencv_version.cpp:(.text+0x180): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1bc): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
opencv_version.cpp:(.text+0x1f8): undefined reference to `cv::CommandLineParser::~CommandLineParser()'
/tmp/ccRHDRg5.o: In function `cv::String::String(char const*)':
opencv_version.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccRHDRg5.o: In function `cv::String::~String()':
opencv_version.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
Content of ~/lib/pokus/installed/lib/pkgconfig/opencv.pc
# Package Information for pkg-config
prefix=~/lib/pokus/installed
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dpm -lopencv_freetype -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_plot -lopencv_dnn -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -L/usr/lib/x86_64-linux-gnu -lQt5Test -lQt5Concurrent -lQt5OpenGL -lpng -lz -ltiff -ljasper -ljpeg -lImath -lIlmImf -lIex -lHalf -lIlmThread -ldc1394 -lavcodec-ffmpeg -lavformat-ffmpeg -lavutil-ffmpeg -lswscale-ffmpeg -lQt5Core -lQt5Gui -lQt5Widgets -ldl -lm -lpthread -lrt -lGLU -lGL -ltbb
Cflags: -I${includedir_old} -I${includedir_new}
Sorry for so frequently asked question, but none of the others seems to solve my problem.
Thanks for all tips!
The file name must come before the params:
g++ opencv_version.cpp $(pkg-config --cflags --libs ~/lib/pokus/installed/lib/pkgconfig/opencv.pc) -o test
I had the same issue trying to work with OPENCV 4.3.0 after installation and so. In order to add the libs and compile properly.
Worked for me in order to test the opencv_version.cpp
g++ opencv_version.cpp $(pkg-config --cflags --libs opencv4) -o opencv_version
Result
./opencv_version
Welcome to OpenCV 4.3.0
I'm jackaroo to learn how to train features of opencv, and I refer to these under url.
http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html
And above article author recommand to learn with his project, like here.
https://github.com/mrnugget/opencv-haar-classifier-training
And I study step by step,
but in the same mould after I copy mergevec.cpp to my opencv apps's directory, like this
cp src/mergevec.cpp ~/opencv-2.4.9/apps/haartraining
cd ~/opencv-2.4.9/apps/haartraining
And then, I want to obtain executable file mergevec by using the following methods
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\
cvhaartraining.cpp\
-lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect
And I get error
/tmp/cc9GpmMW.o: In function `JpgDatasetGenerator::JpgDatasetGenerator(char const*)':
cvhaartraining.cpp:(.text+0xafd5): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
/tmp/cc9GpmMW.o: In function `PngDatasetGenerator::PngDatasetGenerator(char const*)':
cvhaartraining.cpp:(.text+0xb24d): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
cvhaartraining.cpp:(.text+0xb24d): undefined reference to `IOutput::createOutput(char const*, IOutput::OutputType)'
I try to sovle the problem by looking through opecv forum's articles and found almost nothing.
So, um, could anybody help me? thanks a lot..
IOutput is an interface where their methods are declared at ioutput.h and must be implemented somewhere. I found out they were implemented at cvsamplesoutput.cpp so we just need ask gcc to compile that file. For that the correct command should be:
g++ `pkg-config --libs --cflags opencv` -I. -o mergevec mergevec.cpp\
cvboost.cpp cvcommon.cpp cvsamples.cpp cvhaarclassifier.cpp\
cvhaartraining.cpp cvsamplesoutput.cpp\
-lopencv_core -lopencv_calib3d -lopencv_imgproc -lopencv_highgui -lopencv_objdetect
I am compiling a program with the following flags and getting errors (running 64 bit os):
g++ -lm -lml -lcvaux -lhighgui -lcv -lcxcore main.o BRIEF.o -o BRIEF_demo
I get a bunch of undefined references:
main.cpp:(.text+0x1f6): undefined reference to `cvInitMatHeader'
main.cpp:(.text+0x218): undefined reference to cvInitMatHeader'
main.o: In function_Z14drawQuadrangleP9_IplImageiiiiiiii8CvScalari.constprop.77':
main.cpp:(.text+0x2d5): undefined reference to cvLine'
main.cpp:(.text+0x333): undefined reference tocvLine'
main.cpp:(.text+0x398): undefined reference to cvLine'
main.cpp:(.text+0x3f2): undefined reference tocvLine'
Anyone have an idea how to circumvent this?
I suppose you are using the newest OpenCV 2.3.x. cvInitMatHeader() and cvLine() are actually defined in libcxcore.so, which I can see you are including.
My guess is that the order of the linking is wrong, so you need to adjust your command to something like:
g++ main.cpp BRIEF.cpp -o BRIEF_demo -lm -lml -lcvaux -lhighgui -lcv -lcxcore