opencv icon indicating copy to clipboard operation
opencv copied to clipboard

Fix: compilation Issue on ARM64 (msys2 clangarm)

Open thiru31 opened this issue 1 year ago • 1 comments

Added a definition for M_PI in the code to resolve a compilation error encountered when building OpenCV on the MSYS2 environment. The M_PI constant was not defined, causing the compilation to fail.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • [x] I agree to contribute to the project under Apache 2 License.
  • [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • [x] The PR is proposed to the proper branch
  • [x] There is a reference to the original bug report and related work
  • [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name.
  • [ ] The feature is well documented and sample code can be built with the project CMake

thiru31 avatar Jun 29 '24 06:06 thiru31

Hello, my sample CMakeList.txt has not been tested, so if there are some build error, I'm sorry.


M_PI is not a part of C++(and C) Standard. See https://stackoverflow.com/questions/51344013/m-pi-imprecision

(I know this is only armchair theory, not reality.) For example, if someone uses compiler that has -DM_PI=42, this fix will not works well.

Instead of definiton M_PI in user source code, we can use M_PI, with "_USE_MATH_DEFINES` definition.

https://github.com/opencv/opencv/blob/4.x/3rdparty/carotene/CMakeLists.txt

      endif()
  endif()

+ if(MINGW) # TODO: What condition is better ?
+    target_compile_definitions(carotene_objs PRIVATE "-D_USE_MATH_DEFINES=1")
+ endif()

  # we add dummy file to fix XCode build
  add_library(carotene STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} "$<TARGET_OBJECTS:carotene_objs>" 
  "${CAROTENE_SOURCE_DIR}/dummy.cpp")

Or Add #define _USE_MATH_DEFINES before #include <cmath> in phase.cpp

Kumataro avatar Jun 29 '24 10:06 Kumataro

@Kumataro I have updated the PR to use _USE_MATH_DEFINES as suggested, by adding the following to CMakeLists.txt:

if(MINGW)
    target_compile_definitions(carotene_objs PRIVATE "-D_USE_MATH_DEFINES=1")
endif()

I have compiled and tested this change on my local ARM machine, and it resolves the compilation issue successfully.

Please review the changes and let me know if any further adjustments are needed. Thank you!

thiru31 avatar Jun 30 '24 19:06 thiru31

I think the new patch is preferable as it limits the impact to the MINGW environment only. Thank you very much !

Kumataro avatar Jun 30 '24 19:06 Kumataro