29

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

karlphillip
  • 92,053
  • 36
  • 243
  • 426
nkint
  • 11,513
  • 31
  • 103
  • 174

5 Answers5

71

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • sigh. this do compile but remaind me at this problem http://stackoverflow.com/q/9088228/433685 – nkint Feb 01 '12 at 16:45
  • I always forget the name of the pkg-config utility. Thanks for the simple solution! – aaronsnoswell Nov 28 '13 at 01:07
  • Suppose I have an openCV version installed and built in a directory (the global version is different). How should I change the command you posted to compile and build my project with the new local version of openCV? – roschach Aug 02 '18 at 12:46
  • 1
    Execute `pkg-config --cflags --libs opencv` to see the exact parameters the global version passes to **g++**. Then, just copy the same flags and use your local paths instead. Simple! – karlphillip Aug 02 '18 at 21:45
  • I installed 4.2 from source and had to do the following steps while building: https://github.com/opencv/opencv/issues/13154#issuecomment-456652297 – stephen Jan 05 '20 at 18:39
  • 1
    For opencv4, the command is `pkg-config --cflags --libs opencv4` – Cyrill Nov 14 '22 at 14:02
13

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
Community
  • 1
  • 1
Jonathan Ben-Avraham
  • 4,615
  • 2
  • 34
  • 37
  • I tried compiling the g++ version from this guy's video https://www.youtube.com/watch?v=cgo0UitHfp8 on the command line because I wanted to try it without VS. It compiles, but when I run the program nothing happens. – luckyging3r Feb 21 '17 at 04:00
  • Nevermind, It's a missing windows dll file problem. Thank you for your above answer though. – luckyging3r Feb 21 '17 at 04:20
  • I need to add opencv_contrib but it is not in the package list as i can see it using the command pkg-config --libs opencv. –  Sep 24 '17 at 08:52
  • What is reason behind that you need to put both -I (searching include file) and -L (searching file during link time)? Also would you share, why you need to put "-lopencv_core" and "-lopencv_imgproc" additionally. – Cloud Cho Jul 06 '23 at 18:47
5

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})
asendjasni
  • 963
  • 1
  • 16
  • 36
  • 1
    With this cmake file I had to change the includes to " #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" – jjcf89 Aug 07 '19 at 21:45
  • @jjcf89 great, happy to help. The use of CMake is very suitable for compiling, you should really consider using it. For me, it helped me a lot, and it's time saving too. – asendjasni Nov 08 '19 at 14:08
1
  1. Download source files in OpenCV folder and install-opencv.sh script.
  2. 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`
Giorgi Gvimradze
  • 1,714
  • 1
  • 17
  • 34
1

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.

Duloren
  • 2,395
  • 1
  • 25
  • 36