I'm using Kubuntu and have some Problems with linking OpenCv ( 2.4.6.1 ) Librarys ( I guess ).
So I've been using this tutorial http://karytech.blogspot.de/2012/05/opencv-24-on-ubuntu-1204.html and got the installation tested ok ( section: ./opencv_test_core ). Then I downloaded the testfile below but could make it work. Tried some other code always the same error:
g++ -L/usr/local/lib -lcv -lcxcore -lcvaux -lhighui -lm hello.cpp
/usr/bin/ld: cannot find -lcv
/usr/bin/ld: cannot find -lcxcore
/usr/bin/ld: cannot find -lcvaux
/usr/bin/ld: cannot find -lhighui
collect2: Fehler: ld gab 1 als Ende-Status zurück ( ld exit status is 1 )
( as for the strange library names in the tutorial i also tried -lopencv_calib3d ... )
then tried it in code::blocks and get this:
main.c undefined reference to cvGetRows
Headers are in /usr/local/include/opencv
/usr/local/include/opencv2
Librarys are in usr/local/lib
( If that helps ):
$ pkg-config --cflags opencv
returns: -I/usr/local/include/opencv -I/usr/local/include
$ pkg-config --libs opencv
returns: /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so ...
$ apt-cache search opencv
returns: opencv-doc - OpenCV documentation and examples
python-opencv - Python-Anbindungen für die OpenCV-Bibliothek
libavcodec-extra-53 - Libav codec library ...
To link against OpenCV, you have to pass g++ (which will run ld internally) where the libraries are (if they are not standard ones). You can do it manually, if you want to link only some of them.
But the fastest way is to rely on pkg-config:
g++ faceDetect.cpp $(pkg-config --libs opencv --cflags)
However, in the package there's a Makefile, just cd the directory you've extracted that archive, and run make.
Related
i want to compile opencv4.0 with Makefile but undefined reference error occur.
i have used to opencv in Windows and code is just simple code that only show image for test in ubuntu18.10.
but it work if i typing line below on shell.
g++ -o simple main.cpp $(pkg-config opencv4 --libs --cflags)
my Makefile is below
CC = g++
CFLAGS = -W -Wall
SRCS = main.cpp
TARGET = simple
OPENCV = $(pkg-config opencv4 --libs --cflags)
LIBS = $(OPENCV)
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
clean:
rm -f $(OBJECTS) $(TARGET) core
and my opencv4.pc is below.
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.0.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_b
Libs.private: -ldl -lm -lpthread -lrt -L/usr/lib/x86_64-linux-gnu -lGL -lGLU
Cflags: -I${includedir}
and error is below.
g++ -W -Wall -o simple main.cpp
/usr/bin/ld: /tmp/cciHsvhP.o: in function `main':
main.cpp:(.text+0x70): undefined reference to `cv::imread(cv::String const&, int)'
/usr/bin/ld: main.cpp:(.text+0xc4): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
....
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: simple] Error 1
I assume you're using GNU Make, since you are working on Ubuntu Linux.
I also assume what you have posted as:
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
is mis-formatted in the posting and that your Makefile really contains:
$(TARGET):$(SRCS)
(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
with the recipe command tab-indented as it must be.
In your Makefile you believe that:
OPENCV = $(pkg-config opencv4 --libs --cflags)
is a shell-expansion of the command pkg-config opencv4 --libs --cflags that
assigns the output of the command to the Make variable OPENCV. It is not.
It is simply a Make expansion of the string pkg-config opencv4 --libs --cflags,
just as in the next line:
LIBS = $(OPENCV)
$(OPENCV) is the Make expansion of the Make variable OPENCV and not the shell
expansion of a shell command OPENCV.
The string pkg-config opencv4 --libs --cflags is not a Make variable that has a value (obviously).
Neither can it be an invocation of a GNU Make-function
$(pkg-config ...), as there is no such GNU Make function.
So $(pkg-config opencv4 --libs --cflags) expands to nothing. Hence:
LIBS = $(OPENCV)
makes $(LIBS) expand to nothing, and:
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
expands to the same as:
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS)
which is why the opencv libraries are missing from your linkage and the linkage fails.
To assign the output of a shell command to a Make variable, using the $(shell ...) function:
OPENCV := $(shell pkg-config opencv4 --libs --cflags)
Then $(OPENCV) and $(LIBS) will acquire the correct value.
BTW...
Note that your clean recipe attempts to delete a file, core, that is never created by your Makefile.
And..
Be aware that the recipe:
$(TARGET):$(SRCS)
(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
represents in general the most inefficient possible way of automating a
program's build process with Make, because it will recompile all of the N
source files in $(SRCS) whenever you change even 1 of them. In your particular
case as posted, it doesn't matter because N = 1. But when N = 1 there is no
need for Make. In anticipation of writing more professional projects where N is large, you
might like to work through Chapter 2 An Introduction to Makefiles,
at least, in the GNU Make manual.
This part of your makefile is likely broken:
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
It looks like a leading tab character is missing on the second line.
It is not used, so make uses the default rule for building executables from .cpp files, which does not use the LIBS variable (only CFLAGS).
I am trying to compile a short test program that requires Qt4, but have trouble properly linking to the Qt4 library. I have installed Qt4 via
sudo apt-get install qt4-dev-tools
The program code looks like this
#include <QtCore>
#include <iostream>
int main() {
std::cout << "Qt version: " << qVersion() << std::endl;
}
The shared library libQtCore.so is at /usr/lib/x86_64-linux-gnu but trying to compile the following way
g++ -L/usr/lib/x86_64-linux-gnu -Wall -o test.exe test.cpp -lQtCore
returns an error message that there is no file or directory called QtCore.
I have also tried to directly use the QtCore source code, but have received the following error message:
/tmp/ccljEHOY.o: In function main':
test.cpp:(.text+0xa): undefined reference toqVersion()'
collect2: error: ld returned 1 exit status
What am I doing wrong here?
Thanks
Ips
The compiler was not able to find QtCore file because you need to add path with Qt to the list of directories to be searched for header files.
You can use the pkg-config to get proper flags:
$:pkg-config QtCore --cflags
-DQT_SHARED -I/usr/include/qt4 -I/usr/include/qt4/QtCore
For linking:
$:pkg-config QtCore --libs
-lQtCore
You can use pkg-config when calling the compiler:
g++ test.cpp `pkg-config QtCore --cflags --libs`
or
g++ `pkg-config QtCore --cflags` test.cpp `pkg-config QtCore --libs`
Note that the following way won't work:
g++ `pkg-config QtCore --cflags --libs` test.cpp
#include <opencv2/opencv.hpp> //头文件
using namespace cv; //包含cv命名空间
int main()
{
Mat img=imread("cornea.jpg");
imshow("src",img);
waitKey(0);
return 0;
}
And I compile it with:
g++ test.cpp -o test pkg-config opencv --cflags --libs opencv
Which gives me:
/usr/bin/ld: warning: libicui18n.so.54, needed by //home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)<br/>
/usr/bin/ld: warning: libicuuc.so.54, needed by //home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)<br/>
/usr/bin/ld: warning: libicudata.so.54, needed by //home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5, not found (try using -rpath or -rpath-link)<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_clone_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘uenum_next_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘u_strToLower_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_getStandardName_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucol_setAttribute_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_setMillis_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucol_strcoll_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_setSubstChars_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_getTimeZoneDisplayName_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_openCountryTimeZones_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_fromUnicode_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_open_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_getDefaultName_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucol_open_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucol_close_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_inDaylightTime_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucol_getSortKey_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_getAvailableName_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_close_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_get_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_openTimeZoneIDEnumeration_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_getDSTSavings_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_open_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_openTimeZones_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_toUnicode_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘u_strToUpper_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘u_errorName_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_close_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘uenum_close_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_countAvailable_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_getMaxCharSize_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_getAlias_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucal_getDefaultTimeZone_54’未定义的引用<br/>
//home/lmk/Qt5.5.0/5.5/gcc/lib/libQt5Core.so.5:对‘ucnv_compareNames_54’未定义的引用<br/>
collect2: error: ld returned 1 exit status
The backticks in command line pkg-config opencv --cflags --libs opencv is invisible. I have not got the reason now.
Indeed,in early days, I have installed Qt. But now, I do not want my program have something to do with Qt. In order that my program is not affected by the Qt library function, I shielded the path variables of Qt in the /etc/bash.bashrc file and installed a new gcc of 5.3.0. But the same error is still existing!
I'm using Ubuntu 14.04
Download ICU-54.1 from here
http://www.linuxfromscratch.org/blfs/view/7.7/general/icu.html
and install manually.
For me this solved the problem.
I try to compile some files from the opencv-2.4.8/apps/haarfinder but i get the following error:
ld: library not found for -llibtbb.dylib
Notice the double l in the filename.
I try to follow the tutorial here:
http://coding-robin.de/2013/07/22/train-your-own-opencv-haar-classifier.html
And in the comments i readed:
After many, many hours of pondering and looking around. I decided to
take it as a spelling error in some file so I decided to look into
opencv files and search for that "-llibtbb" and finally found it. I
corrected it to only "libtbb" and its finally working.
Sadly he didn't mention what file that was.
I tried with sublime 'Find in files' but i can't find it.
Can someone else take a look or help me in another way?
Like if i know where libtbb.dylib is stored i might be able to duplicate it and add the spelling error myself.
please help, i'm lost :)
Suppose libtbb.dylib is in /usr/local/lib/libtbb.dylib and opencv.pc is in /usr/local/lib/pkgconfig (The location may vary depending on where you install tbb and pkg-config)
Then edit /usr/local/lib/pkgconfig/opencv.pc, change -llibtbb.dylib to /usr/local/lib/libtbb.dylib
Old question, but I needed it, or rather I found this better solution - and this may help other searchers.
Follow the instructions in the github description, rather than that blog post: https://github.com/mrnugget/opencv-haar-classifier-training
This corrects the spelling error in the command line:
g++ `pkg-config --libs --cflags opencv | sed 's/libtbb\.dylib/tbb/'`\
-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
I installed opencv 2.4.12 with tbb from homebrew on El Capitan, and then separately downloaded the source from github in order to compile mergevec and the s/libtbb\.dylib/tbb/ fix on its own still didn't help.
My fix was to add -L/usr/local/lib to the start of the g++ arguments as by default it wasn't searching for my homebrew libs. I also had to add cvsamplesoutput.cpp as mentioned in another answer to fix the following error:
Undefined symbols for architecture x86_64:
"IOutput::createOutput(char const*, IOutput::OutputType)", referenced from:
JpgDatasetGenerator::JpgDatasetGenerator(char const*) in cvhaartraining-8f5a1b.o
PngDatasetGenerator::PngDatasetGenerator(char const*) in cvhaartraining-8f5a1b.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The final command that worked for me was:
g++ -L/usr/local/lib `pkg-config --libs --cflags opencv | sed 's/libtbb\.dylib/tbb/'`\
-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
If the libtbb.dylib file already exists like in my case in the /usr/local/lib folder then all you need to do is to run this command:
export DYLD_LIBRARY_PATH=/usr/local/lib
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 | sed 's/libtbb\.dylib/tbb/' ` -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 want to use OpenCV as a static library. After compiling the library as a static library now i'm testing with some examples to see all the dependencies.
The dependencies added to compile are -staticpkg-config --libs opencv-lpthread -ljpeg -ltiff -lz -ljasper -lpng12
But obtain the following Error message:
g++ -c -g -Wall -O2 `pkg-config --cflags opencv` formas.cpp
g++ -g -Wall -O2 `pkg-config --cflags opencv` formas.o -o paint -static `pkg-config --libs opencv` -lpthread -ljpeg -ltiff -lz -ljasper -lpng12
/usr/local/lib/libopencv_core.a(system.o): In function `cv::tempfile(char const*)':
system.cpp:(.text._ZN2cv8tempfileEPKc+0x3a): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
/usr/local/lib/libopencv_core.a(system.o): In function `cv::getTickCount()':
system.cpp:(.text._ZN2cv12getTickCountEv+0x2a): undefined reference to `clock_gettime'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readHeader()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xde): undefined reference to `png_set_longjmp_fn'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x134): undefined reference to `png_set_longjmp_fn'
/usr/local/lib/libopencv_highgui.a(grfmt_png.o): In function `cv::PngDecoder::readData(cv::Mat&)':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x141): undefined reference to `png_set_longjmp_fn'
collect2: ld returned 1 exit status
make: *** [all] Error 1
I made something wrong or there an error in OpenCV library.
OpenCV version: 2.3.1 (latest version)
formas --> example which create an image an draw some figures and save into a file. So simple.
Thanks in advance.
It seems you have compiled OpenCV with libpng 1.4 or newer but trying to link with libpng 1.2
What cmake command have you used to configure OpenCV?
Update:
You need to add -lrt to the list of libraries you link for the clock_gettime function.