49

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 ?

Jorge Vega Sánchez
  • 7,430
  • 14
  • 55
  • 77

2 Answers2

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.

Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • 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`? – Tomasz Gandor Jan 14 '15 at 11:15
  • @Andrey Kamaev where is this flag located? what is the name of the file? – Gilad Apr 26 '15 at 17:32
  • 1
    Got it, it is inside the CMAKE(GUI) – Gilad Apr 26 '15 at 18:13
  • 2
    Even with setting the flag BUILD_SHARED_LIBS to OFF, I still get libgstreamer-0.10.so.0 linked. Any idea why? – Romanzo Criminale Dec 29 '15 at 06:04
  • 2
    @TomaszGandor `ccmake .` == `./configure --help` – wener Mar 07 '17 at 02:22
  • does `-DBUILD_SHARED_LIBS=OFF` creates the dynamic files too? – roschach Sep 18 '18 at 09:35
  • @FrancescoBoi if you print the options you get this `// Build shared libraries (.dll/.so) instead of static ones (.lib/.a) BUILD_SHARED_LIBS:BOOL=ON` – Picard Nov 11 '19 at 12:50
  • @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. – Thomas Jan 09 '20 at 09:16
  • Reference for BUILD_SHARED_LIBS in 4.x docs: https://docs.opencv.org/4.x/db/d05/tutorial_config_reference.html#tutorial_config_reference_general_static – cod3monk3y Sep 21 '22 at 23:39
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

oarfish
  • 4,116
  • 4
  • 37
  • 66