Ask Your Question
1

How to generate/install OpenCV for static linking?

asked 2019-03-18 00:31:09 -0500

TheBlackViper_Alpha gravatar image

updated 2019-03-28 22:48:51 -0500

Hi, I am trying to build a program that can used on other machines without opencv. I have a machine installed using the usual process of building from source. From what I read I need to turn the BUILD_SHARED_LIBS = OFF option. After this I do not know how to proceed. I can see that some files are in the usr/local/include and usr/local/libs. Is this the right way to generate static libs? Also how do I compile the static libs for my program? Thank you.

EDIT I tried the following to compile.

g++ main.cpp -I /usr/local/include/opencv4 -L/usr/local/lib -lopencv_dnn -lopencv_features2d -lopencv_calib3d -lopencv_flann -lopencv_gapi -lopencv_objdetect -lopencv_ml -lopencv_photo -lopencv_video -lopencv_videoio -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lopencv_stitching  -L/lib/x86_64-linux-gnu -libz -lpng -ljpeg -o binary

and

g++ -I/usr/local/include/opencv4 -I/usr/local/include/opencv4 -L/usr/local/lib/ -g -o binary  main.cpp -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching

Only to get a bunch of undefined reference errors.

edit retag flag offensive close merge delete

Comments

there is no opencv_legacy in opencv3/4

berak gravatar imageberak ( 2019-03-31 04:51:13 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-03-18 04:05:18 -0500

berak gravatar image

updated 2019-03-18 06:32:22 -0500

I need to turn the BUILD_SHARED_LIBS = OFF option.

yes. then (re)run cmake && make install

some files are in the usr/local/include and usr/local/libs

check your opencv version again, the relevant include folder for opencv4 is /usr/local/include/opencv4 (this was changed recently !)

how do I compile the static libs for my program?

above steps will build static (large) opencv libs. you probably wanted to ask:

how do i link my program against static opencv libs ?

that's the tricky part:

  • you'll have to manually link libs, that were conveniently linked against the so's before (like libjpeg -> imgcodecs)
  • the order of libs in the list matters, you have to sort them, dependancies go last, so, -lopencv_imgcodecs -llibpng -lz
edit flag offensive delete link more

Comments

Thanks for answering, however do I only need to properly link in order the libs found in /usr/local/libs ? or are there any third party libs that I need to know and link. Is there a list where the order of dependencies can be found easily?

TheBlackViper_Alpha gravatar imageTheBlackViper_Alpha ( 2019-03-28 22:42:10 -0500 )edit

here's a crude, but working example:

g++ -std=c++0x src/cv.cpp -I /usr/local/include/opencv4 -L /usr/local/lib -L /usr/local/lib/opencv4/3rdparty -O3 -lopencv_aruco -lopencv_imgcodecs -lopencv_calib3d -lopencv_ccalib -lopencv_xfeatures2d -lopencv_features2d -lopencv_xobjdetect -lopencv_dnn -lopencv_flann -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_reg -lopencv_xphoto -lopencv_stitching -lopencv_superres -lopencv_bgsegm -lopencv_face -lopencv_saliency -lopencv_stitching -lopencv_superres -lopencv_tracking -lopencv_video -lopencv_ximgproc -lopencv_shape -lopencv_text -lopencv_optflow -lopencv_bioinspired -lopencv_imgproc -lopencv_core -littnotify -ljpeg -llibwebp -lrt -ldl -lz -lpthread -o src/cv
berak gravatar imageberak ( 2019-03-31 04:52:51 -0500 )edit

Is there a list where the order of dependencies can be found easily?

no, trial & error here ;(

berak gravatar imageberak ( 2019-03-31 04:54:42 -0500 )edit
0

answered 2019-04-01 00:00:23 -0500

TheBlackViper_Alpha gravatar image

Ok after searching everywhere. I finally did it. I am using docker containers as my working environment. I am using OpenCV 4.0.1 and Ubuntu 18.04. My docker file can be found at DockerFile. (The docker image is still built dynamically just follow the steps below to enable static build.)

There are basically two things to do to static compile successfully.

  1. -D BUILD_SHARED_LIBS=OFF

    AND

  2. -D OPENCV_GENERATE_PKGCONFIG=YES

The second option will save you the hassle of manually linking all libraries during compilation. It is very critical. And to compile I just used the typical:

g++ main.cpp -o test `pkg-config --cflags --libs --static opencv4`

Just remember to set the $PKG_CONFIG_PATH to where the opencv4.pc is located.I set mine via:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

You can check it by: echo $PKG_CONFIG_PATH I hope this helps anyone struggling to do this!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-18 00:31:09 -0500

Seen: 100 times

Last updated: Apr 01