OpenCV static linking error - opencv

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.

Related

How to configure the linking g++ with opecv properly in ubuntu 18.04?

I'm new to opencv. And I'm found out that the pkg-config --cflags --libs opencv -lopencv_core -lopencv_imgcodecs should be used to make compile smoothly. But I just don't know why keeping get the "undefined reference" error when compiling, the output results and my Makefile are as follows:
1. For outputs:
g++ -Wall -g opencv.cpp
/tmp/ccg5WiIh.o: In function `main':
/home/akatsuki/Documents/Projects/simpleTest/opencv/opencv.cpp:14: undefined reference to `cv::imread(cv::String const&, int)'
/home/akatsuki/Documents/Projects/simpleTest/opencv/opencv.cpp:25: undefined reference to `cv::namedWindow(cv::String const&, int)'
/home/akatsuki/Documents/Projects/simpleTest/opencv/opencv.cpp:26: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/home/akatsuki/Documents/Projects/simpleTest/opencv/opencv.cpp:29: undefined reference to `cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/home/akatsuki/Documents/Projects/simpleTest/opencv/opencv.cpp:31: undefined reference to `cv::waitKey(int)'
/tmp/ccg5WiIh.o: In function `cv::String::String(char const*)':
/usr/include/opencv2/core/cvstd.hpp:622: undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccg5WiIh.o: In function `cv::String::~String()':
/usr/include/opencv2/core/cvstd.hpp:664: undefined reference to `cv::String::deallocate()'
/tmp/ccg5WiIh.o: In function `cv::Mat::~Mat()':
/usr/include/opencv2/core/mat.inl.hpp:592: undefined reference to `cv::fastFree(void*)'
/tmp/ccg5WiIh.o: In function `cv::Mat::release()':
/usr/include/opencv2/core/mat.inl.hpp:704: undefined reference to `cv::Mat::deallocate()'
/tmp/ccg5WiIh.o: In function `cv::Mat::operator=(cv::Mat&&)':
/usr/include/opencv2/core/mat.inl.hpp:1215: undefined reference to `cv::fastFree(void*)'
collect2: error: ld returned 1 exit status
Makefile:16: recipe for target 'opencv.o' failed
make: *** [opencv.o] Error 1
2. For Makefile:
CC = g++
CCFLAG = -Wall -g
Linker = `pkg-config --cflags --libs opencv` -lopencv_core -lopencv_imgcodecs
all: opencv.exe
opencv.exe: opencv.o
g++ -o $# opencv.o $(Linker)
chmod a+x $#
opencv.o: opencv.cpp
$(CC) $(CCFLAG) opencv.cpp
Could anyone help me figure out the problem? Thanks~
For anyone who searches relevant issues, I leave reply.
Following command prints directories of OpenCV header files.
pkg-config --cflags opencv
// -I/usr/local/include/opencv -I/usr/local/include
Following command prints shared object files
(under /usr/local/lib directory in following case)
which are used for dynamic linking.
Dynamic linking is to link your excutable file (like main file)
to shared object files (like .so files) at runtime
pkg-config --libs opencv
// -L/usr/local/lib -lopencv_cudabgsegm -lopencv_cudaobjdetect
// -lopencv_cudastereo -lopencv_stitching -lopencv_cudafeatures2d
// -lopencv_superres -lopencv_cudacodec -lopencv_videostab
// ...
You can use above printed dependencies
(header file directories and shared object files) in your compilation command
by writing pkg-config --cflags opencv and pkg-config --libs opencv
g++ `pkg-config --cflags opencv` \
-o main \
/your_cpp_file_path/source.cpp \
`pkg-config --libs opencv`
Note that ` is backtick character, not '
backtick character in linux command is used
to use a result from command in command
For example, this part pkg-config --cflags opencv is replaced
with the result from pkg-config --cflags opencv

Linking to Qt4 library

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

Met undefined reference when use glib in ubuntu

I've searched about this a lot. And tried a lot. Just don't know where I go wrong.
Here is my code, it's very simple:
#include <glib.h>
int main()
{
int *ip=g_new(int,1);
*ip=42;
return *ip;
}
First I try apt-get libglib2.0-dev in my Ubuntu and Mint, when it's done, compile with:
gcc `pkg-config --cflags --libs glib-2.0` -o main main.c
/tmp/ccYFljQD.o: In function `main':
main.c:(.text+0x13): undefined reference to `g_malloc_n'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1
And the output of pkg-config:
$ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
So it's not working, then I try to compile from source and install one by my own.
I've apt-get libffi-dev, autogen and configure, make, make install, that's all okay.
$ pkg-config --cflags --libs glib-2.0
-I/home/donpope/software/include/glib-2.0 -I/home/donpope/software/lib/glib-2.0/include -L/home/donpope/software/lib -lglib-2.0
Yet compile with the same error:
gcc `pkg-config --cflags --libs glib-2.0` -o main main.c
/tmp/cctR3iEq.o: In function `main':
main.c:(.text+0x13): undefined reference to `g_malloc_n'
collect2: error: ld returned 1 exit status
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 1
So I need some help here. Thank you!
Update:
Later I try this in a RedHat with older gcc. And it's just okay.
I have this Makefile in my glib sandbox:
PKGS=glib-2.0
CFLAGS+=$(shell pkg-config --cflags $(PKGS))
LDFLAGS+=$(shell pkg-config --libs $(PKGS))
%: %.c
$(CC) $(CFLAGS) $< -o $# $(LIBS) $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) $< -c -o $#
%: %.o
$(CC) $< -o $# $(LIBS) $(LDFLAGS)
Starting from this and tweaking, you should be able to figure out the exact commandline and compile your program.
Also, you should make sure that your PKG_CONFIG_PATH is properly updated to contain the path where you installed glib (typically in PREFIX/lib/pkgconfig).
Ubuntu 18.04 here. I had to place all my glib flags at the end of the compile statement, even after the source .c file. So this compiles fine:
gcc -Wall -o bfs bfs.c `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0`
but this does not :
gcc -Wall -o bfs `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` bfs.c
/tmp/ccvWsLk3.o: In function `newGraph':
bfs.c:(.text+0x58): undefined reference to `g_hash_table_new'
collect2: error: ld returned 1 exit status
Which drove me nuts for a while as I was taught that it's always good style to place the source file at the end of the compile statement. :p

pkg-config not working when compiling an OpenCV program with two objects

I have written an OpenCV program which contains both main.cpp and pedestrian.cpp. I wanted to compile them, so I did the following:
g++ -c -Wall `pkg-config --cflags opencv` main.cpp -o main.o
g++ -c -Wall `pkg-config --cflags opencv` pedestrian.cpp -o pedestrian.o
g++ `pkg-config --libs opencv` pedestrian.o main.o -o detect
After the third statement, every OpenCV command I use is considered an undefined reference. I have no idea why this is happening since other single object programs work just fine with pkg-config. I checked to see if it was a namespace problem and it wasn't either. Any help would be greatly appreciated.
Try changing the order int the last line to:
g++ -o detect pedestrian.o main.o `pkg-config --libs opencv`
This should work. Order matters when linking the .o files. When the linker finds and unknown OpenCV symbol in any of the .o, it looks for its definition in the following linked elements (i.e. in the elements to the right).

pkg-config --cflags opencv: No such file or directory

I'm writing a basic example of opencv, but make command give me message
g++-4.7.real: error: pkg-config --cflags opencv: No such file or directory
g++-4.7.real: error: pkg-config --libs opencv: No such file or directory
issue command pkg-config --cflag opencv give me result as:
-I/usr/local/include/opencv -I/usr/local/include
and pkg-config --libs opencv give me:
-I/usr/local/include/opencv -I/usr/local/include
vudao#vudaopc:~/work/nmath/ntrainer$ pkg-config --libs opencv
/usr/local/lib/libopencv_contrib.a /usr/local/lib/libopencv_stitching.a /usr/local/lib/libopencv_nonfree.a /usr/local/lib/libopencv_superres.a /usr/local/lib/libopencv_ocl.a /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_videostab.a /usr/local/lib/libopencv_gpu.a /usr/local/lib/libopencv_photo.a /usr/local/lib/libopencv_objdetect.a /usr/local/lib/libopencv_legacy.a /usr/local/lib/libopencv_video.a /usr/local/lib/libopencv_ml.a /usr/local/lib/libopencv_calib3d.a /usr/local/lib/libopencv_features2d.a /usr/local/lib/libopencv_highgui.a /usr/local/share/OpenCV/3rdparty/lib/libIlmImf.a /usr/local/share/OpenCV/3rdparty/lib/liblibjasper.a /usr/local/share/OpenCV/3rdparty/lib/liblibtiff.a /usr/local/lib/libopencv_imgproc.a /usr/local/lib/libopencv_flann.a /usr/local/lib/libopencv_core.a /usr/lib/i386-linux-gnu/libbz2.so /usr/lib/i386-linux-gnu/libpng.so /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/i386-linux-gnu/libz.so -lswscale -lavformat -lavutil -lz -lSDL -lasound -lavcodec -lgthread-2.0 -lglib-2.0 -lgobject-2.0 -lfontconfig -lfreetype -lpango-1.0 -lcairo -lgdk_pixbuf-2.0 -lpangocairo-1.0 -lpangoft2-1.0 -lgio-2.0 -latk-1.0 -lgdk-x11-2.0 -lgtk-x11-2.0 -lrt -lpthread -lm -ldl -lstdc++
Below is my Makefile:
CC=g++
CFLAGS=-O2 -g 'pkg-config --cflags opencv'
LDFLAGS='pkg-config --libs opencv'
BIN=ntrainer
ntrainer : ntrainer.cpp
$(CC) $(CFLAGS) $(LDFLAGS) -o $(BIN) ntrainer.cpp
My system is Ubuntu 12.10. I have installed opencv-2.4.7 successfully (I think) following instructions here http://www.samontab.com/web/2011/06/installing-opencv-2-2-in-ubuntu-11-04/
I also have configured and exported PKG_CONFIG_PATH into /etc/bash.bashrc, I'v also ran ldconfig
Please someone tell me what I'm missing? And how to correct it.
Thanks too much.
You used incorrect quotes. You should use ` instead of ':
CC=g++
CFLAGS=-O2 -g `pkg-config --cflags opencv`
LDFLAGS=`pkg-config --libs opencv`
For me, the file was there but the path was wrong:
I had to look for the opencv.pc file, make sure it is in the path for the pkgconfig program to find (i.e. in one of the pkgconfig folders) and then make sure the prefix path in there is right.
In my case it was wrong because it was not installed via make install but i made a make package package because it was cross-compiled, and the path I had installed it at didn't match the CMAKE path.
running
pkg-config --cflags opencv
or
pkg-config --libs opencv
tells you where it is looking for the files, so you can make sure they match.

Resources