C++ Cannot Compile OpenCV Programs

Asked 2 years ago, Updated 2 years ago, 161 views

The following C++ programs cannot be compiled. What is the cause?

#include "stdio.h"
# include "highgui.h"
# include "cv.h"

int main(intargc, char**argv){

    IplImage*img=cvLoadImage(argv[1]);

    cvNamedWindow("scrot.png", CV_WINDOW_AUTOSIZE);

    cvShowImage("scrot.png", img);

    cvReleaseImage(&img);

    cvDestroyWindow("scrot.png");

}

Error Messages

reg++-Wall-o "rei2-1" "rei2-1.cpp" (directory: /home/pi/Desktop/C)/tmp/ccWxBQc9.o:In function main': 
rei2-1.cpp: (.text+0x28): Undefined reference to cvLoadImage'
rei2-1.cpp: (.text+0x3c): Undefined reference to cvNamedWindow'
rei2-1.cpp: (.text+0x4c): Undefined reference to cvShowImage'
rei2-1.cpp: (.text+0x58): Undefined reference to cvReleaseImage'
rei2-1.cpp: (.text+0x60): Undefined reference to cvDestroyWindow'  
collect2:error:ld returned1 exit status compilation failure – 

Add

·Open Geany on the desktop version of Raspberry Pi and press the compilation button there.
·OpenCV version is 3.4.1.

c++ opencv raspberry-pi

2022-09-30 21:33

1 Answers

When trying to call OpenCV functions, it appears that "Definition not found" is an error, but the Include is written differently depending on the OpenCV version.
If you use 2.x or later, use the include section.

#include<opencv/cv.h> 
# include <opencv/cxcore.h>
# include <opencv/highgui.h>

You should also include the opencv/* subdirectory name as shown in .

Note:
http://hikopatchi.hatenablog.com/entry/2014/03/31/184937
https://github.com/opencv/opencv/tree/3.4/include/opencv


2022-09-30 21:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.