gocv icon indicating copy to clipboard operation
gocv copied to clipboard

Missing -lpng from cgo_static.go

Open kevinabrandon opened this issue 2 years ago • 0 comments

Description

I've been trying to get a static gocv build and was getting link errors having to do with png. After a lot of running around I found that if I added -lpng into cgo_static.go it fixes it.

Steps to Reproduce

  1. Install static opencv (see my instructions at the very bottom for that)
  2. cd $GOPATH/pkg/mod/gocv.io/x/[email protected]/cmd/version
  3. go build --tags static --ldflags '-extldflags "-static"' -v -x -a -o /tmp/go-version You end up with this at the very end of a ton of output:
TERM='dumb' g++ -I /home/kab/go/pkg/mod/gocv.io/x/[email protected] -fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b121=/tmp/go-build -gno-record-gcc-switches -o $WORK/b121/_cgo_.o $WORK/b121/_cgo_main.o $WORK/b121/_x001.o $WORK/b121/_x002.o $WORK/b121/_x003.o $WORK/b121/_x004.o $WORK/b121/_x005.o $WORK/b121/_x006.o $WORK/b121/_x007.o $WORK/b121/_x008.o $WORK/b121/_x009.o $WORK/b121/_x010.o $WORK/b121/_x011.o $WORK/b121/_x012.o $WORK/b121/_x013.o $WORK/b121/_x014.o $WORK/b121/_x015.o $WORK/b121/_x016.o $WORK/b121/_x017.o $WORK/b121/_x018.o $WORK/b121/_x019.o $WORK/b121/_x020.o $WORK/b121/_x021.o $WORK/b121/_x022.o $WORK/b121/_x023.o $WORK/b121/_x024.o $WORK/b121/_x025.o $WORK/b121/_x026.o $WORK/b121/_x027.o $WORK/b121/_x028.o $WORK/b121/_x029.o $WORK/b121/_x030.o $WORK/b121/_x031.o -L/usr/lib/x86_64-linux-gnu/ -lpng -L/usr/local/lib -L/usr/local/lib/opencv4/3rdparty -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_optflow -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_highgui -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_video -lopencv_videoio -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core -littnotify -llibprotobuf -lIlmImf -lquirc -lippiw -lippicv -lade -lz -ljpeg -ldl -lm -lpthread -lrt -lquadmath
# gocv.io/x/gocv
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngEncoder::write(cv::Mat const&, std::vector<int, std::allocator<int> > const&)':
grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x49): undefined reference to `png_create_write_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0xd2): undefined reference to `png_create_info_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0xfa): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x13f): undefined reference to `png_set_write_fn'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x20c): undefined reference to `png_set_compression_level'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x21b): undefined reference to `png_set_compression_strategy'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x275): undefined reference to `png_set_IHDR'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x28c): undefined reference to `png_write_info'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x2a1): undefined reference to `png_set_bgr'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x2ad): undefined reference to `png_set_swap'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x371): undefined reference to `png_write_image'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x384): undefined reference to `png_write_end'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x39e): undefined reference to `png_destroy_write_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x429): undefined reference to `png_set_packing'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x489): undefined reference to `png_set_filter'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngEncoder5writeERKNS_3MatERKSt6vectorIiSaIiEE+0x4d6): undefined reference to `png_init_io'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::readDataFromBuf(void*, unsigned char*, unsigned long)':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder15readDataFromBufEPvPhm+0x28): undefined reference to `png_get_io_ptr'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder15readDataFromBufEPvPhm+0xd8): undefined reference to `png_error'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::~PngDecoder()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoderD2Ev+0x78): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::~PngDecoder()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoderD0Ev+0x78): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::readData(cv::Mat&)':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0xfd): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x136): undefined reference to `png_set_swap'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x15c): undefined reference to `png_set_strip_alpha'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x1a2): undefined reference to `png_set_rgb_to_gray'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x1af): undefined reference to `png_set_interlace_handling'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x1bc): undefined reference to `png_read_update_info'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x214): undefined reference to `png_read_image'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x226): undefined reference to `png_read_end'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x24c): undefined reference to `png_get_valid'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x269): undefined reference to `png_get_eXIf_1'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x2f2): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x3b1): undefined reference to `png_set_tRNS_to_alpha'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x3e2): undefined reference to `png_set_gray_to_rgb'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x400): undefined reference to `png_get_valid'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x421): undefined reference to `png_get_eXIf_1'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x456): undefined reference to `png_set_palette_to_rgb'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x476): undefined reference to `png_set_bgr'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x486): undefined reference to `png_set_expand_gray_1_2_4_to_8'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder8readDataERNS_3MatE+0x4a7): undefined reference to `png_set_strip_16'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::readHeader()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x84): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xac): undefined reference to `png_create_read_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xc1): undefined reference to `png_create_info_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0xd1): undefined reference to `png_create_info_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x11b): undefined reference to `png_set_longjmp_fn'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x1a3): undefined reference to `png_init_io'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x1df): undefined reference to `png_read_info'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x208): undefined reference to `png_get_IHDR'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x27a): undefined reference to `png_get_tRNS'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x323): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: grfmt_png.cpp:(.text._ZN2cv10PngDecoder10readHeaderEv+0x358): undefined reference to `png_set_read_fn'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngDecoder::close()':
grfmt_png.cpp:(.text._ZN2cv10PngDecoder5closeEv+0x6e): undefined reference to `png_destroy_read_struct'
/usr/bin/ld: /usr/local/lib/libopencv_imgcodecs.a(grfmt_png.cpp.o): in function `cv::PngEncoder::writeDataToBuf(void*, unsigned char*, unsigned long)':
grfmt_png.cpp:(.text._ZN2cv10PngEncoder14writeDataToBufEPvPhm+0x4f): undefined reference to `png_get_io_ptr'
collect2: error: ld returned 1 exit status
  1. If you then edit the cgo_static.go to include a -lpng on line 10 (I put it between the -ljpeg -ldl) it builds just fine with the expected warnings

Your Environment

  • Operating System and version: Ubuntu 20.04.1
  • OpenCV version used: OpenCV 4.5.3
  • GoCV version used: 0.29.0
  • Go version: go version go1.16.3 linux/amd64
  • Did you run the env.sh or env.cmd script before trying to go run or go build? no, here's my build command:
go build --tags static --ldflags '-extldflags "-static"' -v -x -a -o /tmp/go-version
  • How did you install OpenCV?
cd ~/go/pkg/mod/gocv.io/x/[email protected]/ # replace X.YY.Z with whatever the current version is, as of this writing it was 0.29.0
make download                           # provided by gocv, this downloads the correct opencv to your /tmp/opencv folder
cd /tmp/opencv/opencv-X.Y.Z/            # replace X.Y.Z with whatever the current version is, as of this writing it was 4.5.3
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE  \
      -D CMAKE_INSTALL_PREFIX=/usr/local \                               # sets the install path to the global machine
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-X.Y.Z/modules \  # replace X.Y.Z with current version (4.5.3)
      -D WITH_JASPER=OFF \
      -D BUILD_DOCS=OFF \
      -D BUILD_EXAMPLES=OFF \
      -D ENABLE_CXX11=ON \
      -D WITH_INF_ENGINE=OFF \
      -D WITH_QT=OFF \
      -D WITH_GTK=OFF \               # not needed on lambda, but needed if you're going to use the gui stuff
      -D WITH_FFMPEG=OFF \
      -D WITH_CUDA=ON \
      -D WITH_TIFF=OFF \              # required to encode/decode tiff files, but for now we aren't doing it
      -D WITH_OPENJPEG=OFF \          # NOT required to encode/decode jpg files, they seem to work without it
      -D WITH_WEBP=OFF \
      -D WITH_QT=OFF \
      -D WITH_PNG=ON \                # required to encode/decode png files
      -D WITH_1394=OFF \
      -D HAVE_OPENEXR=OFF \
      -D BUILD_TESTS=OFF \
      -D BUILD_PERF_TESTS=OFF \
      -D BUILD_opencv_java=NO \
      -D BUILD_opencv_python=NO \
      -D BUILD_opencv_python2=NO \
      -D BUILD_opencv_python3=NO \
      -D BUILD_SHARED_LIBS=OFF \       # this is the secret sauce to keep things static
      -D OPENCV_GENERATE_PKGCONFIG=ON ..
make -j3            # replace 3 with whatever number of CPUs you want to use
sudo make install   # installs it to the prefix wiping away whatever previous installation you had
sudo ldconfig

kevinabrandon avatar Feb 14 '22 20:02 kevinabrandon