简体   繁体   中英

OpenCV 2.4.8 compiling and linking error

I am a beginner with OpenCV, and I had to compile from source since I am using it on an account on an HPC machine. I compiled it locally so that it exists in my home directory under ~/ext . Now I am trying to compile the simple example from this documentation page , but I am having trouble compiling and linking my new, local openCV installation.

Here is the code in my test.cpp file:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
  Mat image;
  image = imread( argv[1], 1 );

  if( argc != 2 || !image.data )
    {
      printf( "No image data \n" );
      return -1;
    }

  namedWindow( "Display Image", WINDOW_AUTOSIZE );
  imshow( "Display Image", image );

  waitKey(0);

  return 0;
}

And here is my simple Makefile:

CC = g++
SRC = test.cpp
EXEC = test

CFLAGS = -I/home/my_username/ext/include/
LFLAGS = -L/home/my_username/ext/lib/ -lcxcore -lcv -lhighgui -lcvaux -lml

# YOU PROBABLY DO NOT HAVE TO CHANGE ANYTHING BELOW THIS LINE.

# This generates a list of object file names from the source file names
OBJ = $(addsuffix .o, $(basename $(SRC)))

# "make" makes the executable.
$(EXEC): $(OBJ)
    $(CC) $(LFLAGS) $(OBJ) -o $(EXEC)

# This says how to build an object (.o) file from a source (.c) file
%.o : %.cpp
    $(CC) $(CFLAGS) -c $< -o $@

# "make clean" deletes objects and executable
clean:
    rm -f $(EXEC) *.o 

With this configuration I receive the following error when I try to make .

g++ -I/home/kjorg50/ext/include/ -c test.cpp -o test.o
g++ -L/home/kjorg50/ext/lib/ -lcxcore -lcv -lhighgui -lcvaux -lml test.o -o test
test.o: In function `main':
test.cpp:(.text+0x1c7): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
test.cpp:(.text+0x1fb): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
test.o: In function `cv::Mat::operator=(cv::Mat const&)':
test.cpp:(.text._ZN2cv3MataSERKS0_[cv::Mat::operator=(cv::Mat const&)]+0x111): undefined reference to `cv::Mat::copySize(cv::Mat const&)'
test.o: In function `cv::Mat::release()':
test.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
make: *** [test] Error 1

I am pretty sure I am either missing some #include statements, or my CFLAGS and/or LFLAGS values are incorrect. So, would anyone know what library files I am missing in the compilation?

EDIT - To get it to compile I had to add the correct paths to my LIBRARY_PATH and LD_LIBRARY_PATH env variables

Here's a pretty decent reference on makefiles .

After your comment, I used the above reference to create a Makefile that compiles your code on my system (though it throws a logic error when run). I had originally thought that the ordering of your libraries might be wrong. That's true because of the required ordering with the gnu C++ compiler, your library references need to go last. But, it also looks like at least with the up to date Ubuntu opencv libraries, you do not have the right library names either.

You have:

# "make" makes the executable.
$(EXEC): $(OBJ)
    $(CC) $(LFLAGS) $(OBJ) -o $(EXEC)

Change this to:

$(EXEC): $(OBJ) 
    $(CC) -o $(EXEC) $(OBJ) $(LFLAGS)

with LFLAGS being:

LFLAGS = -L/home/my_username/ext/lib/ -lopencv_core -lopencv_highgui 

Give that a shot and let me know how it goes. If that does not work, then you are not referencing the correct location of your libraries with -L command. When I install them on my system using aptitude, they are in the /usr/lib folder.

To get it to compile, I had to add /home/my_username/ext/lib/ to the LIBRARY_PATH and LD_LIBRARY_PATH environment variables. I did this by adding it to my ~/.bashrc file.

I now have other runtime errors related to opencv, but those are not related to the compilation issue.

I use g++ "pkg-config opencv --libs" main.cpp -o main to compile my OpenCV codes.

This is the output of the pkg-config opencv --libs command:

-lopencv_calib3d
-lopencv_contrib
-lopencv_core
-lopencv_features2d
-lopencv_flann
-lopencv_gpu
-lopencv_highgui
-lopencv_imgproc
-lopencv_legacy
-lopencv_ml
-lopencv_objdetect
-lopencv_ocl
-lopencv_photo
-lopencv_stitching
-lopencv_superres
-lopencv_ts
-lopencv_video
-lopencv_videostab

I guess you need to correct the library names in order to get the code to compile.

Also I am pretty sure that you are not missing out any includes as you have used #include <opencv2/opencv.hpp> which includes all the opencv headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM