Home > Blockchain >  How to build Opencv code in a docker file
How to build Opencv code in a docker file

Time:07-29

I want to build and run a c opencv code using docker. Here is my dockerfile:

FROM nvidia/cuda:11.5.0-cudnn8-runtime-ubuntu20.04
FROM ubuntu

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y \
    g   git wget cmake sudo

RUN apt-get install -y \
    build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev \
    python3-dev python3-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev \
    libcanberra-gtk-module libcanberra-gtk3-module

RUN git clone https://github.com/opencv/opencv.git && \
    cd /opencv && mkdir build && cd build && \
    cmake -D CMAKE_BUILD_TYPE=Release -D    CMAKE_INSTALL_PREFIX=/usr/local .. && \
    make -j"$(nproc)" && \
    make install  && ldconfig

The above commands makes my opencv libs, but I don't how to use it to run the actual code. I added this two lines at the end of the dockerfile (wav.cpp is the name of my cpp file that I want to run):

COPY . .

RUN g   -o wav wav.cpp

But at the end I get this error, which obviously says it can't find the opencv headers.

wav.cpp:2:10: fatal error: opencv2/imgproc.hpp: No such file or directory 2 | #include "opencv2/imgproc.hpp" | ^~~~~~~~~~~~~~~~~~~~~ compilation terminated.

Now how should I resolve this header (and lib) dependency problem? Thank you.

CodePudding user response:

You didn't specify the location for the headers and which libraries should be linked by gcc. Please take a look at the manual of gcc/g for the flags -I and -L. Should be something like this:

RUN g   -o wav wav.cpp -I <opencv header location> -L <opencv libs location> -lopencv_core ....

CodePudding user response:

Using @emrhzc's answer I could build my code inside the dockerfile. Now my final working command is:

RUN g   -o wav wav.cpp `pkg-config --cflags --libs opencv4`
  • Related