4

I want to make static linking with library OpenCV, but I have some problems.
I use Linux (Ubuntu), IDE Clion, cmake and library OpenCV.

When I run project I get error:

[50%] Linking CXX executable OpenCVLinkingStatic /usr/bin/ld: attempted static link of dynamic object `/usr/lib/x86_64-linux-gnu/libopencv_videostab.so.2.4.9' collect2: error: ld returned 1 exit status CMakeFiles/OpenCVLinkingStatic.dir/build.make:112: recipe for target 'OpenCVLinkingStatic' failed make[3]: *** [OpenCVLinkingStatic] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/OpenCVLinkingStatic.dir/all' failed make[2]: *** [CMakeFiles/OpenCVLinkingStatic.dir/all] Error 2 CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/OpenCVLinkingStatic.dir/rule' failed make[1]: *** [CMakeFiles/OpenCVLinkingStatic.dir/rule] Error 2 Makefile:118: recipe for target 'OpenCVLinkingStatic' failed make: *** [OpenCVLinkingStatic] Error 2 

CMakeListst.txt:

cmake_minimum_required(VERSION 3.10) project(OpenCVLinkingStatic) set(CMAKE_CXX_STANDARD 11) find_package(OpenCV REQUIRED) set(SOURCE_FILES main.cpp) add_executable(OpenCVLinkingStatic main.cpp) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(OpenCVLinkingStatic ${OpenCV_LIBS} "-static") 

main.cpp:

#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace cv; int main(int argc, char** argv) { Mat image = imread("/opt/my.jpg", CV_LOAD_IMAGE_COLOR); namedWindow("Display window", WINDOW_AUTOSIZE); imshow("Display window", image); waitKey(0); return 0; } 

I installed openCV library via command "sudo apt-get install libopencv-dev" in terminal.

I have all needed labraries with expansion ".a":

libopencv_core.a libopencv_highgui.a libopencv_video.a libopencv_objdetect.a 

They are located in "/usr/lib/x86_64-linux-gnu/".

When I try execute command:

"gcc -static -std=c++11 main.cpp /usr/lib/x86_64-linux-gnu/libopencv_core.a /usr/lib/x86_64-linux-gnu/libopencv_highgui.a /usr/lib/x86_64-linux-gnu/libopencv_video.a /usr/lib/x86_64-linux-gnu/libopencv_objdetect.a -lm -lpthread -lstdc++ -o MyBinary". 

I get many lines containing each time typical errors:

"/usr/lib/x86_64-linux-gnu/libopencv_core.a(persistence.cpp.o): In function icvCloseFile(CvFileStorage*) : (.text._ZL12icvCloseFileP13CvFileStorage+0x52): undefined reference to gzclose /usr/lib/x86_64-linux-gnu/libopencv_core.a(persistence.cpp.o): In function icvEof(CvFileStorage*) : (.text._ZL6icvEofP13CvFileStorage+0x42): undefined reference to gzeof... /usr/lib/x86_64-linux-gnu/libopencv_highgui.a(window_gtk.cpp.o): In function cvImageWidget_class_init(_CvImageWidgetClass*) : (.text._ZL24cvImageWidget_class_initP19_CvImageWidgetClass+0xa): undefined reference to gtk_widget_get_type /usr/lib/x86_64-linux-gnu/libopencv_highgui.a(window_gtk.cpp.o): In function cvImageWidget_class_init(_CvImageWidgetClass*) : (.text._ZL24cvImageWidget_class_initP19_CvImageWidgetClass+0x15): undefined reference to gtk_type_class /usr/lib/x86_64-linux-gnu/libopencv_highgui.a(window_gtk.cpp.o): In function cvImageWidget_class_init(_CvImageWidgetClass*) (.text._ZL24cvImageWidget_class_initP19_CvImageWidgetClass+0x20): undefined reference to g_type_check_class_cast collect2: error: ld returned 1 exit status" 

Earlier I built openCV via Sources and endicated parameter "-DBUILD_SHARED_LIBS=OFF".
But it didn't work so, however I had all libraries with expansion ".a".
I got errors "indefined cv::imread" and other like this.
If I use dynamic linking all work correctly.
Can you help me resolve the problem?
Thanks.

1
  • 1
    My guess is that the .a you have are from a different version than the header files. Commented Apr 19, 2018 at 9:27

1 Answer 1

1

Usually in case of static linking Open CV core library requires some 3-d party libraries. For example zlib

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.