OpenCV as a static library (cmake options)
Asked Answered
D

2

50

I want to use OpenCV library in an embedded system and I need to compile my project using OpenCV as a static library.

How can I create the library using cmake options ?

Drat answered 28/9, 2011 at 12:26 Comment(0)
T
77

To build OpenCV as static library you need to set BUILD_SHARED_LIBS flag to false/off:

cmake -DBUILD_SHARED_LIBS=OFF ..

But I think it is not enough for your task because you actually need to cross-compile library for you architecture. In case of Android or IOS such port already exists and you can simply use it. In case of another platform you need to create your own cmake toolchain file for cross-compiling and probably make a number of fixes in OpenCV build system.

Tautologism answered 28/9, 2011 at 15:0 Comment(9)
Can cross-compiling for Windows (using mingw32) be done away with some magical -DCMAKE_CXX_COMPILER=i586-mingw32msvc-g++? I see CMake's advantages over autotools, but what's CMake's equivalent of ./configure --help?Deirdre
@Andrey Kamaev where is this flag located? what is the name of the file?Arbor
Got it, it is inside the CMAKE(GUI)Arbor
Even with setting the flag BUILD_SHARED_LIBS to OFF, I still get libgstreamer-0.10.so.0 linked. Any idea why?Minstrel
@TomaszGandor ccmake . == ./configure --helpLooby
does -DBUILD_SHARED_LIBS=OFF creates the dynamic files too?Spicer
@FrancescoBoi if you print the options you get this // Build shared libraries (.dll/.so) instead of static ones (.lib/.a) BUILD_SHARED_LIBS:BOOL=ONCompose
@RomanzoCriminale BUILD_SHARED_LIBS only makes OpenCV's own libs into static ones, but does nothing about its (transitive) dependencies like GStreamer. You'd need to build static libraries of all dependencies and somehow ensure that the OpenCV build system uses them... it might not be trivial.Indocile
Reference for BUILD_SHARED_LIBS in 4.x docs: docs.opencv.org/4.x/db/d05/…Kamenskuralski
D
0

The BUILD_SHARED_LIBS=OFF cmake option will create static libraries.

It should be noted that at the time of writing this, OpenCV does not really support static build, in that the result will not be useable when installed somewhere.

https://github.com/opencv/opencv/issues/21447#issuecomment-1013088996

Doughty answered 16/1, 2022 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.