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)
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
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"
Hi guys I am doing an application in c++ for ARM. I have a application prototype which I compiled with cross compilation and it works very nice in the ARM. What I did was this:
First I got the .o file like this
arm-linux-g++ -c PrototipoTRU.cpp
Then I got the .exe like this (My application use thread)
arm-linux-g++ PrototipoTRU.o -o tru2 -pthread
and everything works perfect.
My problem is when I tried to compile an .cpp file which use OpenCV. I tried that:
First I tried to got the .o:
arm-linux-g++ -c camera.cpp
That does not work, I got this output:
IPCamera.cpp:5:30: error: opencv2/opencv.hpp: No such file or directory
IPCamera.cpp:6:39: error: opencv2/highgui/highgui.hpp: No such file or directory
IPCamera.cpp:7:39: error: opencv2/imgproc/imgproc.hpp: No such file or directory
IPCamera.cpp:11:22: error: X11/Xlib.h: No such file or directory
IPCamera.cpp:16: error: 'cv' is not a namespace-name
IPCamera.cpp:16: error: expected namespace-name before ';' token
IPCamera.cpp: In function 'int main(int, char**)':
IPCamera.cpp:46: error: 'cv' has not been declared
IPCamera.cpp:46: error: expected ';' before 'cap'
IPCamera.cpp:52: error: 'Display' was not declared in this scope
IPCamera.cpp:52: error: 'disp' was not declared in this scope
IPCamera.cpp:52: error: 'XOpenDisplay' was not declared in this scope
IPCamera.cpp:53: error: 'Screen' was not declared in this scope
IPCamera.cpp:53: error: 'scrn' was not declared in this scope
IPCamera.cpp:53: error: 'DefaultScreenOfDisplay' was not declared in this scope
IPCamera.cpp:63: error: 'cv' has not been declared
IPCamera.cpp:63: error: expected ';' before 'frame'
IPCamera.cpp:66: error: 'cv' has not been declared
IPCamera.cpp:66: error: 'CV_WINDOW_NORMAL' was not declared in this scope
IPCamera.cpp:68: error: 'cvMoveWindow' was not declared in this scope
IPCamera.cpp:73: error: 'CV_WND_PROP_FULLSCREEN' was not declared in this scope
IPCamera.cpp:73: error: 'CV_WINDOW_FULLSCREEN' was not declared in this scope
IPCamera.cpp:73: error: 'cvSetWindowProperty' was not declared in this scope
IPCamera.cpp:79: error: 'cap' was not declared in this scope
IPCamera.cpp:82: error: 'frame' was not declared in this scope
IPCamera.cpp:89: error: 'cv' has not been declared
IPCamera.cpp:92: error: 'cv' has not been declared
So it seem that there is some problem with the link, however if do this:
g++ -c IPCamera.cpp
I got the .o file, but obviously when I get the .exe it does not work in the ARM . What I do not undestand is why if I compile an application without opencv, like the first example, the arm-linux-g++ works propertly and when I try to compile the openCV application no.
I tried to compiler also like this:
arm-linux-g++ -c IPCamera.cpp `pkg-config opencv --libs --cflags`
but is the same result, however if I do this:
g++ -c IPCamera.cpp `pkg-config opencv --libs --cflags`
It works. So I guess that is a path problem but I do not have idea how I can solved it.
Someone can help me??
Thank you guys
Hi dennisfen this is the content of mi file:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(GCC_COMPILER_VERSION "4.6" CACHE STRING "GCC Compiler version")
set(FLOAT_ABI_SUFFIX "")
if (NOT SOFTFP)
set(FLOAT_ABI_SUFFIX "hf")
endif()
set(CMAKE_C_COMPILER arm-linux-
gnueabi${FLOAT_ABI_SUFFIX}-gcc-${GCC_COMPILER_VERSION})
set(CMAKE_CXX_COMPILER arm-linux-
gnueabi${FLOAT_ABI_SUFFIX}-g++-${GCC_COMPILER_VERSION})
set(ARM_LINUX_SYSROOT /usr/arm-linux-gnueabi${FLOAT_ABI_SUFFIX} CACHE PATH "ARM cross
compilation system root")
set(CMAKE_CXX_FLAGS "" CACHE STRING "c++ flags")
set(CMAKE_C_FLAGS "" CACHE STRING "c flags")
set(CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "shared linker flags")
set(CMAKE_MODULE_LINKER_FLAGS "" CACHE STRING "module linker flags")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,nocopyreloc" CACHE STRING "executable linker
flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mthumb -fdata-sections -Wa,--noexecstack
-fsigned-char -Wno-psabi")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mthumb -fdata-sections -Wa,--noexecstack
-fsigned-char -Wno-psabi")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")
It seems that you need to set include path to your cross OpenCV installation:
arm-linux-g++ -I/path/to/opencv/include IPCamera.cpp -o tru2
When you call pkg-config it reports settings for you host system not for your arm cross toolchain.
As #dennisfen suggest you may need to set include path as well as the library path to your cross OpenCV with the libraries you are using as follows:
arm-linux-g++ -I/path/to/opencv/include -L/path/to/library IPCamera.cpp -o tru2 -lopencv_core -pthread
Hi guys I am doing an application in c++ for ARM. I have a application prototype which I compiled with cross compilation and it works very nice in the ARM. What I did was this:
First I got the .o file like this
arm-linux-g++ -c PrototipoTRU.cpp
Then I got the .exe like this (My application use thread)
arm-linux-g++ PrototipoTRU.o -o tru2 -pthread
and everything works perfect.
My problem is when I tried to compile an .cpp file which use OpenCV. I tried that:
First I tried to got the .o:
arm-linux-g++ -c camera.cpp
That does not work, I got this output:
IPCamera.cpp:5:30: error: opencv2/opencv.hpp: No such file or directory
IPCamera.cpp:6:39: error: opencv2/highgui/highgui.hpp: No such file or directory
IPCamera.cpp:7:39: error: opencv2/imgproc/imgproc.hpp: No such file or directory
IPCamera.cpp:11:22: error: X11/Xlib.h: No such file or directory
IPCamera.cpp:16: error: 'cv' is not a namespace-name
IPCamera.cpp:16: error: expected namespace-name before ';' token
IPCamera.cpp: In function 'int main(int, char**)':
IPCamera.cpp:46: error: 'cv' has not been declared
IPCamera.cpp:46: error: expected ';' before 'cap'
IPCamera.cpp:52: error: 'Display' was not declared in this scope
IPCamera.cpp:52: error: 'disp' was not declared in this scope
IPCamera.cpp:52: error: 'XOpenDisplay' was not declared in this scope
IPCamera.cpp:53: error: 'Screen' was not declared in this scope
IPCamera.cpp:53: error: 'scrn' was not declared in this scope
IPCamera.cpp:53: error: 'DefaultScreenOfDisplay' was not declared in this scope
IPCamera.cpp:63: error: 'cv' has not been declared
IPCamera.cpp:63: error: expected ';' before 'frame'
IPCamera.cpp:66: error: 'cv' has not been declared
IPCamera.cpp:66: error: 'CV_WINDOW_NORMAL' was not declared in this scope
IPCamera.cpp:68: error: 'cvMoveWindow' was not declared in this scope
IPCamera.cpp:73: error: 'CV_WND_PROP_FULLSCREEN' was not declared in this scope
IPCamera.cpp:73: error: 'CV_WINDOW_FULLSCREEN' was not declared in this scope
IPCamera.cpp:73: error: 'cvSetWindowProperty' was not declared in this scope
IPCamera.cpp:79: error: 'cap' was not declared in this scope
IPCamera.cpp:82: error: 'frame' was not declared in this scope
IPCamera.cpp:89: error: 'cv' has not been declared
IPCamera.cpp:92: error: 'cv' has not been declared
So it seem that there is some problem with the link, however if do this:
g++ -c IPCamera.cpp
I got the .o file, but obviously when I get the .exe it does not work in the ARM . What I do not undestand is why if I compile an application without opencv, like the first example, the arm-linux-g++ works propertly and when I try to compile the openCV application no.
I tried to compiler also like this:
arm-linux-g++ -c IPCamera.cpp `pkg-config opencv --libs --cflags`
but is the same result, however if I do this:
g++ -c IPCamera.cpp `pkg-config opencv --libs --cflags`
It works. So I guess that is a path problem but I do not have idea how I can solved it.
Someone can help me??
Thank you guys
Hi dennisfen this is the content of mi file:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(GCC_COMPILER_VERSION "4.6" CACHE STRING "GCC Compiler version")
set(FLOAT_ABI_SUFFIX "")
if (NOT SOFTFP)
set(FLOAT_ABI_SUFFIX "hf")
endif()
set(CMAKE_C_COMPILER arm-linux-
gnueabi${FLOAT_ABI_SUFFIX}-gcc-${GCC_COMPILER_VERSION})
set(CMAKE_CXX_COMPILER arm-linux-
gnueabi${FLOAT_ABI_SUFFIX}-g++-${GCC_COMPILER_VERSION})
set(ARM_LINUX_SYSROOT /usr/arm-linux-gnueabi${FLOAT_ABI_SUFFIX} CACHE PATH "ARM cross
compilation system root")
set(CMAKE_CXX_FLAGS "" CACHE STRING "c++ flags")
set(CMAKE_C_FLAGS "" CACHE STRING "c flags")
set(CMAKE_SHARED_LINKER_FLAGS "" CACHE STRING "shared linker flags")
set(CMAKE_MODULE_LINKER_FLAGS "" CACHE STRING "module linker flags")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,nocopyreloc" CACHE STRING "executable linker
flags")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mthumb -fdata-sections -Wa,--noexecstack
-fsigned-char -Wno-psabi")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mthumb -fdata-sections -Wa,--noexecstack
-fsigned-char -Wno-psabi")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_MODULE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--fix-cortex-a8 -Wl,--no-undefined -Wl,--gc-
sections -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now ${CMAKE_EXE_LINKER_FLAGS}")
It seems that you need to set include path to your cross OpenCV installation:
arm-linux-g++ -I/path/to/opencv/include IPCamera.cpp -o tru2
When you call pkg-config it reports settings for you host system not for your arm cross toolchain.
As #dennisfen suggest you may need to set include path as well as the library path to your cross OpenCV with the libraries you are using as follows:
arm-linux-g++ -I/path/to/opencv/include -L/path/to/library IPCamera.cpp -o tru2 -lopencv_core -pthread
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.