arm712
arm712

Reputation: 117

Statically Link OpenCV

I would like to compile a C++ program that uses openCV statically, so that I can use it on other computers without having to install openCV.

I compiled openCV 2.4.5 with following flags in order to get static libraries and to install it besides my main installation which is version 3.1.

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/opt/openCV_2_4_5 -D WITH_FFMPEG=OFF -DBUILD_SHARED_LIBS=NO ..

ls /opt/openCV_2_4_5/lib
libopencv_calib3d.a     libopencv_flann.a    libopencv_legacy.a     libopencv_photo.a      libopencv_video.a
libopencv_contrib.a     libopencv_gpu.a      libopencv_ml.a         libopencv_stitching.a  libopencv_videostab.a
libopencv_core.a        libopencv_highgui.a  libopencv_nonfree.a    libopencv_superres.a   pkgconfig
libopencv_features2d.a  libopencv_imgproc.a  libopencv_objdetect.a  libopencv_ts.a         python2.7

After installing, I tried to compile a c++ program for face detection like this:

g++ -std=c++11 -L/opt/openCV_2_4_5/lib -I/opt/openCV_2_4_5/include -o ex2 ex2.cpp -L. -lpthread -lz -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn -lopencv_dpm -lopencv_fuzzy -lopencv_line_descriptor -lopencv_optflow -lopencv_plot -lopencv_reg -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_rgbd -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_face -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ximgproc -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_xobjdetect -lopencv_objdetect -lopencv_ml -lopencv_xphoto -lopencv_videoio -lopencv_imgcodecs -lopencv_photo -lopencv_imgproc -lopencv_core

From that I got the error:

/tmp/cct4prBn.o: In function `main':
ex2.cpp:(.text+0x141): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x2bc): undefined reference to `cv::imread(std::string const&, int)'
ex2.cpp:(.text+0x4f3): undefined reference to `cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
collect2: error: ld returned 1 exit status
make_2_4_5:9: recipe for target 'ex2' failed
make: *** [ex2] Error 1

I used the same library flags as my installation of OpenCV 3.1 has. I got them from:

pkg-config --cflags --libs opencv

The -L and -I flags point to the openCV 2.4.5 installation in the /opt directory.

Why doesn't it compile? All OpenCV libraries are listed in the section where the static libraries should be.

Upvotes: 1

Views: 3231

Answers (1)

Chungzuwalla
Chungzuwalla

Reputation: 1068

In OpenCV 2 I believe cv::imread() and cv::imwrite() are in the highgui library. In OpenCV 3 they were moved to the imgcodecs library. Try adding:

-lopencv_highgui

to your command line.

Upvotes: 1

Related Questions