Building Opencv as Static Libraries

OpenCV as a static library (cmake options)

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.

How to build the whole openCV library into a static library?

Building OpenCV static libs

I think that is possible to build the whole OpenCV libs as a big static lib. But it is highly recommended to build the single and separated static libs for a fined granulated compilation. There are also parts of OpenCV project that compiling with Emscripten are very hard to build. And what about if you want use only a set of OpenCV functions? The final .js or .wasm file will be too big.

Example opencv-em

We did this script opencv-em to build a set of static libs that we need for a project. It build the static libs and pack the needed include directories.

Step build description

We have set up a shell build script. We used cmake for the compilation because the existent CMakeLists.txt in the OpenCV project directory, this simplify things a lot:

 cmake .. -GNinja -DCMAKE_TOOLCHAIN_FILE=$EM_TOOLCHAIN $OPENCV_CONF $OPENCV_INTRINSICS -DCMAKE_CXX_FLAGS="$EM_FLAGS" -DCMAKE_C_FLAGS="$EM_FLAGS"

we used Ninja as a compiler but you can use make.

-DCMAKE_TOOLCHAIN_FILE=$EM_TOOLCHAIN

This define the toolchain in this case Emscripten:

EM_TOOLCHAIN="$EMSCRIPTEN/cmake/Modules/Platform/Emscripten.cmake"

You need of course to install Emscripten with emsdk.

OPENCV_CONF is the variable where you define which libs to build and the necessary files to include and to exclude (that is very important!)
Note also that compilation may vary in base of Emscripten and OpenCV version!

Downloads

If you want you can downloads the libs in the releases page. Read the Emscripten and OpenCV version used.

using static libraries instead of dynamic libraries in opencv

I am able to get the static libraries working in VS 2013 by changing the project's Runtime Library to /MTd

Sample Image

and then including these Linker >> Input >> Additional Dependencies:

opencv_core248d.lib
opencv_imgproc248d.lib
opencv_highgui248d.lib
opencv_ml248d.lib
opencv_video248d.lib
opencv_features2d248d.lib
opencv_calib3d248d.lib
opencv_objdetect248d.lib
opencv_contrib248d.lib
opencv_legacy248d.lib
opencv_flann248d.lib
libpngd.lib
libtiffd.lib
zlibd.lib
IlmImfd.lib
libjasperd.lib
libjpegd.lib
comctl32.lib
gdi32.lib
vfw32.lib

Building an OpenCV Application with Static Libraries

Problem solved after a few steps. You can take a look at my post on the OpenCV Q&A page.

http://answers.opencv.org/question/152366/build-application-with-static-libraries/

Overall, it was "remember to clear your CMake cache file between each invocation of cmake" and to move/copy the opencv_ffmpegXXX.dll (310 in my case), put it in the same directory with your EXE.

Building OpenCV as a static library leads to thousands of undefined references

I fixed it

My build command was missing the --static flag.

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

Now it can build my test application with OpenCV3.2.0 with no problems but I get this error when i use OpenCV4.1.0.

/usr/bin/ld: cannot find -lgflags_shared

I dont think I need this lib so i fixed this by removing -lgflags_shared from the opencv.pc file. Doing it manually works but using the command line is more convenient.

sed -i 's/-lgflags_shared //g' unix-install/opencv4.pc

Building OpenCV as static libraries

Looking at OpenCV's CMakeLists.txt, it appears as if you're using the wrong names for the OpenCV CMake options.

BUILD_SHARED_LIBRARIES should be BUILD_SHARED_LIBS and BUILD_PYTHON_SUPPORT should be BUILD_opencv_python



Related Topics



Leave a reply



Submit