I installed opencv in RaspberryPi and configured Makefile but it can't find header files.
How to configure Makefile correctly?
I have 2 .cpp files and 1 .h file.
BlobLabeling.cpp BlobLabeling.h hand_tracking.cpp
When I run make, it returns the following:
pi#raspberrypi ~/test $ make
g++ BlobLabeling.cpp
In file included from BlobLabeling.cpp:2:0:
BlobLabeling.h:9:31: fatal error: highgui/highgui.hpp: No such file or directory
compilation terminated.
Makefile:11: recipe for target 'BlobLabeling.o' failed
make: *** [BlobLabeling.o] Error 1
in BlobLabeling.cpp
#include "BlobLabeling.h"
in hand_tracking.cpp
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
#include "BlobLabeling.h"
in BlobLabeling.h
#include "highgui/highgui.hpp"
#include "opencv.hpp"
Makefile
CXX = g++
LDFLAGS = -lopencv_legacy -lopencv_highgui -lopencv_core -lopencv_ml -lopencv_video -lopencv_imgproc -lopencv_calib3d -lopencv_objdetect -L/usr/lib
CPPFLAGS = -g -I/usr/include/opencv -I/usr/include/opencv2
all: BlobLabeling.o hand_tracking.o
g++ -o test BlobLabeling.o hand_tracking.o
BlobLabeling.o: BlobLabeling.cpp
g++ BlobLabeling.cpp
hand_tracking.o: hand_tracking.cpp BlobLabeling.h
g++ hand_tracking.cpp
in /usr/include/opencv2
pi#raspberrypi ~/test $ ls /usr/include/opencv2
calib3d features2d imgproc objdetect stitching videostab
contrib flann legacy opencv.hpp ts
core highgui ml photo video
please use the proper c++ headers:
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
along with the include path:
-I/usr/include/opencv2
instead of the outdated "cv.h" and "highgui.h"
Related
I'm trying to run a simple c++ file that includes openCV libraries and creates a simple Mat. Unfortunately, when I try and compile the file using g++, it returns a number of errors.
So far, I've tried removing the Mat definition all together and just included the openCV files, which worked! I'm new to the command line, so I'm not sure if it's an issue with the way I'm using g++. However, I have worked quite a bit on openCV in Xcode (though with Xcode, there's no need to work in the command line, as everything is neatly built for you at run time).
Also, this is my first time posting to Stack Overflow, so I may have made a mistake formatting.
This is my main.cpp file which I'm attempting to compile. As I mentioned before, if I remove the line "Mat test;", it compiles successfully and writes to console as it should.
#include <iostream>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
int main() {
Mat test;
cout << "Mat Defined!" << endl;
return(0);
}
This is the g++ command I am using:
g++ -o main.out main.cpp
and this is the error I get when I try and run it:
/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::~Mat()':
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x3c): undefined reference to `cv::fastFree(void*)'
/usr/bin/ld: /tmp/ccbbhoNd.o: in function `cv::Mat::release()':
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x68): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
Any help is greatly appreciated. I also opted out of the verbose flag for g++, as it returned a ton of lines and I wasn't sure how people feel about 50+ lines of code. Thanks so much!
You have problem with linking to OpenCV library. You need to pass to ld program information where to find all necessary lib's. In your case it will be:
g++ -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -o main.out main.cpp -lopencv_core
All OpenCV linker flags are:
-lopencv_calib3d
-lopencv_contrib
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_highgui
-lopencv_imgproc
-lopencv_legacy
-lopencv_ml
-lopencv_nonfree
-lopencv_objdetect
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab
After all I suggest to jump in CMake build system for your C++ project's. With OpenCV it's much easier to compile program. Checkout doc's. CMake will prepare for you makefile with all needed dependencies.
Best Regards!
It fails because you didn't link the libraries in your command. During compilation, your operating system can't reach opencv libraries because you didn't address them. You need to compile your cpp file using the code below:
g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -o output
Attempting to compile a very simple program to test out my recent installation of OpenCV 3.2.0. My program opencvtest.cpp is in a folder called cpp, which also holds the opencv-3.2.0 installation directory.
Here's my program.
//Include file for every supported OpenCV function
#include <opencv2/opencv.hpp>
int main( int argc, char** argv ) {
cv::Mat img = cv::imread( argv[1], -1 );
if( img.empty() ) return -1;
cv::namedWindow( "Example 2-1", cv::WINDOW_AUTOSIZE );
cv::imshow( "Example 2-1", img );
cv::waitKey( 0 );
cv::destroyWindow( "Example 2-1" );
return 0;
}
Below I've included the call to pkg-config to show what parameters I'm using, and my actual g++ command to try and compile my file.
asif.ahmed:~/cpp $ pkg-config opencv --cflags --libs
-I/usr/include/opencv -I/usr/include/opencv2 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
asif.ahmed:~/cpp $ g++ opencvtest.cpp `pkg-config opencv --cflags --libs` -o main
opencvtest.cpp:2:10: fatal error: 'opencv2/opencv.hpp' file not found
#include <opencv2/opencv.hpp>
^
1 error generated.
Can anyone else suggest anything to get this to compile? I don't have a great grasp of command line compiling C++.
Check path to opencv.hpp (is it really /usr/include/opencv2/opencv2/opencv.hpp?) - If you use -I/usr/include/opencv2 perhaps you need #include "opencv.hpp" instead of #include <opencv2/opencv.hpp>, or better use -I/usr/include/ and #include "opencv2/opencv.hpp"
First time I cannot compile hello world.
I've followed tons of tutorials how to install opencv.
I just have following example:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imageMe = imread("test.jpg", 0);
return 0;
}
from opencv website.
Looks straightforward, but it won't compile.
g++ display_image.cpp `pkg-config --cflags --libs opencv` -l
Here's the error:
display_image.cpp:2:10: fatal error: 'opencv2/core/core.hpp' file not found
#include <opencv2/core/core.hpp>
^
1 error generated.
And running:
pkg-config --cflags --libs opencv
return this:
-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
these files are present.
Running ls /usr/local/Cellar/opencv3/3.2.0/include/opencv2/core | grep core.hpp
Gives the result:
core.hpp
What's the problem?
edit:
I've been using following tutorials to install opencv on my machine:
http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/
https://www.learnopencv.com/install-opencv-3-on-yosemite-osx-10-10-x/
https://blogs.wcode.org/2014/10/howto-install-build-and-use-opencv-macosx-10-10/
http://seeb0h.github.io/howto/howto-install-homebrew-python-opencv-osx-el-capitan/
edit:
Even after all these answers I cannot compile a single app. Even if it somehow can find .hpp file directly included in my file it fails finding includes inside.
Probably my configuration is wrong.
In /usr/local/Cellar/opencv3 I have 2 directories:
3.2.0 - probably installed binaries
HEAD-9053839 - compiled from source
Now I also have opencv.pc file located in /usr/local/lib/pkgconfig:
prefix=/usr/local/Cellar/opencv3/3.2.0
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
As a prefix I've also tried HEAD-9053839.
I've created even simplier example:
#include <opencv2/opencv.hpp>
int main(){return 0; }
It's still telling me that 'opencv2/opencv.hpp' file not found.
Then I printed what pkg-config says:
pkg-config --cflags --libs opencv
-I/usr/local/Cellar/opencv3/3.2.0/include/opencv -I/usr/local/Cellar/opencv3/3.2.0/include/opencv2 -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
And found out it'd rather should be directly include dir, not opencv/opencv2 subdirectories.
So I've ran g++ like this:
g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l display_image.cpp
Then it told me:
ld: library not found for -lopencv_contrib
After removing it it said:
ld: library not found for -lopencv_legacy
Then I had to remove -l before display_image.cpp, but then I've got another error:
Undefined symbols for architecture x86_64:
"cv::String::deallocate()", referenced from:
cv::String::~String() in display_image-9d8f86.o
cv::String::operator=(cv::String const&) in display_image-9d8f86.o
This is hell.
I don't have a compilation environment to regenerate your error but I think the problem is about these lines in your code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
since the opencv directory is being searched from
/usr/local/Cellar/opencv3/3.2.0/include/opencv2
as your "pkg-config --cflags --libs opencv" implies. So, I suppose, changing the include lines into
#include <core/core.hpp>
#include <highgui/highgui.hpp>
would help, or you can implicitly declare the directory you want to search like this:
g++ -I/usr/local/Cellar/opencv3/3.2.0/include display_image.cpp ...
In OpenCV 3.x, the layout of the header files has changed and now you pretty much just use:
#include <opencv2/opencv.hpp>
So, in concrete terms, I am suggesting you replace your 2nd and 3rd lines with the above incantation.
See the OpenCV Transition Guide.
Soner's answer should solve the OP's problem. I want to add some other solutions.
If you use CMake to install openCV from source, it will put OpenCV libraries in /usr/local/lib and OpenCV headers in /usr/local/include. Where the headers all go in the subfolder /usr/local/include/opencv. Since they are default library and header directories for most IDE's, all the examples you find will work with that config.
Now, brew does not install OpenCV to the above folders as you already knew. So you have three options:
Change compiler's search paths for OpenCV libraries and headers: g++ -I/usr/local/Cellar/opencv3/3.2.0/include -L/usr/local/Cellar/opencv3/3.2.0/lib
Soft link /usr/local/Cellar/opencv3/3.2.0/include/opencv to /usr/local/include and /usr/local/Cellar/opencv3/3.2.0/lib to /usr/local/lib
Install OpenCV from source with CMake.
I did it!
It was a problem with pkg-config.
In /usr/local/Cellar/opencv3/3.2.0/lib/pkgconfig I've found opencv.pc, which looks like this:
# Package Information for pkg-config
prefix=/usr/local/Cellar/opencv3/3.2.0
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.2.0
Libs: -L${exec_prefix}/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core
Libs.private: -framework OpenCL -L/usr/local/opt/jpeg/lib -ljpeg -L/usr/local/lib -lpng -ltiff -lImath -lIlmImf -lIex -lHalf -lIlmThread -L/usr/lib -lz -framework Cocoa -framework AVFoundation -framework CoreGraphics -framework CoreMedia -framework CoreVideo -framework QuartzCore -framework Accelerate
Cflags: -I${includedir_old} -I${includedir_new}
I've replaced old opencv.pc located in: /usr/local/lib/pkgconfig
Now I'm not getting any errors.
I also had to use different include file:
#include <opencv2/opencv.hpp>
i have a file with only import:
#include <iostream>
#include <stdio.h>
#include "cxcore.hpp"
#include "highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
}
and i try to compile with g++ -I/usr/include/opencv -lopencv -lm m.cpp
but get whit error:
In file included from /usr/include/opencv/cxcore.hpp:46,
from m.cpp:5:
/usr/include/opencv/cxmisc.h:214: error: expected constructor, destructor, or type conversion before ‘void’
/usr/include/opencv/cxmisc.h:220: error: expected constructor, destructor, or type conversion before ‘int’
/usr/include/opencv/cxmisc.h:226: error: ‘CV_INLINE’ does not name a type
/usr/include/opencv/cxmisc.h:516: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_CN_MAX’ was not declared in this scope
In file included from m.cpp:5:
/usr/include/opencv/cxcore.hpp:70: error: template declaration of ‘cv::CV_EXPORTS cv::Size_’
/usr/include/opencv/cxcore.hpp:71: error: template declaration of ‘cv::CV_EXPORTS cv::Point_’
/usr/include/opencv/cxcore.hpp:72: error: template declaration of ‘cv::CV_EXPORTS cv::Rect_’
/usr/include/opencv/cxcore.hpp:77: error: expected initializer before ‘fromUtf16’
/usr/include/opencv/cxcore.hpp:78: error: expected initializer before ‘toUtf16’
/usr/include/opencv/cxcore.hpp:80: error: expected initializer before ‘format’
/usr/include/opencv/cxcore.hpp:82: error: expected initializer before ‘:’ token
m.cpp:38: error: expected ‘}’ at end of input
this is my copencv lib content:
alberto#zefiro:~$ ls /usr/include/opencv/
cvaux.h cvcompat.h cv.hpp cvtypes.h cvvidsurv.hpp cxcore.h cxerror.h cxmat.hpp cxoperations.hpp highgui.h ml.h
cvaux.hpp cv.h cvinternal.h cvver.h cvwimage.h cxcore.hpp cxflann.h cxmisc.h cxtypes.h highgui.hpp
i'm on ubuntu 10.10
You need to properly include the headers -I (capital i) and libraries -l (lowercase L).
On the newest OpenCV versions you should do:
#include <cv.h>
#include <highgui.h>
And then try to compile it with:
g++ m.cpp -o app `pkg-config --cflags --libs opencv`
Note: if you execute only pkg-config --cflags --libs opencv in the command line you will see the paths and libraries you need to include in the g++ command line.
if your development environment does not have pkg-config and because of this the accepted answer by karlphilip is not practical, or, you need to know the minimal set of libraries required to link your application, then assuming code such as
#include <cv.h>
#include <highgui.h>
int main()
{
return 0;
}
you can add library arguments from the following list sequentially from the top until you find the minimal set of arguments that you need:
-lopencv_core
-lopencv_imgproc
-lopencv_highgui
-lopencv_ml
-lopencv_video
-lopencv_features2d
-lopencv_calib3d
-lopencv_objdetect
-lopencv_contrib
-lopencv_legacy
-lopencv_flann
For example, the C source code listed at the top of this post compiles and links cleanly with only
gcc hello.c -o hello \
-I /usr/include/opencv \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
on my old x86_64 Ubuntu 12.04 box.
Assuming C++ code such as
#include "core/core.hpp"
#include "highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
return 0;
}
then you would compile and link with
g++ hello.cpp -o hello \
-I /usr/include/opencv2 \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
I suggest you use CMake to compile OpenCV with G++, this way is more suitable, I think.
cmake_minimum_required(VERSION 3.1)
project(YOUR_PROJECT_NAME)
set(CMAKE_GXX_FLAGS "-Wall -Wextra -Wconversion -pedantic -std=gnu11")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(YOUR_EXCUTABLE YOUR_CODE_SOURCE_FILES)
target_link_libraries(YOUR_EXCUTABLE ${OpenCV_LIBS})
Download source files in OpenCV folder and
install-opencv.sh script.
By running script file you
automatically install needed files for opencv. Run the following
code:
chmod +x install-opencv.sh
./install-opencv.sh
In case if you install different version of the library please update the first line of version inside the installation script.
For more information use this tutorial. Compile it with the next line:
g++ `pkg-config --cflags opencv` example.cpp `pkg-config --libs opencv`
I think that the accepted answer is a bit old.
At least for OpenCV 4.X, the most recently release today, you can use:
#include <opencv2/opencv.hpp>
and compile your program by something like:
g++ your_program_file.cpp -o your_program `pkg-config --cflags --libs opencv4`
Note opencv4 and not only opencv.
For example, using the command above to compile the following your_program_file.cpp file:
#include <opencv2/opencv.hpp>
int main() {
std::cout << "The current OpenCV version is " << CV_VERSION << "\n";
return 0;
}
And running:
./your_program
Outputs:
The current OpenCV version is 4.4.0
Which indeed matches with my current OpenCV settings.
I am setting up a new machine with OpenCV 2.3.1. The machine is a Windows 7 box, and I followed the installation instructions given by the OpenCV website (used CMake with MinGW to build).
Here is my code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
char var;
cv::Mat img;
img = cv::imread("C:/test/img.jpg");
cv::namedWindow("Image");
cv::imshow("Image", img);
std::cin >> var;
return 1;
}
Here is my make command:
g++ -o main main.cpp -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy
Here is my path:
C:\OpenCV-2.3.1\install\bin;C:\OpenCV-2.3.1\install\include;C:\QtSDK\QtCreator\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CMake 2.8\bin;C:\QtSDK\mingw\bin;
Here is my error:
main.cpp:2:33: error: opencv2/core/core.hpp: No such file or directory
main.cpp:3:39: error: opencv2/highgui/highgui.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:8: error: 'cv' has not been declared
main.cpp:8: error: expected ';' before 'img'
main.cpp:9: error: 'img' was not declared in this scope
main.cpp:9: error: 'cv' has not been declared
main.cpp:10: error: 'cv' has not been declared
main.cpp:11: error: 'cv' has not been declared
This is not making sense. Why won't this compile? Why can't it find opencv2/core/core.hpp?
g++ doesn't consider %PATH% ($PATH on Unix) when looking for include files.
Add the following to the compilation command: -IC:\OpenCV-2.3.1\install\include:
g++ -IC:\OpenCV-2.3.1\install\include -o main main.cpp -lopencv_core ...
You are not including the right OpenCV directories.
I made an installation of a previous version of OpenCV in C:\OpenCV2.3, and these are the paths I had to add for my compiler to find the headers:
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
Traditionally, g++ takes headers directories with the -I flag, which you doesn't seem to be using at all.
on unix like os
g++ filename.cpp -o exec `pkg-config opencv cvblob --libs --cflags`
(cvblob optional if you are checking for blob)