0

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.

MeiH
  • 1,763
  • 11
  • 17

2 Answers2

0

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 ....
rok
  • 2,574
  • 3
  • 23
  • 44
  • Thanks. Actually that's my problem, when we build inside docker, where will be those locations to specify? – MeiH Jul 28 '22 at 07:38
  • based on your make command should be `/usr/local/include/opencv4` for headers and `/usr/local/lib/` for libraries.. – rok Jul 28 '22 at 10:42
0

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`
MeiH
  • 1,763
  • 11
  • 17