Ask Your Question

compile static openCV library

asked 2017-01-13 20:25:54 -0500

sknelson111 gravatar image

updated 2017-01-14 14:29:38 -0500


I'd like to compile my program with openCV as a static library, to create a standalone app. I've tried using the cmake flag BUILD_SHARED_LIBS=OFF, however when I try to run my app on an ubuntu machine without openCV installed, I get the following error:

./gray_static_v03.out: error while loading shared libraries: cannot open shared object file: No such file or directory

Here is a link to the dockerfile - where you can see my install commands:

and here is the command I use to compile:

g++ -o gray.out gray.cpp `pkg-config opencv --cflags --libs --static`

Any ideas what I am doing wrong? Any help would be greatly appreciated!

Thank you!

edit retag flag offensive close merge delete


also, please use opencv3.2, not 3.0

berak gravatar imageberak ( 2017-01-14 04:26:52 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-01-14 04:31:08 -0500

berak gravatar image

updated 2017-01-14 12:50:37 -0500

i'm not sure, if you really want to go without any png support, but for statically linking, without any assumptions about libraries available on the final host machine you either need:

-DBUILD_PNG=OFF -DWITH_PNG=OFF   //disable png entirely


-DBUILD_PNG=ON -DWITH_PNG=ON     //link in libpng statically from 3rdparty folder

same pattern also applies to JPG, and TIFF !

edit flag offensive delete link more



Thanks for your reply Berak. I think I see now that I need to build all the dependencies statically as well, that was probably my problem. So I will give it a shot - and if I have more questions I will let you know. Thank you!

sknelson111 gravatar imagesknelson111 ( 2017-01-14 15:22:54 -0500 )edit

oh, I'm sorry, I see what you were saying. Yeah, added -DBUILD_PNG=ON -DWITH_PNG=ON and everything is working now. Thank you so much!

sknelson111 gravatar imagesknelson111 ( 2017-01-14 17:17:56 -0500 )edit

Question Tools

1 follower


Asked: 2017-01-13 20:25:54 -0500

Seen: 1,709 times

Last updated: Jan 14 '17