Ask Your Question

CMake option to prefer static libs

asked 2018-10-12 13:43:26 -0600

GruenSein gravatar image

updated 2018-10-13 05:58:51 -0600

berak gravatar image

Hello all,

I am using OpenCV 3.4.3 on macOS 10.14 for one of my projects. While I have installed and compiled OpenCV myself, I'd like to build an executable with statically linked OpenCV libs so my colleagues don't all have to go through this procedure themselves. Since I have both the shared .dylibs and static .a file, I was wondering, if there is some CMake option to pass, so that find_package will prefer the static libraries. For boost, there is such an option "set(Boost_USE_STATIC_LIBS ON)". I have found some mentions of OpenCV_STATIC/SHARED online but none seem to work. Otherwise, I'd have to manually specify the path to the files, which I find a lot less elegant and less portable.

Any ideas are appreciated. Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-13 01:00:23 -0600

berak gravatar image

updated 2018-10-13 02:51:23 -0600

there is a cmake option, when building the opencv libs:


and another option, when building your own program using those:


(this should go into your local CMakeLists.txt

((note: please really try with cmake for your own prog first. e.g. pkg-config won't be able to order the libs by dependancy and will miss any system libs, that now need to be linked (again, order-dependant)))

good luck !

edit flag offensive delete link more


Thank you for your reply. Unfortunately, this is not working for me. I have the shared as well as the static libraries in my lib-directory. Setting OpenCV_STATIC to ON does not help. A quick check with tool -L reveals that the created binary still requires multiple OpenCV-dylib.

GruenSein gravatar imageGruenSein ( 2018-10-13 05:12:42 -0600 )edit

unfortunately now, unclear, what you did, exactly.

also, even if you use dynamic linking, there still will be .a "stub" libs. (very small ones). maybe you confuse those whith the "real" static ones ?

berak gravatar imageberak ( 2018-10-13 05:36:39 -0600 )edit

So, I installed OpenCV using brew with the --build-from-source flag. This actually builds dynamic and static libs. It explicitly states cmake .. -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS_RELEASE=-DNDEBUG. After this was done, I ran my own CMake build script, which uses

find_package(OpenCV Required)

The code builds and links without any problems but when I check the binary's dependencies with

otool -L binary

the libs I use are still listed as libopencv....dylib.

GruenSein gravatar imageGruenSein ( 2018-10-13 05:48:01 -0600 )edit

sorry, i can't help at all with apple specific problems. (and to my knowledge, there is no support for brew from opencv at all)

berak gravatar imageberak ( 2018-10-13 05:57:01 -0600 )edit

Thanks for your time, even though I think, this is unrelated to macOS. CMake is able to locate the libraries properly and the static versions are there as well. For some reason, the find_package simply prefers the dylibs, even though the OpenCV_STATIC-flag is set.

GruenSein gravatar imageGruenSein ( 2018-10-13 07:31:38 -0600 )edit

Question Tools

1 follower


Asked: 2018-10-12 13:43:26 -0600

Seen: 5,244 times

Last updated: Oct 13 '18