我正在开发Ubuntu18.04,我希望构建OpenCV(4.1.0)作为静态库,并创建一个示例程序。构建OpenCV是完美无缺的,但当我运行测试应用程序时,我会遇到数千个错误。
构建OpenCV:
样本程序
#include <stdio.h>
#include <opencv2/opencv.hpp>
int main( int argc, char** argv )
{
cv::Mat testmat;
printf("Test\n");
return 0;
}
pkg-config --cflags --libs opencv4
- **I get these errors:** [Full Console.log](http://m.uploadedit.com/bbtc/1562935328413.txt)
/usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_class_init(void*, void*)&apos;: window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0xa): undefined reference to `gtk_widget_get_type&apos; window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0x15): undefined reference to `g_type_class_peek&apos; window_gtk.cpp:
.text._ZL24cvImageWidget_class_initPvS_+0x20): undefined reference to `g_type_check_class_cast&apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvOnTrackbar(_GtkWidget*, void*)&apos;: window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0xd): undefined reference to `gtk_range_get_type&apos; window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x18): undefined reference to `g_type_check_instance_cast&apos; window_gtk.cpp:
.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x20): undefined reference to `gtk_range_get_value&apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `std::_Sp_counted_ptr_inplace&lt;CvWindow, std::allocator&lt;CvWindow&gt;, (__gnu_cxx::_Lock_policy)2&gt;::_M_dispose()&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 `gtk_widget_destroy&apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvWindowThreadLoop(void*)&apos;: window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x41): undefined reference to `gtk_main_iteration_do&apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x53): undefined reference to `g_usleep&apos; window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x58): undefined reference to `g_thread_yield&apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x113): undefined reference to `gtk_main_iteration_do&apos; window_gtk.cpp:
.text._ZL19icvWindowThreadLoopPv+0x11d): undefined reference to `g_usleep&apos; window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x122): undefined reference to `g_thread_yield&apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_size_request(_GtkWidget*, _GtkRequisition*)&apos;: window_gtk.cpp:
(.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x19): undefined reference to `gtk_widget_get_type&apos; window_gtk.cpp:
.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x47): undefined reference to `g_type_register_static_simple&apos; window_gtk.cpp:
.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x5b): undefined reference to `g_type_check_instance_cast&apos; /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_set_size(_GtkWidget*, int, int)&apos;: window_gtk.cpp:
.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x19): undefined reference to `gtk_widget_get_type&apos; window_gtk.cpp:.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x47): undefined reference to `g_type_register_static_simple&apos;
,我会感谢你的帮助。
测试修补程序:
显而易见的答案是,OpenCV无法在静态mod中生成正确的.pc文件。,但我不认为这是问题所在,为了验证我构建了OpenCV3.2.0(据我所知,.pc文件在3.2.0中正确生成),并安装了带有"sudo -dev“的超建版本。预置版起作用了,但我的自建版本也有同样的错误。我查看了这两个文件,它们几乎是一样的。
DiffCheck: 比较
opencv.pc(pre-build): 预
opencv.pc(self-build): 自我
build-output(opencv): 露天矿
build-output(testapp): 测试构建
发布于 2019-07-15 08:06:41
我修好了
我的构建命令缺少了-静态标志。
g++ TestApp.cpp -o TestApp `pkg-config --cflags --static --libs opencv4`
现在,它可以使用OpenCV3.2.0构建我的测试应用程序,没有任何问题,但是当我使用OpenCV4.1.0时会遇到这个错误。
/usr/bin/ld: cannot find -lgflags_shared
我认为我不需要这个库,所以我通过从-lgflags_shared文件中删除opencv.pc来修正这个问题。手动工作,但使用命令行更方便。
sed -i 's/-lgflags_shared //g' unix-install/opencv4.pc
https://stackoverflow.com/questions/57022152
复制相似问题