Recently I installed opencv on my ubuntu 16.04 machine(I've done this many times before and used it ok). I tried compiling yolo program, it compiled ok and ran ok. When I set OPENCV=1 in the Makefile, it compiles the program with opencv. To set the compile and link flags it has these two lines.
LDFLAGS+= `pkg-config --libs opencv` -lstdc++
COMMON+= `pkg-config --cflags opencv`
My pkg-config commands work correctly. See the output below.
ckim@chan-ubuntu:~/YOLOV2/darknet$ pkg-config --cflags opencv
-I/home/ckim/opencv_install/installation/OpenCV-3.4/include/opencv -I/home/ckim/opencv_install/installation/OpenCV-3.4/include
ckim@chan-ubuntu:~/YOLOV2/darknet$ pkg-config --libs opencv
-L/home/ckim/opencv_install/installation/OpenCV-3.4/lib -lopencv_stitching -lopencv_videostab -lopencv_superres -lopencv_surface_matching -lopencv_dnn_objdetect -lopencv_line_descriptor -lopencv_aruco -lopencv_img_hash -lopencv_xobjdetect -lopencv_dpm -lopencv_freetype -lopencv_stereo -lopencv_face -lopencv_objdetect -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_saliency -lopencv_hfs -lopencv_ccalib -lopencv_tracking -lopencv_datasets -lopencv_plot -lopencv_optflow -lopencv_ximgproc -lopencv_fuzzy -lopencv_bioinspired -lopencv_highgui -lopencv_videoio -lopencv_text -lopencv_dnn -lopencv_reg -lopencv_hdf -lopencv_bgsegm -lopencv_rgbd -lopencv_sfm -lopencv_xfeatures2d -lopencv_calib3d -lopencv_shape -lopencv_imgcodecs -lopencv_features2d -lopencv_video -lopencv_ml -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
A couple of days ago, I tried compiling a simple opencv pgrogram that I find on a web page and tried compiling it. (Iwanted to test a basic display window's behavior). The program was like this. very typical)
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(1); // Wait for a keystroke in the window
return 0;
}
When I compile and link it with command
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
it gives me no error and I see the file test
but it's not executable. When I run it after setting chmod +x test
, I get
ckim@chan-ubuntu:~/opencvtest$ ./test
bash: ./test: cannot execute binary file: Exec format error
ckim@chan-ubuntu:~/opencvtest$ file test
test: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
What could be wrong?
As per the comment you're command line...
gcc -c test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test
includes the -c
flag which tells the compiler to compile only -- don't link. Also. as you're code is c++
rather than c
you need to link with the correct runtime libraries. That means using g++
rather than gcc
. So the command should be...
g++ test.cpp `pkg-config --cflags opencv` `pkg-config --libs opencv` -o test