Building OpenCV as a static library gives thousands of undefined references[SOLVED]
I am working on Ubuntu 18.04 and i want to build OpenCV(4.1.0) as static lib and create a sample program. Building OpenCV works flawlessly but i get thousands of errors when i run the test application.
Building OpenCV:
configure 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 ..
build:
make -j8
install:
sudo make install
pkg-config setup:
sudo cp unix-install/opencv4.pc /usr/lib/x86_64-linux-gnu/pkgconfig/
Sample Program
#include <stdio.h>
#include <opencv2/opencv.hpp>
int main( int argc, char** argv )
{
cv::Mat testmat;
printf("Test\n");
return 0;
}
build:
g++ TestApp.cpp -o TestApp `pkg-config --cflags --libs opencv4`
- I get these errors: Full Console.log
/usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_class_init(void*, void*)': window_gtk.cpp:(.text._ZL24cvImageWidget_class_initPvS_+0xa): undefined reference to `gtk_widget_get_type' window_gtk.cpp:(.text._ZL24cvImageWidget_class_initPvS_+0x15): undefined reference to `g_type_class_peek' window_gtk.cpp:(.text._ZL24cvImageWidget_class_initPvS_+0x20): undefined reference to `g_type_check_class_cast' /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvOnTrackbar(_GtkWidget*, void*)': window_gtk.cpp:(.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0xd): undefined reference to `gtk_range_get_type' window_gtk.cpp:(.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x18): undefined reference to `g_type_check_instance_cast' window_gtk.cpp:(.text._ZL13icvOnTrackbarP10_GtkWidgetPv+0x20): undefined reference to `gtk_range_get_value' /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `std::_Sp_counted_ptr_inplace<CvWindow, std::allocator<CvWindow>, (__gnu_cxx::_Lock_policy)2>::_M_dispose()': 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' /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `icvWindowThreadLoop(void*)': window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x41): undefined reference to `gtk_main_iteration_do' window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x53): undefined reference to `g_usleep' window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x58): undefined reference to `g_thread_yield' window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x113): undefined reference to `gtk_main_iteration_do' window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x11d): undefined reference to `g_usleep' window_gtk.cpp:(.text._ZL19icvWindowThreadLoopPv+0x122): undefined reference to `g_thread_yield' /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_size_request(_GtkWidget*, _GtkRequisition*)': window_gtk.cpp:(.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x19): undefined reference to `gtk_widget_get_type' window_gtk.cpp:(.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x47): undefined reference to `g_type_register_static_simple' window_gtk.cpp:(.text._ZL26cvImageWidget_size_requestP10_GtkWidgetP15_GtkRequisition+0x5b): undefined reference to `g_type_check_instance_cast' /usr/local/opencv/lib/libopencv_highgui.a(window_gtk.cpp.o): In function `cvImageWidget_set_size(_GtkWidget*, int, int)': window_gtk.cpp:(.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x19): undefined reference to `gtk_widget_get_type' window_gtk.cpp:(.text._ZL22cvImageWidget_set_sizeP10_GtkWidgetii+0x47): undefined reference to `g_type_register_static_simple'
I would be thankful for any help.
I kind of fixed it: The first error when building with OpenCV3.2.0 is undefined reference to `jpeg_std_error'. The -ljpeg library is linked under Libs.private in the .pc file and the command pkg-config --libs opencv32 returns only the Libs without the Libs.private params. So copying the Libs.private params to the Libs params "fixed" the issue.