0

I am compiling an example for OpenCV with the following code:

#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(0);                                          // Wait for a keystroke in the window
    return 0;
}

The compilation code is:

g++ -I/usr/local/include/opencv2 `pkg-config --cflags --libs opencv` -L /usr/local/share/OpenCV/3rdparty/lib/ opencv.cpp -o opencv -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_features2d -lopencv_imgcodecs

If I omit one of the libraries I add additionally, I get linking errors. When trying to run the program, I get an"Invalid machine code" error. How can that be solved?

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • You are linking to `-L /usr/local/share/OpenCV/3rdparty/lib/`, I think you should be linking to `-L/usr/local/lib/`. the 3rdparty libs is for some image codecs.... also i usually don't use `-lopencv_imgcodecs` and in this case you are not using `-lopencv_features2d` either – api55 Apr 08 '16 at 09:14
  • If I omit `-lopencv_imgcodecs`, I get `/usr/bin/ld: /tmp/cc2raORo.o: undefined reference to symbol '_ZN2cv6imreadERKNS_6StringEi' /usr/local/lib/libopencv_imgcodecs.so: error adding symbols: DSO missing from command line`, if I omit the other linking path, I get `/usr/bin/ld: cannot find -lippicv`. – arc_lupus Apr 08 '16 at 10:52
  • Sorry, I am used to opencv 2.4... in 3.x they move imread to imgcodecs. Dont forget to include `#include "opencv2/imgcodecs.hpp"` also, `-I/usr/local/include/opencv2` this should be without opencv2. (you already have that part in every include). For the, `-lippicv` you have to either use that linking path (dont forget to link the other one too) or compile opencv with that option (see this [link](http://stackoverflow.com/questions/34401117/compiling-code-with-opencv-usr-bin-ld-cannot-find-lippicv)) – api55 Apr 08 '16 at 12:10

0 Answers0