gpt4 book ai didi

c++ - 将 OpenCV 构建为静态库会导致数以千计的 undefined reference

转载 作者:太空宇宙 更新时间:2023-11-04 11:47:57 24 4
gpt4 key购买 nike

我正在使用 Ubuntu 18.04,我想将 OpenCV(4.1.0) 构建为静态库并创建一个示例程序。构建 OpenCV 可以完美运行,但在运行测试应用程序时出现数千个错误。

构建 OpenCV:

  • 配置cmake:

    cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_SHARED_LIBS=OFF -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.1.0/modules -D CMAKE_INSTALL_PREFIX=/usr/local/opencv ..

  • 构建:

    make -j8

  • 安装:

    sudo make install

  • pkg-config 设置:

    sudo cp unix-install/opencv4.pc /usr/lib/x86_64-linux-gnu/pkgconfig/

示例程序

#include <stdio.h>
#include <opencv2/opencv.hpp>
int main( int argc, char** argv )
{
cv::Mat testmat;
printf("Test\n");
return 0;
}
  • 构建:

    g++ TestApp.cpp -o TestApp `pkg-config --cflags --libs opencv4`

/usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;cvImageWidget_class_init(void*, void*)&amp;apos;: window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0xa): undefined reference to &#96;gtk_widget_get_type&amp;apos; window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0x15): undefined reference to &#96;g_type_class_peek&amp;apos; window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0x20): undefined reference to &#96;g_type_check_class_cast&amp;apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;icvOnTrackbar(_GtkWidget*, void*)&amp;apos;: window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0xd): undefined reference to &#96;gtk_range_get_type&amp;apos; window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x18): undefined reference to &#96;g_type_check_instance_cast&amp;apos; window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x20): undefined reference to &#96;gtk_range_get_value&amp;apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;std::_Sp_counted_ptr_inplace&amp;lt;CvWindow, std::allocator&amp;lt;CvWindow&amp;gt;, (__gnu_cxx::_Lock_policy)2&amp;gt;::_M_dispose()&amp;apos;: window_gtk.cpp:
.text._ZNSt23_Sp_counted_ptr_inplaceI8CvWindowSaIS0_ELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv[_ZNSt23_Sp_counted_ptr_inplaceI8CvWindowSaIS0_ELN9__gnu_cxx12_Lock_policyE2EE10_M_disposeEv]+0x12): undefined reference to &#96;gtk_widget_destroy&amp;apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;icvWindowThreadLoop(void*)&amp;apos;: window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x41): undefined reference to &#96;gtk_main_iteration_do&amp;apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x53): undefined reference to &#96;g_usleep&amp;apos; window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x58): undefined reference to &#96;g_thread_yield&amp;apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x113): undefined reference to &#96;gtk_main_iteration_do&amp;apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x11d): undefined reference to &#96;g_usleep&amp;apos; window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x122): undefined reference to &#96;g_thread_yield&amp;apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;cvImageWidget_size_request(_GtkWidget*, _GtkRequisition*)&amp;apos;: window_gtk.cpp:
(.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x19): undefined reference to &#96;gtk_widget_get_type&amp;apos; window_gtk.cpp:
.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x47): undefined reference to &#96;g_type_register_static_simple&amp;apos; window_gtk.cpp:
.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x5b): undefined reference to &#96;g_type_check_instance_cast&amp;apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function &#96;cvImageWidget_set_size(_GtkWidget*, int, int)&amp;apos;: window_gtk.cpp:
.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x19): undefined reference to &#96;gtk_widget_get_type&amp;apos; window_gtk.cpp:.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x47): undefined reference to &#96;g_type_register_static_simple&amp;apos;

如有任何帮助,我将不胜感激。



测试修复:

显而易见的答案是 OpenCV 无法在静态 mod 中生成正确的 .pc 文件。 但我不认为那是问题。为了验证我构建了 OpenCV3.2.0(据我所知,.pc 文件在 3.2.0 中正确生成)并使用“sudo apt-get”安装了 perbuilt 版本安装 libopencv-dev”。预建的工作,但我得到了与自建版本相同的错误。我对这两个文件进行了差异检查,发现它们几乎相同。

差异检查: diff

opencv.pc(预构建): pre

opencv.pc(自建): self

构建输出(opencv): opencvbuild

构建输出(测试应用): testappbuild

最佳答案

我修好了

我的构建命令缺少 --static 标志。

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

现在它可以毫无问题地使用 OpenCV3.2.0 构建我的测试应用程序,但是当我使用 OpenCV4.1.0 时出现此错误。

/usr/bin/ld: cannot find -lgflags_shared

我认为我不需要这个库,所以我通过从 opencv.pc 文件中删除 -lgflags_shared 来解决这个问题。手动操作可行,但使用命令行更方便。

sed -i 's/-lgflags_shared //g' unix-install/opencv4.pc

关于c++ - 将 OpenCV 构建为静态库会导致数以千计的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57022152/

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