opencv: Can not build Opencv statically on archLinux at all

System Information

OpenCV version: 4.7.0 Operating System / Platform: Arch Linux Compiler & compiler version: GCC 12.2.1

Detailed description

I have been trying to build opencv statically on archlinux and it seems that this is impossible, each time dynamic libraries are built instead.

I have tried to use the BUILD_SHARED_LIBS=OFF and OpenCV_STATIC variables, I even went inside the cmake directory and set these manually everywhere in the source code and it still building shared libraries.

This issue is only on archlinux, Debian and MacOS seems to be building static libs with the BUILD_SHARED_LIBS

Any thoughts?

Steps to reproduce

Trying this would not work

cmake -DBUILD_SHARED_LIBS=OFF ../

Issue submission checklist

  • I report the issue, it’s not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Perhaps these issues come from the OpenVDB library, it has several places where it sets variable in question: e.g. https://github.com/AcademySoftwareFoundation/openvdb/blob/e7ec2a716eac2bd03fc491d2d9639c6d5afd61cf/cmake/FindOpenVDB.cmake#L500 Somehow it got included into the build process, OpenCV does not use it, some other library should be involved here.

Hello, I couldn’t reproduce this issue on ubuntu 23.04 with cmake 3.26.1. So I feel this problem is not related with cmake version.

We need a more information to investigate/resolve this issue. If BUILD_SHARED_LIBS=OFF is ignored, we want to detect what lines makes it. Please could you try it ?

I prepared this snippet (Line 481 and 483), which is shown current line number in CMakeList.txt and show BUILD_SHARED_LIBS variable.

    479 # OpenCV build components
    480 # ===================================================
    481 message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )
    482 OCV_OPTION(BUILD_SHARED_LIBS        "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" NOT (ANDROID OR APPLE_FRAMEWORK) )
    483 message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )

All my test result are here. LINE 17 means initial value, LINE 1163 means finally value. In this case, BUILD_SHARED_LIBS parameter has been propagated from terminal/console to cmake correctly.

Case1: Default

$ /usr/local/bin/cmake -S opencv -B build4-main-cmake| grep "DEBUG"
[DEBUG] LINE 17:BUILD_SHARED_LIBS=
[DEBUG] LINE 481:BUILD_SHARED_LIBS=
[DEBUG] LINE 483:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=ON

Case 2: Defined with “ON”

$ /usr/local/bin/cmake -S opencv -B build4-main-cmake2 -DBUILD_SHARED_LIBS=ON | grep BUILD_SHARED_LIBS
[DEBUG] LINE 17:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 481:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 483:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=ON

Case 3: Defined with “OFF”

$ /usr/local/bin/cmake -S opencv -B build4-main-cmake2 -DBUILD_SHARED_LIBS=OFF | grep BUILD_SHARED_LIBS
[DEBUG] LINE 17:BUILD_SHARED_LIBS=OFF
[DEBUG] LINE 481:BUILD_SHARED_LIBS=OFF
[DEBUG] LINE 483:BUILD_SHARED_LIBS=OFF
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=OFF
All changes to debug CMakeList.txt
$ git --no-pager diff 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd47d83a2c..2bdaedd5b9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,7 @@ FATAL: In-source builds are not allowed.
 ")
 endif()
 
+message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )
 
 include(cmake/OpenCVMinDepVersions.cmake)
 
@@ -477,7 +478,9 @@ OCV_OPTION(WITH_FLATBUFFERS "Include Flatbuffers support (required by DNN/TFLite
 
 # OpenCV build components
 # ===================================================
+message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )
 OCV_OPTION(BUILD_SHARED_LIBS        "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" NOT (ANDROID OR APPLE_FRAMEWORK) )
+message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )
 OCV_OPTION(BUILD_opencv_apps        "Build utility applications (used for example to train classifiers)" (NOT ANDROID AND NOT WINRT) IF (NOT APPLE_FRAMEWORK) )
 OCV_OPTION(BUILD_opencv_js          "Build JavaScript bindings by Emscripten" OFF )
 OCV_OPTION(BUILD_ANDROID_PROJECTS   "Build Android projects providing .apk files" ON  IF ANDROID )
