opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.42k stars 5.76k forks source link

Make error when compling with opencv_contrib-3.4.16 #3216

Open LiShuaixin opened 2 years ago

LiShuaixin commented 2 years ago
System information (version)
Detailed description

When compiled with opencv_contrib, it failed as:

[ 16%] Building CXX object modules/hdf/CMakeFiles/opencv_hdf.dir/src/hdf5.cpp.o
In file included from /usr/include/hdf5/mpich/hdf5.h:22,
                 from /home/lee/Library/opencv-3.4.16/opencv_contrib-3.4.17/modules/hdf/src/hdf5.cpp:37:
/usr/include/hdf5/mpich/H5public.h:60:13: fatal error: mpi.h: No such file or directory
   60 | #   include <mpi.h>
      |             ^~~~~~~
compilation terminated.
make[2]: *** [modules/hdf/CMakeFiles/opencv_hdf.dir/build.make:63: modules/hdf/CMakeFiles/opencv_hdf.dir/src/hdf5.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:5661: modules/hdf/CMakeFiles/opencv_hdf.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 17%] Built target opencv_cudaarithm
[ 20%] Built target opencv_imgproc
[ 20%] Linking CXX static library ../../../../../../lib/libmultiview.a
[ 20%] Built target multiview
make: *** [Makefile:163: all] Error 2

I can make smoothly without contrib-module. It seems like an issue with hdf5 and openmpi.... How should I fix the issue? Many thanks!!!!

alalek commented 2 years ago

As a workaround you could disable hdf module (refer to OpenCV build documentation): cmake ... -DBUILD_opencv_hdf=OFF

Kumataro commented 2 years ago

I'm sorry if the comment is wrong.

What is cause.

I think you maybe installed only parallel hdf5(libhdf5-mpich-dev) and not installed serial hdf5(libhdf5-dev). libhdf5-mpich-dev depends MPI library.

kmtr@kmtr:~/work/build$ LANG=C apt search libhdf5 | grep "\-dev"

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libhdf5-dev/impish 1.10.6+repack-5 amd64
libhdf5-mpi-dev/impish 1.10.6+repack-5 amd64
libhdf5-mpich-dev/impish,now 1.10.6+repack-5 amd64 [installed]
libhdf5-openmpi-dev/impish 1.10.6+repack-5 amd64

Currently contrib-hdf5 module supports only libhdf5-dev . It doesn't depend MPI Library.

So that to compile contrib-hdf5 with libhdf5-mpich-dev is failed with missing MPI library. (Maybe same trouble with libhdf5-openmpi-dev is happend too).

How to fix

There are 2 extra options.

1) sudo apt install libhdf5-dev to install serial hdf5.

CMake gives priority to libhdf5-dev than libhdf5-mpich-dev.

2) if you want to use parallel hdf5, CMakefile should be changed.

My test is not too enough to pull reqeust, which is only to run opencv_test_hdf and to check dependency library.

kmtr@kmtr:~/work/opencv_contrib$ git --no-pager diff
diff --git a/modules/hdf/CMakeLists.txt b/modules/hdf/CMakeLists.txt
index 2a1ae68b..2c0bb4a5 100644
--- a/modules/hdf/CMakeLists.txt
+++ b/modules/hdf/CMakeLists.txt
@@ -25,11 +25,28 @@ if(NOT HDF5_FOUND)
     ocv_module_disable(hdf) # no return
 endif()

+set(HDF5_ADD_LIBRARIES "")
+set(HDF5_ADD_INCLUDE_DIRS "")
+
+if(HDF5_IS_PARALLEL)
+    status("HDF5: Parallel")
+    find_package(MPI)
+
+    if(NOT MPI_FOUND)
+        status("HDF5_IS_PARALLEL is TRUE, but MPI is NOT FOUND -> hdf is disabled,")
+        ocv_module_disable(hdf) # no return
+    endif()
+    set(HDF5_ADD_LIBRARIES ${MPI_CXX_LIBRARIES})
+    set(HDF5_ADD_INCLUDE_DIRS ${MPI_CXX_INCLUDE_DIRS})
+else()
+    status("HDF5: Serial")
+endif()
+
 set(HAVE_HDF5 1)

 ocv_warnings_disable(CMAKE_CXX_FLAGS -Winvalid-offsetof)

 set(the_description "Hierarchical Data Format I/O")
 ocv_define_module(hdf opencv_core WRAP python)
-ocv_target_link_libraries(${the_module} ${HDF5_LIBRARIES})
-ocv_include_directories(${HDF5_INCLUDE_DIRS})
+ocv_target_link_libraries(${the_module} ${HDF5_LIBRARIES} ${HDF5_ADD_LIBRARIES})
+ocv_include_directories(${HDF5_INCLUDE_DIRS} ${HDF5_ADD_INCLUDE_DIRS})
kmtr@kmtr:~/work/build$ ldd bin/opencv_test_hdf | grep mpi
        libhdf5_mpich.so.103 => /lib/x86_64-linux-gnu/libhdf5_mpich.so.103 (0x00007f200bd1e000)
        libmpi.so.40 => /lib/x86_64-linux-gnu/libmpi.so.40 (0x00007f200bbf3000)
        libmpich.so.12 => /lib/x86_64-linux-gnu/libmpich.so.12 (0x00007f200b127000)
oguzakyuz commented 1 year ago

Make sure you check out the same tags for the opencv and opencv_contrib modules. For example do: git checkout tags/4.5.0 in both opencv folder and opencv_contrib folder.