gpt4 book ai didi

c++ - 如何编译静态 OpenCV4 库?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:03:29 29 4
gpt4 key购买 nike

出于测试目的,我需要使用静态库编译我的 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/和/usr/local/include/ 中都有文件。我有测试程序来显示使用的图像highgui、core 和 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'

显然使用 pkgconfig 已被删除 OpenCV4.无论如何找到所需的库以及使用静态库构建的正确顺序?

更新:修复脚本后我现在只有一些错误使用以下内容:

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

最佳答案

到处找就OK了。我终于做到了。我正在使用 docker 容器作为我的工作环境。我正在使用 OpenCV 4.0.1 和 Ubuntu 18.04。我的 docker 文件可以在 DockerFile 找到. (docker 镜像仍然是动态构建的,只需按照以下步骤启用静态构建即可。)

要成功进行静态编译,基本上需要做两件事。

  1. -D BUILD_SHARED_LIBS=OFF

  2. -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

关于c++ - 如何编译静态 OpenCV4 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55234833/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com