@@ -1157,6 +1160,8 @@ else()
 endif()
 string(STRIP "${OPENCV_COMPILER_STR}" OPENCV_COMPILER_STR)
 
+message("[DEBUG] LINE ${CMAKE_CURRENT_LIST_LINE}:" BUILD_SHARED_LIBS=${BUILD_SHARED_LIBS} )
+
 status("")
 status("  C/C++:")
 status("    Built as dynamic libs?:" BUILD_SHARED_LIBS THEN YES ELSE NO)

Apparently OpenVDB comes as VTK dependency (https://gitlab.kitware.com/vtk/vtk/-/blob/master/IO/OpenVDB/CMakeLists.txt). Disable VTK in OpenCV if you don’t need it (WITH_VTK=OFF) or maybe disabling it in VTK would help (VTK_MODULE_ENABLE_VTK_IOOpenVDB=NO)?

@mshabunin Sorry for being too late on this, I was really busy. I managed to run the command and extract the log related to BUILD_SHARED_LIBS in a specific file that is way much reduced in size.

This is the command I ran:

grep  -B 100 -A 2 -nr  'BUILD_SHARED_LIBS' trace.log  > reduced_trace.log

reduced_trace.log

I have done what has been requested by adding the debug message as it should be, here is my output:

case 1 Default:

/opencv_issue/build_opencv> cmake -S ../opencv -B build4-main-cmake| grep "DEBUG"
[DEBUG] LINE 17:BUILD_SHARED_LIBS=
[DEBUG] LINE 481:BUILD_SHARED_LIBS=
[DEBUG] LINE 483:BUILD_SHARED_LIBS=ON
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
<string>:1: DeprecationWarning:

  `numpy.distutils` is deprecated since NumPy 1.23.0, as a result
  of the deprecation of `distutils` itself. It will be removed for
  Python >= 3.12. For older Python versions it will remain present.
  It is recommended to use `setuptools < 60.0` for those Python versions.
  For more details, see:
    https://numpy.org/devdocs/reference/distutils_status_migration.html



Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mpi4py'
COMPONENT = openvdb
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=ON

Case 2: Defined with “ON”

opencv_issue/build_opencv> cmake -S ../opencv -B build4-main-cmake2 -DBUILD_SHARED_LIBS=ON | grep BUILD_SHARED_LIBS
[DEBUG] LINE 17:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 481:BUILD_SHARED_LIBS=ON
[DEBUG] LINE 483:BUILD_SHARED_LIBS=ON
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
<string>:1: DeprecationWarning:

  `numpy.distutils` is deprecated since NumPy 1.23.0, as a result
  of the deprecation of `distutils` itself. It will be removed for
  Python >= 3.12. For older Python versions it will remain present.
  It is recommended to use `setuptools < 60.0` for those Python versions.
  For more details, see:
    https://numpy.org/devdocs/reference/distutils_status_migration.html


Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mpi4py'
COMPONENT = openvdb
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=ON

Case 3: Defined with “OFF”

 cmake -S ../opencv -B build4-main-cmake2 -DBUILD_SHARED_LIBS=OFF | grep BUILD_SHARED_LIBS
[DEBUG] LINE 17:BUILD_SHARED_LIBS=OFF
[DEBUG] LINE 481:BUILD_SHARED_LIBS=OFF
[DEBUG] LINE 483:BUILD_SHARED_LIBS=OFF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy.distutils
<string>:1: DeprecationWarning:

  `numpy.distutils` is deprecated since NumPy 1.23.0, as a result
  of the deprecation of `distutils` itself. It will be removed for
  Python >= 3.12. For older Python versions it will remain present.
  It is recommended to use `setuptools < 60.0` for those Python versions.
  For more details, see:
    https://numpy.org/devdocs/reference/distutils_status_migration.html


Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'mpi4py'
COMPONENT = openvdb
[DEBUG] LINE 1163:BUILD_SHARED_LIBS=ON
exit 1