将OpenCV构建为静态库会导致数千个未定义的引用

时间:2019-07-13 19:21:35

标签: c++ linux opencv static ubuntu-18.04

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

构建OpenCV:

  • 配置cmake:

      

    cmake -D CMAKE_BUILD_TYPE =发布-D   OPENCV_GENERATE_PKGCONFIG = ON -D   BUILD_SHARED_LIBS = OFF -D   OPENCV_EXTRA_MODULES_PATH = .. / .. / opencv_contrib-4.1.0 /模块   -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”。预建的工作,但我与自建版本相同的错误。我检查了两个文件,它们几乎相同。

DiffCheck: diff

opencv.pc(预构建): pre

opencv.pc(自行构建): self

构建输出(opencv): opencvbuild

build-output(testapp): testappbuild

1 个答案:

答案 0 :(得分:0)

我已修复

我的构建命令缺少-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