15

I'm trying to compile a sample program after installing Opencv with the command:

g++ hello-world.cpp -o hello-world -I /usr/local/include/opencv -L /usr/local/lib -lm -lcv -lhighgui -lcvaux

however, I'm getting an error that says:

/usr/bin/ld: cannot find -lcv  
/usr/bin/ld: cannot find -lhighgui  
/uer/bin/ld: cannot find -lcvaux
collect2: ld returned 1 exit status

What do I need to do to correct this?? I installed opencv by downloading the latest stable version and using cmake to create the build files, then ran make install from the command line.

Was there anything I may have missed?

3 Answers 3

34

UPDATED-

Better use this command:

g++ opencv.cpp -o opencv -L `pkg-config --cflags --libs opencv`

The pkg-config command will locate the correct include and library for your source code.
For better handling with OpenCV programming go with an IDE like code::block.

Maybe this tutorial will help you in OpenCV programming with code::block:
How to Setup OpenCV for code :: block in Linux and Windows?

Sign up to request clarification or add additional context in comments.

1 Comment

For OpenCV 4 you should use g++ opencv.cpp -o opencv -L `pkg-config --cflags --libs opencv4`
21

Recently I started using OpenCV and I got similar problem and for me this works really well:

-lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_objdetect

Hope it will solve your problem.

1 Comment

with the addition of -lopencv_videoio this worked for me but i don't understand why we need to do this? I mean i already included the libraries and pkg-config lists all libraries. I started learning c++ recently but this is so different than other languages.
9

You need to add another -L argument specifying the actual location of the OpenCV libraries.

9 Comments

so instead of -L /usr/local/lib have -L <library location>? or an additional -L... and where would I find the libraries? When I do a make install, it shows me all the files are up to date and the location... but theres so many different things... all the .h files are in /usr/local/include/opencv... do the library files have a different extension?
Go through the cmake output to find out.
thanks for your quick response - editied my above comment but by the time I did, you had already responded. I think I may be unclear on what I'm really looking for. When I do a make install, it shows me all the files are up to date and the location... but theres so many different things... all the .h files are in /usr/local/include/opencv... do the library files have a different extension?
ok, all of those seem to be in /usr/local/lib/ - let me try this out - thanks
Then your -l arguments are wrong. Try -lopencv_highgui and the like.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.