title: How to compile a c++ application using static opencv libraries within docker
body: <p>I'm building my first OpenCV based application in C++. My goal is to build an intermediate docker image that can compile the application statically so that it can run standalone in the resulting smaller image. I'm open to using any docker image for this step, but just so that you can see exactly what I have, here's the dockerfile to reproduce the entire environment:</p>
  
  <pre><code>FROM ubuntu:18.04 as compiler
  
  ENV OPENCV_VERSION='3.4.2' DEBIAN_FRONTEND=noninteractive
  
  RUN apt-get -y update &amp;&amp; \
      apt-get -y upgrade &amp;&amp; \
      apt-get -y dist-upgrade &amp;&amp; \
      apt-get -y autoremove &amp;&amp; \
      apt-get install -y build-essential cmake 
  RUN apt-get install -y qt5-default libvtk6-dev
  RUN apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libopenexr-dev libgdal-dev
  RUN apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev
  RUN apt-get install -y unzip wget
  RUN wget --progress=dot:giga https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip &amp;&amp; \
      unzip -q ${OPENCV_VERSION}.zip &amp;&amp; \
      rm ${OPENCV_VERSION}.zip &amp;&amp; \
      mv opencv-${OPENCV_VERSION} OpenCV &amp;&amp; \
      cd OpenCV &amp;&amp; \
      mkdir build &amp;&amp; \
      cd build &amp;&amp; \
      cmake  \
          -D BUILD_SHARED_LIBS=OFF \ 
          -D WITH_QT=ON \
          -D WITH_OPENGL=ON \ 
          -D FORCE_VTK=ON \
          -D WITH_TBB=ON \ 
          -D WITH_GDAL=ON \ 
          -D WITH_XINE=ON \ 
          -D BUILD_EXAMPLES=OFF \
          -D ENABLE_PRECOMPILED_HEADERS=OFF \
          -D BUILD_DOCS=OFF \
          -D BUILD_PERF_TESTS=OFF \
          -D BUILD_TESTS=OFF \
          -D BUILD_opencv_apps=OFF \
          .. &amp;&amp; \
      make -j4 &amp;&amp; \
      make install &amp;&amp; \
      ldconfig
  
  COPY compile-test.cpp compile-test.cpp
  
  RUN g++ -std=c++11 -static compile-test.cpp -o /app $(pkg-config --cflags --libs opencv)
  </code></pre>
  
  <p>I can currently compile my c++ apps without issue using the dyanmic libs, but this creates a massive docker image, and I really want to be able to build standalone binaries for distribution, with minimal size.</p>
  
  <p>As you can see I'm compiling OpenCV from source including the flag <code>BUILD_SHARED_LIBS=OFF</code> to make sure I get the <code>.a</code> static libs, rather than the <code>.so</code> dynamic libs. I took a hint from a <a href="https://github.com/milq/milq/blob/master/scripts/bash/install-opencv.sh" rel="noreferrer">highly recommended build script</a>, and modified it for use with docker omitting a few python things as I'm using c++.</p>
  
  <p>Because I was having so much trouble with my real application, I've gone ahead and created a much simpler app, which also blows up during compilation. I believe this has something to do with the included <code>cflags</code> and <code>libs</code>. The problem is currently beyond my comprehension. I get mountains of errors that seem to change when I adjust a single include on my compile command. Here's the simplest app I'm trying to compile. It really doesn't do anything, but it does include a lib.</p>
  
  <pre><code>#include "opencv2/imgcodecs.hpp"
  using namespace cv; 
  Mat img;
  
  int main( int argc, char** argv ) {
    img = cv::imread( argv[1], IMREAD_COLOR );
  }
  </code></pre>
  
  <p>Then I try to compile this like so:</p>
  
  <pre><code>g++ -std=c++11 -static compile-test.cpp -o /app $(pkg-config --cflags --libs opencv)
  </code></pre>
  
  <p>And it ends up in a pile of errors much too long to completely paste here.</p>
  
  <pre><code>//usr/local/lib/libopencv_imgcodecs.a(grfmt_jpeg.cpp.o): In function `cv::JpegEncoder::write(cv::Mat const&amp;, std::vector&lt;int, std::allocator&lt;int&gt; &gt; const&amp;)':
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0xf8): undefined reference to `jpeg_CreateCompress'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x105): undefined reference to `jpeg_std_error'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x2b5): undefined reference to `jpeg_set_defaults'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x2d0): undefined reference to `jpeg_set_quality'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x2fe): undefined reference to `jpeg_quality_scaling'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x30d): undefined reference to `jpeg_quality_scaling'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x367): undefined reference to `jpeg_default_qtables'
  grfmt_jpeg.cpp:(.text._ZN2cv11JpegEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x379): undefined reference to `jpeg_start_compress'
  grfmt_jpeg.cpp:
  ...
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <h3>Some Things I've Already Tried</h3>
  
  <ul>
  <li>Beginning to google each of the seemingly unique compile errors and adding related flags to the end of my compile code.</li>
  <li>Reordering some of the include flags, but there are just too many to do this effectively</li>
  <li>Using the <code>opencv-dev</code> package instead of compiling it myself, but it seems you can't do this and expect to use static libs.</li>
  </ul>