compiling opencv in c++ - c++

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.

Related

G++ returning errors after attempted compilation with openCV using G++

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

g++ - Python.h: No such file or directory

I'm trying to make a C++ script that will run some simple Python code:
// t.cpp
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Upon running g++ t.cpp, I get the error:
t.cpp:1:20: fatal error: Python.h: No such file or directory
compilation terminated
I've found many similar questions, all specific to an IDE or other development software, or were solved by installing python3-dev. The python3-dev package is already installed, and I even tried manually including the header when attempting to compile:
g++ t.cpp -I ~/.virtualenvs/MainEnv/include/python3.5m/Python.h
g++ t.cpp -I /usr/include/python3.5m/Python.h
Neither changes anything.
How can I fix this error?
UPDATE: I found that using g++ t.cpp -I /usr/include/python3.5/ seems to include the header, but then it runs into more errors:
t.cpp:(.text+0x10): undefined reference to `Py_Initialize'
t.cpp:(.text+0x1f): undefined reference to `PyRun_SimpleStringFlags'
t.cpp:(.text+0x24): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
I've set up a similar example on my github
g++ t.cpp is missing a few things:
Tell g++ where the headers are for cpython (by -I/path/to/headers/)
Tell g++ to link against libpython (by -lpython3.5m)
You can also retrieve these flags with pkg-config
$ pkg-config python-3.5 --libs --cflags
-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m -lpython3.5m
Your commandline should look something like g++ -I/usr/include/python3.5m t.cpp -lpython3.5m
#include <...> is for includes that come with the compiler.
Use #include "Python.h" for any other includes.
Run the following commands to compile your code:
mytest.cpp:
#include <Python.h>
int main(int argc, char* argv[])
{
Py_Initialize();
PyRun_SimpleString("print('TEST PASSED')");
Py_Finalize();
return 0;
}
Compile:
$ g++ mytest.cpp `pkg-config python3-embed --libs --cflags` -o mytest
$ ./mytest

Compiling hello world with opencv c++ on mac os x sierra

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>

'"SDL.h" no such file or directory found' when compiling

Here's a piece of my current Makefile:
CFLAGS = -O2 -Wall -pedantic -std=gnu++11 `sdl-config --cflags --libs` -lSDL_mixer
I have libsdl installed properly, SDL.h is in /usr/include/sdl where it belongs, but it just won't compile. I also have the line #include "SDL.h" in my .h files, but still no go.
Anyone knows why?
For Simple Direct Media Layer 2 (SDL2), after installing it on Ubuntu 16.04 via:
sudo apt-get install libsdl2-dev
I used the header:
#include <SDL2/SDL.h>
and the compiler linker command:
-lSDL2main -lSDL2
Additionally, you may also want to install:
apt-get install libsdl2-image-dev
apt-get install libsdl2-mixer-dev
apt-get install libsdl2-ttf-dev
With these headers:
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
and the compiler linker commands:
-lSDL2_image
-lSDL2_ttf
-lSDL2_mixer
If the header file is /usr/include/sdl/SDL.h and your code has:
#include "SDL.h"
You need to either fix your code:
#include "sdl/SDL.h"
Or tell the preprocessor where to find include files:
CFLAGS = ... -I/usr/include/sdl ...
header file lives at
/usr/include/SDL/SDL.h
__OR__
/usr/include/SDL2/SDL.h # for SDL2
in your c++ code pull in this header using
#include <SDL.h>
__OR__
#include <SDL2/SDL.h> // for SDL2
you have the correct usage of
sdl-config --cflags --libs
__OR__
sdl2-config --cflags --libs # sdl2
which will give you
-I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
-L/usr/lib/x86_64-linux-gnu -lSDL
__OR__
-I/usr/include/SDL2 -D_REENTRANT
-lSDL2
at times you may also see this usage which works for a standard install
pkg-config --cflags --libs sdl
__OR__
pkg-config --cflags --libs sdl2 # sdl2
which supplies you with
-D_GNU_SOURCE=1 -D_REENTRANT -I/usr/include/SDL -lSDL
__OR__
-D_REENTRANT -I/usr/include/SDL2 -lSDL2 # SDL2
Most times SDL is in /usr/include/SDL. If so then your #include <SDL.h> directive is wrong, it should be #include <SDL/SDL.h>.
An alternative for that is adding the /usr/include/SDL directory to your include directories. To do that you should add -I/usr/include/SDL to the compiler flags...
If you are using an IDE this should be quite easy too...
the simplest idea is to add pkg-config --cflags --libs sdl2 while compiling the code.
g++ file.cpp `pkg-config --cflags --libs sdl2`
Having a similar case and I couldn't use StackAttacks solution as he's referring to SDL2 which is for the legacy code I'm using too new.
Fortunately our friends from askUbuntu had something similar:
Download SDL
tar xvf SDL-1.2.tar.gz
cd SDL-1.2
./configure
make
sudo make install

OpenCV hello world not compiling like it should

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)