Ask Your Question
0

Building OpenCV as a static library gives thousands of undefined references[SOLVED]

asked 2019-07-12 07:57:12 -0500

Aaron Zettler gravatar image

updated 2019-07-15 21:53:51 -0500

supra56 gravatar image

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`

/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;

I would be thankful for any help.

edit retag flag offensive close merge delete

Comments

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.

Aaron Zettler gravatar imageAaron Zettler ( 2019-07-15 00:59:02 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2019-07-15 02:49:40 -0500

Aaron Zettler gravatar image

I fixed it

My build command was missing the --static flag.

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

Now it can build my test application with OpenCV3.2.0 with no problems but I get this error when i use OpenCV4.1.0.

/usr/bin/ld: cannot find -lgflags_shared

I dont think I need this lib so i fixed this by removing -lgflags_shared from the opencv.pc file. Doing it manually works but using the command line is more convenient.

sed -i 's/-lgflags_shared //g' unix-install/opencv4.pc
edit flag offensive delete link more

Comments

1

i fixed this by removing -lgflags_shared

that's probably ok. the sfm module from contrib is the only module depending on gflags (and most likely you don't build thatt one anyway)

berak gravatar imageberak ( 2019-07-15 03:42:00 -0500 )edit
0

answered 2019-07-12 10:35:43 -0500

mshabunin gravatar image

You will need to add necessary pkg-config files and libraries manually. OpenCV can not generate correct .pc file in this mode.

edit flag offensive delete link more

Comments

1

Thanks for the response. I dont think thats the problem. To verify i built OpenCV3.2.0(the .pc file generates correctly in 3.2.0 as far as i know) and installed the perbuilt version with "sudo apt-get install libopencv-dev". The prebuilt worked but i got the same errors with the self-built version. I diffchecked both files and they were pretty much the same.

DiffCheck:diff

opencv.pc(pre-build):pre

opencv.pc(self-build):self

build-output(opencv):opencvbuild

build-output(testapp):testappbuild

Aaron Zettler gravatar imageAaron Zettler ( 2019-07-12 13:12:56 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-07-12 07:57:12 -0500

Seen: 95 times

Last updated: Jul 15