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
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
How to Determine the Precise Set of Environment Variables a Systemd Environmentfile Would Set
Nasm Print One Character at a Time
Perf-Report Show Value of CPU Register
Access Denied to Android.Git.Kernel.Org
Convert Multiple Sftp Command Lines to a Single Line Command Line
Awk: Words Frequency from One Text File, How to Ouput into Myfile.Txt
Start X86_64 Code on X86 (32Bit) Linux, Running on X86_64 Cpu
Synchronizing Four Shell Scripts to Run One After Another in Unix
This Script Wont Sort Correctly by Age
How to Mount Google Bucket as Local Disk on Linux Instance with Full Access Rights
Collecting the Data for a Partiulcar Process from Pmu for Every 1 Milli Second
Redirect Two or More Stdout to a Single Stdin
Getting Github Files (And Updates) Onto an Ubuntu Web Server
How to Save the Execution Log When We Run a Command Using Putty/Plink
Qmake .Pro File Not Parsed Correctly to Generate Ld_Library_Path
Suppress or Prevent Duplicate Inotifywait Events