出于测试目的,我需要使用静态库编译C ++ opencv程序。我的cmake构建选项为:
-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 \
我可以看到/usr/local/libs/ and the headers in /usr/local/include/
中都有文件。并且我有测试程序来显示使用highgui, core and imgcodec libs
的图像。读取类似的问题/线程我尝试使用以下命令编译程序:构建顺序至关重要:
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
但是它只会吐出许多错误,例如:
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'
显然OpenCV4.已使用pkgconfig
无论如何,有没有找到所需的库以及使用静态库进行构建的正确顺序?
更新:现在,修复脚本后,我只有几个错误 使用以下内容:
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
我现在只有:
/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
答案 0 :(得分:0)
在到处搜索后确定。我终于做到了。我正在使用docker容器作为我的工作环境。我正在使用OpenCV 4.0.1和Ubuntu 18.04。我的docker文件位于DockerFile。 (docker镜像仍是动态构建的,只需按照以下步骤启用静态构建即可。)
要成功成功进行静态编译,基本上有两件事要做。
-D BUILD_SHARED_LIBS=OFF
AND
-D OPENCV_GENERATE_PKGCONFIG=YES
第二个选项将避免您在编译过程中手动链接所有库的麻烦。这非常关键。编译时我只使用了典型的:
g++ main.cpp -o test `pkg-config --cflags --libs --static opencv4`
只记得将$ PKG_CONFIG_PATH设置为opencv4.pc所在的位置。我通过以下方式设置我的位置:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
您可以通过以下方式进行检查:echo $PKG_CONFIG_PATH
我希望这可以帮助任何努力做到这一点的人!
编辑:** Dockerfile