问题
I'm trying to create a docker image with OpenCV 4.3 and my own C++ program linked against OpenCV, both built for Linux. Because building OpenCV takes quite a while and the resulting docker image is pretty large and I only need libopencv_core.so
and libopencv_calib3d.so
, I tried to create the following two layers:
- Build OpenCV from source and build my own C++ program that links against OpenCV
- Copy the shared OpenCV libraries and my compiled program created in the first build layer to the second layer and execute my program
Unfortunately, the linker outputs
error while loading shared libraries: libopencv_core.so.4.3: cannot open shared object file: No such file or directory
Adding
RUN ls /usr/local/lib
in the second layer just before the command to execute my program shows that I successfully copied the built library files:
libopencv_calib3d.so
libopencv_calib3d.so.4.3
libopencv_calib3d.so.4.3.0
libopencv_core.so
libopencv_core.so.4.3
libopencv_core.so.4.3.0
I already tried these statements to make the libraries visible:
RUN ldconfig /usr/local/lib
RUN export LD_LIBRARY_PATH="/lib:/usr/lib:/usr/local/lib"
RUN ldconfig -v
where ldconfig -v
prints (among other things):
/usr/local/lib:
libopencv_calib3d.so.4.3 -> libopencv_calib3d.so.4.3.0
libopencv_core.so.4.3 -> libopencv_core.so.4.3.0
I was a bit confused because ldconfig
didn't list libopencv_calib3d.so
, that's why I tried create a simlink manually but it complained that libopencv_calib3d.so
already exists. So it seems that everything is there to run my program. Any suggestions what I am doing wrong?
Appendix
This is the Dockerfile that I'm using (reduced to the important parts):
FROM vookimedlo/ubuntu-clang:clang_bionic AS builder
# Install dependencies
RUN apt-get update & ...
ARG libPath=/usr/local/lib
# Install OpenCV
RUN git clone & cmake & make install
tar -czvf ${libPath}/opencv.tar.gz /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_calib3d.so.4.* /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_core.so.4.*
# Download and build my program
...
FROM ubuntu AS runtime
ARG libPath=/usr/local/lib
# Copy compiled libraries
COPY --from=builder ${libPath}/opencv.tar.gz ${libPath}/opencv.tar.gz
RUN tar -xvf ${libPath}/opencv.tar.gz -C / && rm ${libPath}/opencv.tar.gz
# Copy my program
...
RUN ldconfig /usr/local/lib
RUN export LD_LIBRARY_PATH="/lib:/usr/lib:/usr/local/lib"
RUN ldconfig -v
# Run my program
CMD ...
回答1:
In OPs post they mention using .so.4.*
This will not get the .so.4 file, only the .so.4. files. I suspect what is happening is a .so -> .so.4 -> so.4-> .so.4.3 -> .so.4.3.0 link chain is missing a link.
To solve this the following lines:
# Install OpenCV
RUN git clone & cmake & make install
tar -czvf ${libPath}/opencv.tar.gz /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_calib3d.so.4.* /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_core.so.4.*
Should be updated to:
# Install OpenCV
RUN git clone & cmake & make install
tar -czvf ${libPath}/opencv.tar.gz \
/usr/local/lib/libopencv_calib3d.so* \
/usr/local/lib/libopencv_core.so*
(Line breaks and \ only added to make it easier to read for some)
A long list of ls -l /usr/local/lib/libopencv_* from the generated system to confirm.
来源:https://stackoverflow.com/questions/61001290/linker-in-second-docker-layer-doesnt-find-shared-opencv-libraries-built-in-prev