Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile OpenCV statically linked with libstdc++

Tags:

opencv

mingw

I need to compile OpenCV statically linked with libstdc++ to avoid problems of different dll versions of libstdc++-6.dll needed by Qt5 and OpenCV. Following the steps of this article: http://www.argong.com/docs/how-to-OpenCV-2.2.0.pdf and adding the lines below to CMakeLists.txt i expected to get the OpenCV DLLs statically linked with libstdc++, but the OpenCV continues dependent of the libstdc++-6.dll. What i'm doing wrong to get the OpenCV libraries statically linked with libstdc++?

  if (MINGW)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static-libgcc")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++")
    set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS} -static-libgcc -s")
    set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS} -static-libgcc -static-libstdc++ -s")
  endif()
like image 244
Antonio Dias Avatar asked Dec 14 '25 14:12

Antonio Dias


1 Answers

First ensure you're not pulling dependencies on libraries that link on shared system libraries. You can for example enable compilation of bundled source components like zlib, jpeg, etc., with flags BUILD_ZLIB, BUILD_JPEG, etc. You can also disable dependencies on optional components like Vtk (WITH_VTK flag) and others that may be already present in the system as shared libraries. Then, if you are building OpenCV as a shared library, push the setting on the shared linker flags variable (CMAKE_SHARED_LINKER_FLAGS) in the cache with cmake -C command:

set(CMAKE_SHARED_LINKER_FLAGS "-static -static-libgcc -static-libstdc++" CACHE INTERNAL "" FORCE)

Alternatively, if you are compiling OpenCV statically with -DBUILD_SHARED_LIBS=FALSE, you can plug gcc linker flags for static linking on your final shared object or executable.

like image 66
ceztko Avatar answered Dec 18 '25 04:12

ceztko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!