Question

I installed openCV following the link mentioned below : https://help.ubuntu.com/community/OpenCV#aStep_1

The installation went smooth, Now when I compile my test program: using the following command :

opencv openCVtest.cpp

it throws me the following error :

compiling openCVtest.cpp
g++: error: missing argument to ‘-l’
Output file => openCVtest

I looked up the script I am using to compile ( its given here ) and saw this particular line that will be used to compile my c++ code :

g++ -ggdb `pkg-config --cflags opencv` -o `basename $1 .cpp` $1 `pkg-config --libs opencv`;

I understand that "-l" is asking for some libraries, but I think I should not be passing anything other than my code to this script.

PS here is the detail of what's executed in that line:

hduser@ishan-Notebook-PC:~/Documents/OpenCv$ echo g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`

g++ -ggdb -I/usr/include/opencv -I/usr/include/opencv2 -o opencvtest opencvtest.cpp -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l

Can you guys please help me figure out , what exactly am I doing wrong? Thanks !

UPDATE

I found my opencv.pc file containing an extra "-l" I removed that flag and the above errors goes away :

contents of opencv.pc file:

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

Notice the extra "-l" in last line.

However , now I get the following error :

openCVtest.cpp:1:39: fatal error: opencv2/highgui/highgui.hpp: No such file or directory
 #include "opencv2/highgui/highgui.hpp"
Was it helpful?

Solution

Seems that pkg-config is returning one extra -l at the end. You can try running the command without it by hand:

g++ -ggdb -I/usr/include/opencv -I/usr/include/opencv2 -o opencvtest opencvtest.cpp -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy

Update:
For the second issue you are having, try to use the include paths without the opencv and opencv2 directories.

So instead of this:

Cflags: -I${includedir}/opencv -I${includedir}/opencv2

Try this:

Cflags: -I${includedir}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top