静态链接c opencv

我想与库OpenCV进行静态链接,但是我遇到了一些问题.

我使用
Linux(Ubuntu),IDE Clion,cmake和库OpenCV.

当我运行项目时,我收到错误:

[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;
}

我在终端通过命令“sudo apt-get install libopencv-dev”安装了openCV库.

我有所有需要扩展“.a”的工作室:

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

它们位于“/usr/lib / x86_64-linux-gnu /”中.

当我尝试执行命令时:

"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".

每次典型错误都会包含很多行:

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

之前我通过Sources构建了openCV并且标注了参数“-DBUILD_SHARED_LIBS = OFF”.
但它不起作用,但是我所有的库都扩展了“.a”.
我得到错误“indefined cv :: imread”和其他类似的错误.
如果我使用动态链接,所有工作正常.
你能帮我解决这个问题吗?
谢谢.

最佳答案 通常在静态链接的情况下,Open CV核心库需要一些3-d方库.例如zlib

点赞