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

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.

Related

I got an error while trying to compile the darknet. [darknet] Error1

I got an error while doing Makefile in jetson nano
This is an error message.
chmod +x *.sh
g++ -std=c++11 -std=c++11 -Iinclude/ -I3rdparty/stb/include -DOPENCV_DNN `pkg-config --cflags libconfig++` `pkg-config --cflags libcurl` `pkg-config --cflags sqlite3` `pkg-config --cflags libxml-2.0` `pkg-config --cflags libhttpserver` `pkg-config --cflags live555` `pkg-config --cflags libswscale` `pkg-config --cflags x264` -DOPENCV `pkg-config --cflags opencv4 2> /dev/null || pkg-config --cflags opencv` -DGPU -I/usr/local/cuda/include/ -DCUDNN -DCUDNN_HALF -Wall -Wfatal-errors -Wno-unused-result -Wno-unknown-pragmas -fPIC -Ofast -DOPENCV_DNN -DRTSP -DOPENCV -DGPU -DCUDNN -I/usr/local/cudnn/include -DCUDNN_HALF -fPIC obj/image_opencv.o obj/http_stream.o obj/gemm.o obj/utils.o obj/dark_cuda.o obj/convolutional_layer.o obj/list.o obj/image.o obj/activations.o obj/im2col.o obj/col2im.o obj/blas.o obj/crop_layer.o obj/dropout_layer.o obj/maxpool_layer.o obj/softmax_layer.o obj/data.o obj/matrix.o obj/network.o obj/connected_layer.o obj/cost_layer.o obj/parser.o obj/option_list.o obj/darknet.o obj/detection_layer.o obj/captcha.o obj/route_layer.o obj/writing.o obj/box.o obj/nightmare.o obj/normalization_layer.o obj/avgpool_layer.o obj/coco.o obj/dice.o obj/yolo.o obj/detector.o obj/layer.o obj/compare.o obj/classifier.o obj/local_layer.o obj/swag.o obj/shortcut_layer.o obj/activation_layer.o obj/rnn_layer.o obj/gru_layer.o obj/rnn.o obj/rnn_vid.o obj/crnn_layer.o obj/demo.o obj/tag.o obj/cifar.o obj/go.o obj/batchnorm_layer.o obj/art.o obj/region_layer.o obj/reorg_layer.o obj/reorg_old_layer.o obj/super.o obj/voxel.o obj/tree.o obj/yolo_layer.o obj/gaussian_yolo_layer.o obj/upsample_layer.o obj/lstm_layer.o obj/conv_lstm_layer.o obj/scale_channels_layer.o obj/sam_layer.o obj/convolutional_kernels.o obj/activation_kernels.o obj/im2col_kernels.o obj/col2im_kernels.o obj/blas_kernels.o obj/crop_layer_kernels.o obj/dropout_layer_kernels.o obj/maxpool_layer_kernels.o obj/network_kernels.o obj/avgpool_layer_kernels.o obj/pw_encrypt.o -o darknet -lm -pthread `pkg-config --libs libconfig++` `pkg-config --libs libcurl` `pkg-config --libs sqlite3` `pkg-config --libs libxml-2.0` `pkg-config --libs libhttpserver` `pkg-config --libs live555` `pkg-config --libs libswscale` `pkg-config --libs x264` `pkg-config --libs opencv4 2> /dev/null || pkg-config --libs opencv` -L/usr/local/cuda/lib64 -lcuda -lcudart -lcublas -lcurand -L/usr/local/cudnn/lib64 -lcudnn -lstdc++ -lcrypt
Package sqlite3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sqlite3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sqlite3' found
Package sqlite3 was not found in the pkg-config search path.
Perhaps you should add the directory containing `sqlite3.pc'
to the PKG_CONFIG_PATH environment variable
No package 'sqlite3' found
obj/parser.o: In function `parse_implicit':
parser.c:(.text+0x4b54): undefined reference to `make_implicit_layer'
obj/parser.o: In function `save_implicit_weights':
parser.c:(.text+0x85d4): undefined reference to `pull_implicit_layer'
obj/parser.o: In function `load_implicit_weights':
parser.c:(.text+0x9b84): undefined reference to `push_implicit_layer'
collect2: error: ld returned 1 exit status
Makefile.nano:249: recipe for target 'darknet' failed
make: *** [darknet] Error 1
I set it up as follows and compiled it, but an error occurred. Do you know what the problem is?
GPU=1
CUDNN=1
CUDNN_HALF=1
OPENCV=1
AVX=0
OPENMP=0
LIBSO=1
OPENCV_DNN=1
TENSORRT=0
RTSP=1
1. Copy the cudNN files that are part of jetpack to the correct location for darknet to use /usr/local/cuda/
# add the cudnn header file to the include folder
sudo cp /usr/include/cudnn.h /usr/local/cuda/include
# copy the cudnn shared object library files to the cuda library folder
sudo cp -P /usr/lib/aarch64-linux-gnu/libcudnn* /usr/local/cuda/lib64
# change the ownership and read writes for the jetson nano user
sudo chmod a+r /usr/local/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
2. Compile darknet using build_darknet_nano.sh
Turns out that src/representation_layer.c wasn't getting compiled and that's why the weird error was turning up saying that it can't find the functions. I solved this by including the c file in the build commands.
I have created this build_darknet_nano.sh script which you can run to compile darknet on jetson nano:
https://gist.github.com/Raunak-Singh-Inventor/4f1a20808bf2579ec938f8e050bf6514
To run this:
save this code in a file called build_darknet_nano.sh
chmod u+x build_darknet_nano.sh
./build_darknet_nano.sh
And there you go. That should succesfully compile darknet on the Jetson Nano. Try running ./darknet to see if everything works.

"opencv2: no such file or directory" while trying to build Darknet?

I am trying to use Darkent with OpenCV and CUDA. I installed darknet according to these instructions:
https://pjreddie.com/darknet/install/
I installed CUDA according to these instructions:
https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html
Finally, I installed OpenCV according to these instructions:
http://www.linuxfromscratch.org/blfs/view/svn/general/opencv.html
I then added the following lines to the end of my bashrc:
export PATH=/usr/local/cuda-11.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-11.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}
Next, I modified the Makefile in the darknet directory such that GPU=1, and OPENCV=1. I remade, and ran into a bunch of repeated errors saying:
No package 'opencv' found
gcc -Iinclude/ -Isrc/ -DOPENCV `pkg-config --cflags opencv` -DGPU -I/usr/local/cuda/include/ -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DOPENCV -DGPU -c ./src/lstm_layer.c -o obj/lstm_layer.o
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
I checked, and although I had added the directory "/usr/local/lib/pkgconfig" to my PKG_CONFIG_PATH, there was no opencv.pc file there. I googled this, and read an answer that suggested to create the file manually, so this is what I did, with the following content:
prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
This solved the repeating error mentioned above, but I am still getting a similar error when I make:
./src/image_opencv.cpp:5:10: fatal error: opencv2/opencv.hpp: No such file or directory
5 | #include "opencv2/opencv.hpp"
| ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
I'm not sure if I fixed the first issue, and this is a separate one, or if the first fix just fixed a symptom, and not the problem. Since then, I also tried:
sudo apt install libopencv-dev
to no effect.
pkg-config --modversion
produces: 2.x.x
pkg-config --cflags opencv
produces:
[code]
-I/usr/include/opencv -I/usr/include/opencv2
[/code]
Any help would be greatly appreciated. I am running ubuntu 20.04, kernel 5.4.0-53-generic.
I tried this and it worked, create opencv.pc file with these contents:
# Package Information for pkg-config
prefix=/usr
exec_prefix=${prefix}
libdir=${prefix}/lib/x86_64-linux-gnu
includedir_new=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.0
Libs: -L${libdir} -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_new}
and place it in /usr/local/lib/pkgconfig/.

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).

Resources