title: How to compile static OpenCV4 libraries?
body: <p>For testing purposes, I need to compile my C++ opencv program using static libs. My cmake build options are: </p>
  
  <pre><code>    -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D BUILD_DOCS=OFF \
      -D BUILD_EXAMPLES=OFF \
      -D BUILD_opencv_apps=OFF \
      -D BUILD_opencv_python2=ON \
      -D BUILD_opencv_python3=ON \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_SHARED_LIBS=OFF \ 
      -D BUILD_TESTS=OFF \
      -D ENABLE_PRECOMPILED_HEADERS=OFF\
      -D FORCE_VTK=OFF \
      -D WITH_FFMPEG=OFF \
      -D WITH_GDAL=OFF \ 
      -D WITH_IPP=OFF \
      -D WITH_OPENEXR=OFF \
      -D WITH_OPENGL=OFF \ 
      -D WITH_QT=OFF\
      -D WITH_TBB=OFF \ 
      -D WITH_XINE=OFF \ 
      -D BUILD_JPEG=ON  \
      -D BUILD_TIFF=ON \
      -D BUILD_PNG=ON \
  </code></pre>
  
  <p>I can see that I have the files in both <code>/usr/local/libs/ and the headers in /usr/local/include/</code> .And I have test program to display an image which uses <code>highgui, core and imgcodec libs</code> Reading similar questions/threads I tried compiling my program using the following where the build order is critical:</p>
  
  <pre><code>    g++ main.cpp -I /usr/local/include/opencv4 -L /usr/local/lib 
      -L /usr/local/share/opencv4 -L /usr/local/lib/opencv4 -lopencv_highgui \
      -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_highgui \
      -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching \
      -lopencv_video -lopencv_imgproc -lopencv_core -ljpeg -lpng -ltiff -lrt \
      -lz -lpthread -o binary
  </code></pre>
  
  <p>However it just spits out numerous errors like:</p>
  
  <pre><code>    usr/local/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvOnKeyPress(_GtkWidget*, _GdkEventKey*, void*)':window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0x98): undefined reference to `g_mutex_lock'
      window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0xaa): undefined reference to `g_cond_broadcast'
      window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0xb6): undefined reference to `g_mutex_unlock'/usr/local/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidgetSetImage(_CvImageWidget*, void const*)':
      window_gtk.cpp:(.text._Z21cvImageWidgetSetImageP14_CvImageWidgetPKv+0x185): undefined reference to `gtk_widget_get_type' /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): In function `OPENCL_FN_clGetExtensionFunctionAddress_switch_fn(char const*)':
  opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x33): undefined reference to `dlsym'
  opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x19c): undefined reference to `dlopen'
  opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x1b3): undefined reference to `dlsym'
  opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x296): undefined reference to `dlclose'
  </code></pre>
  
  <p>Apparently using <code>pkgconfig</code> has been dropped for <a href="https://github.com/opencv/opencv/issues/13154" rel="nofollow noreferrer">OpenCV4.</a>
  Is there anyway to find the libraries required and the proper order to build using static libraries?</p>
  
  <p><strong>UPDATE: Now I only have a few errors after fixing the script</strong>
  Using the following:</p>
  
  <pre><code>g++ main.cpp -I /usr/local/include/opencv4 -L/usr/local/lib -lopencv_dnn -lopencv_features2d -lopencv_calib3d -lopencv_flann -lopencv_objdetect -lopencv_ml -lopencv_photo -lopencv_video -lopencv_videoio -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lpng -ljpeg -o binary
  </code></pre>
  
  <p>I only now have:</p>
  
  <pre><code>/usr/bin/ld: /usr/local/lib/libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzeof'
  //lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line
  collect2: error: ld returned 1 exit status
  </code></pre>