Home > Net >  Raspberry Camera compile error: undefined reference to symbol
Raspberry Camera compile error: undefined reference to symbol

Time:12-26

I am trying to compile a program for the raspberry pi. But when I run the build in Geany I got this error:

g   $(pkg-config opencv4 --cflags --libs) -o g   $(pkg-config raspicam --cflags --libs) -o camera_2 camera_2.cpp (in directory: /home/pi/Desktop)
/usr/bin/ld: /tmp/ccTDUfOT.o: undefined reference to symbol '_ZN2cv6imshowERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayE'
/usr/bin/ld: //usr/local/lib/libopencv_highgui.so.405: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Compilation failed.

The camera.cpp file looks like this:

#include <opencv2/opencv.hpp>
#include <raspicam_cv.h>
#include <iostream>

using namespace std;
using namespace cv;
using namespace raspicam;

Mat frame;

void Setup ( int argc,char **argv, RaspiCam_Cv &Camera )
  {
    Camera.set ( CAP_PROP_FRAME_WIDTH,  ( "-w",argc,argv,400 ) );
    Camera.set ( CAP_PROP_FRAME_HEIGHT,  ( "-h",argc,argv,240 ) );
    Camera.set ( CAP_PROP_BRIGHTNESS, ( "-br",argc,argv,50 ) );
    Camera.set ( CAP_PROP_CONTRAST ,( "-co",argc,argv,50 ) );
    Camera.set ( CAP_PROP_SATURATION,  ( "-sa",argc,argv,50 ) );
    Camera.set ( CAP_PROP_GAIN,  ( "-g",argc,argv ,50 ) );
    Camera.set ( CAP_PROP_FPS,  ( "-fps",argc,argv,100));

  }


int main(int argc,char **argv)
{
    RaspiCam_Cv Camera;
    Setup(argc, argv, Camera);
    cout<<"Connecting to camera"<<endl;
    if (!Camera.open())
    {       
        cout<<"Failed to Connect"<<endl;
        return -1;
    }
    cout<<"Camera Id = "<<Camera.getId()<<endl;
     
    Camera.grab();
    Camera.retrieve(frame);

    imshow("frame", frame);
    waitKey();
    return 0;  
}

So far I have figured that when I remove Mat frame; the error does not appear.

The pkg-config file looks like this:


prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv2
includedir_new=${prefix}/include/opencv4


Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.5.5
L: -Libs${exec_prefix}/lib -lopencv_calib3d -lopencv_core -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_gapi -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_videoio
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}

The command in Geany looks like this:

g   $(pkg-config opencv4 --cflags --libs) -o g   $(pkg-config raspicam --cflags --libs) -o %e %f

Do you have any idea what is wrong and what I do have to change? Thank you

CodePudding user response:

The argument -o g is very weird because it will make an output file named g , which is confusing because that's also the name of your compiler. You probably want to remove that since you already have a -o argument.

Secondly, the order of the different objects/libraries that are getting linked together often matters when you are linking a program. Try putting the calls to pkg-config at the end of the command.

CodePudding user response:

I have solved the error. I have added two libraries to the command:

g   $(pkg-config opencv4 --cflags --libs) $(pkg-config raspicam --cflags --libs) -o %e %f -lopencv_highgui -lopencv_core

I wonder why the libraries do not work in the config file.

  • Related