2

For testing purposes, I need to compile my C++ opencv program using static libs. My cmake build options are:

    -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D BUILD_DOCS=OFF \
    -D BUILD_EXAMPLES=OFF \
    -D BUILD_opencv_apps=OFF \
    -D BUILD_opencv_python2=ON \
    -D BUILD_opencv_python3=ON \
    -D BUILD_PERF_TESTS=OFF \
    -D BUILD_SHARED_LIBS=OFF \ 
    -D BUILD_TESTS=OFF \
    -D ENABLE_PRECOMPILED_HEADERS=OFF\
    -D FORCE_VTK=OFF \
    -D WITH_FFMPEG=OFF \
    -D WITH_GDAL=OFF \ 
    -D WITH_IPP=OFF \
    -D WITH_OPENEXR=OFF \
    -D WITH_OPENGL=OFF \ 
    -D WITH_QT=OFF\
    -D WITH_TBB=OFF \ 
    -D WITH_XINE=OFF \ 
    -D BUILD_JPEG=ON  \
    -D BUILD_TIFF=ON \
    -D BUILD_PNG=ON \

I can see that I have the files in both /usr/local/libs/ and the headers in /usr/local/include/ .And I have test program to display an image which uses highgui, core and imgcodec libs Reading similar questions/threads I tried compiling my program using the following where the build order is critical:

    g++ main.cpp -I /usr/local/include/opencv4 -L /usr/local/lib 
    -L /usr/local/share/opencv4 -L /usr/local/lib/opencv4 -lopencv_highgui \
    -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_highgui \
    -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching \
    -lopencv_video -lopencv_imgproc -lopencv_core -ljpeg -lpng -ltiff -lrt \
    -lz -lpthread -o binary

However it just spits out numerous errors like:

    usr/local/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvOnKeyPress(_GtkWidget*, _GdkEventKey*, void*)':window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0x98): undefined reference to `g_mutex_lock'
    window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0xaa): undefined reference to `g_cond_broadcast'
    window_gtk.cpp:(.text._ZL13icvOnKeyPressP10_GtkWidgetP12_GdkEventKeyPv+0xb6): undefined reference to `g_mutex_unlock'/usr/local/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidgetSetImage(_CvImageWidget*, void const*)':
    window_gtk.cpp:(.text._Z21cvImageWidgetSetImageP14_CvImageWidgetPKv+0x185): undefined reference to `gtk_widget_get_type' /usr/local/lib/libopencv_core.a(opencl_core.cpp.o): In function `OPENCL_FN_clGetExtensionFunctionAddress_switch_fn(char const*)':
opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x33): undefined reference to `dlsym'
opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x19c): undefined reference to `dlopen'
opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x1b3): undefined reference to `dlsym'
opencl_core.cpp:(.text._ZL49OPENCL_FN_clGetExtensionFunctionAddress_switch_fnPKc+0x296): undefined reference to `dlclose'

Apparently using pkgconfig has been dropped for OpenCV4. Is there anyway to find the libraries required and the proper order to build using static libraries?

UPDATE: Now I only have a few errors after fixing the script Using the following:

g++ main.cpp -I /usr/local/include/opencv4 -L/usr/local/lib -lopencv_dnn -lopencv_features2d -lopencv_calib3d -lopencv_flann -lopencv_objdetect -lopencv_ml -lopencv_photo -lopencv_video -lopencv_videoio -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lpng -ljpeg -o binary

I only now have:

/usr/bin/ld: /usr/local/lib/libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzeof'
//lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
Fer-de-lance
  • 375
  • 5
  • 23
  • Here: https://stackoverflow.com/questions/19901934/strange-linking-error-dso-missing-from-command-line – Nuzhny Mar 19 '19 at 07:54

1 Answers1

1

Ok after searching everywhere. I finally did it. I am using docker containers as my working environment. I am using OpenCV 4.0.1 and Ubuntu 18.04. My docker file can be found at DockerFile. (The docker image is still built dynamically just follow the steps below to enable static build.)

There are basically two things to do to static compile successfully.

  1. -D BUILD_SHARED_LIBS=OFF

    AND

  2. -D OPENCV_GENERATE_PKGCONFIG=YES

The second option will save you the hassle of manually linking all libraries during compilation. It is very critical. And to compile I just used the typical:

g++ main.cpp -o test `pkg-config --cflags --libs --static opencv4`

Just remember to set the $PKG_CONFIG_PATH to where the opencv4.pc is located.I set mine via:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

You can check it by: echo $PKG_CONFIG_PATH I hope this helps anyone struggling to do this!

EDIT: **Dockerfile

Fer-de-lance
  • 375
  • 5
  • 23