2012-02-01 23 views
17

Tengo un archivo con sólo importan:compilar OpenCV en C++

#include <iostream> 
#include <stdio.h> 

#include "cxcore.hpp" 
#include "highgui.hpp" 

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 

} 

y trato de compilar con g ++ -I/usr/include/OpenCV -lopencv -lm m.cpp

pero llegar whit de 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

esta es mi copencv contenido lib:

[email protected]:~$ 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 

estoy en ubuntu 10.10

Respuesta

39

Es necesario incluir adecuadamente la encabezados -I (capital i) y bibliotecas -l (L minúscula).

En las versiones más nuevas de OpenCV que debe hacer:

#include <cv.h> 
#include <highgui.h> 

Y luego tratar de compilarlo con:

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

Nota: si se ejecuta solamente pkg-config --cflags --libs opencv en la línea de comandos, verá el rutas y bibliotecas que debe incluir en la línea de comando g ++.

+0

suspiro. esto compila pero me conserva en este problema http://stackoverflow.com/q/9088228/433685 – nkint

+0

(problema que pensé resolver para cambiar el encabezado ...) – nkint

+0

Siempre olvido el nombre de la utilidad pkg-config. Gracias por la simple solución! – aaronsnoswell

3

si su entorno de desarrollo no tiene pkg-config y debido a esto la accepted answer by karlphilip no es práctico, o, lo que necesita saber el conjunto mínimo de bibliotecas necesarias para enlazar la aplicación, a continuación, suponiendo código como

#include <cv.h> 
#include <highgui.h> 

int main() 
{ 
    return 0; 
} 

puede agregar argumentos de la biblioteca de la siguiente lista secuencialmente desde la parte superior hasta que encuentre el conjunto mínimo de argumentos que usted necesita:

 
    -lopencv_core 
    -lopencv_imgproc 
    -lopencv_highgui 
    -lopencv_ml 
    -lopencv_video 
    -lopencv_features2d 
    -lopencv_calib3d 
    -lopencv_objdetect 
    -lopencv_contrib 
    -lopencv_legacy 
    -lopencv_flann 

Por ejemplo, el código fuente en C que aparece en la parte superior de este post com pilotes y enlaces limpiamente con solamente

gcc hello.c -o hello \ 
    -I /usr/include/opencv \ 
    -L /usr/lib \ 
    -lopencv_core \ 
    -lopencv_imgproc 

en mi viejo x86_64 Ubuntu 12.04 cuadro.

Suponiendo código C++ como

#include "core/core.hpp" 
#include "highgui/highgui.hpp" 

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
{ 
    return 0; 
} 

allí tendría que compilar y enlazar con

g++ hello.cpp -o hello \ 
    -I /usr/include/opencv2 \ 
    -L /usr/lib \ 
    -lopencv_core \ 
    -lopencv_imgproc 
+0

Intenté compilar la versión de g ++ del video de este tipo https://www.youtube.com/watch?v=cgo0UitHfp8 en la línea de comandos porque quería probarlo sin VS. Se compila, pero cuando ejecuto el programa no pasa nada. – luckyging3r

+0

No importa, es un problema con el archivo dll de Windows. Gracias por su respuesta anterior sin embargo. – luckyging3r

+0

Necesito agregar opencv_contrib pero no está en la lista de paquetes, ya que puedo verlo usando el comando pkg-config --libs opencv. – speedious