gocv
gocv copied to clipboard
static link application with gocv
Description
i am trying to build an application with gocv with static link. However i get lots of errors.
Steps to Reproduce
- write Dockerfile
FROM golang:1.17-buster AS builder
ARG OPENCV_VERSION="4.5.3"
ENV OPENCV_VERSION $OPENCV_VERSION
RUN apt-get update && apt-get install -y curl unzip
RUN curl -Lo opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
unzip -q opencv.zip && \
curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
unzip -q opencv_contrib.zip && \
rm opencv.zip opencv_contrib.zip
RUN apt-get -y update && apt-get -y install unzip wget build-essential cmake curl git libgtk2.0-dev && \
apt-get install -y pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev && \
apt-get install -y libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
RUN cd opencv-${OPENCV_VERSION} && \
mkdir build && \
cd build && \
rm -rf * && \
cmake \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D BUILD_SHARED_LIBS=OFF \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${OPENCV_VERSION}/modules \
-D OPENCV_GENERATE_PKGCONFIG=ON .. && \
make -j $(nproc --all) && \
make install && \
ldconfig
RUN go clean --cache
RUN mkdir /go/test
COPY ./cmd/version /go/test
RUN cd /go/test && go mod init main && go mod tidy
RUN cd /go/test && \
go run main.go
- 构建镜像
sudo docker build -f ./Dockerfile -t docker_test:latest .
- get lots of err
/usr/bin/ld: dxt.cpp:(.text._ZNK2cv18DctIPPLoop_InvokerclERKNS_5RangeE[_ZNK2cv18DctIPPLoop_InvokerclERKNS_5RangeE]+0x236): undefined reference to `ippicvFree'
opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x23): undefined reference to `dlsym'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x19b): undefined reference to `dlopen'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x1b2): undefined reference to `dlsym'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x221): undefined reference to `dlopen'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x238): undefined reference to `dlsym'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x271): undefined reference to `dlclose'
/usr/bin/ld: opencl_core.cpp:(.text._ZL34OPENCL_FN_clReleaseEvent_switch_fnP9_cl_event+0x2ac): undefined reference to `dlclose'
/usr/bin/ld: /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): in function `OPENCL_FN_clGetExtensionFunctionAddress_switch_fn(char const*)':
race.cpp:(.text._ZN2cv5utils5trace7details6Region4ImplC2ERNS2_23TraceManagerThreadLocalEPS3_RS3_RKNS3_21LocationStaticStorageEl+0x12d): undefined reference to `__itt_id_create_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4ImplC2ERNS2_23TraceManagerThreadLocalEPS3_RS3_RKNS3_21LocationStaticStorageEl+0x170): undefined reference to `__itt_api_version_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4ImplC2ERNS2_23TraceManagerThreadLocalEPS3_RS3_RKNS3_21LocationStaticStorageEl+0x188): undefined reference to `__itt_domain_create_ptr__3_0'
/usr/bin/ld: /usr/local/lib/libopencv_core.a(trace.cpp.o): in function `cv::utils::trace::details::Region::Impl::leaveRegion(cv::utils::trace::details::TraceManagerThreadLocal&)':
trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x89): undefined reference to `__itt_metadata_add_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x98): undefined reference to `__itt_string_handle_create_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0xf9): undefined reference to `__itt_metadata_add_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x108): undefined reference to `__itt_string_handle_create_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x161): undefined reference to `__itt_task_end_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x25d): undefined reference to `__itt_metadata_add_ptr__3_0'
/usr/bin/ld: trace.cpp:(.text._ZN2cv5utils5trace7details6Region4Impl11leaveRegionERNS2_23TraceManagerThreadLocalE+0x270): undefined reference to `__itt_string_handle_create_ptr__3_0'
/usr/bin/ld: /usr/local/lib/libopencv_core.a(trace.cpp.o): in function `cv::utils::trace::details::Region::destroy()':
Your Environment
- Operating System and version: ubuntu18.04
- OpenCV version used: 4.5.3
- How did you install OpenCV? in docker
- GoCV version used: 0.29.0
- Go version: 1.17
- Did you run the
env.sh
orenv.cmd
script before trying togo run
orgo build
?
i change last run of my dockerfile as:
RUN cd /go/test && \
export CGO_CPPFLAGS="$(pkg-config --cflags opencv4)" && \
export CGO_LDFLAGS="-g -O2 -L/usr/lib -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu $(pkg-config --libs --static opencv4) " && \
GOOS=linux GOARCH=amd64 go build -v -ldflags "-extldflags "-static" -s -w" main.go
the err message become:
runtime/cgo
# runtime/cgo
/usr/bin/ld: cannot find -lIconv::Iconv
collect2: error: ld returned 1 exit status
i also try make static_install:
# builder image
FROM golang:1.17-buster AS builder
RUN apt-get update && apt-get install -y git sudo
RUN git clone https://github.com/hybridgroup/gocv.git
RUN cd gocv && make deps
RUN cd gocv && make download
RUN cd gocv && make sudo_pre_install_clean
RUN cd gocv && make build_static
RUN cd gocv && make sudo_install
RUN cd gocv && make clean
RUN cd gocv && make verify
i got also get lots of err message: like undefined reference...
I met the same problem, did u solve it yet?