简单的 HDF5 C++ 问题

Simple HDF5 C++ issue

我正在尝试使用执行基本的 C++ 脚本 (here)。我相信我已经正确安装了 HDF5 库:

sudo apt install libhdf5-dev

当我第一次尝试编译代码时使用:

g++ hdf5_simple.cpp -lhdf5 -lhdf5_cpp -o hdf5_simple

我得到了:

hdf5_simple.cpp:3:10: fatal error: H5Cpp.h: No such file or directory

够简单,我试试:

g++ -I/usr/include/hdf5/serial/ hdf5_simple.cpp -lhdf5 -lhdf5_cpp -o hdf5_simple

它没有任何问题,但是当我尝试 运行 时得到:

The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.10.4, library is 1.13.0
        SUMMARY OF THE HDF5 CONFIGURATION
        =================================

General Information:
-------------------
                   HDF5 Version: 1.13.0
                  Configured on: Wed Dec 22 18:04:31 UTC 2021
                  Configured by: ***@***
                    Host system: x86_64-unknown-linux-gnu
              Uname information: Linux *** 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
                       Byte sex: little-endian
             Installation point: /usr/local/hdf5

Compiling Options:
------------------
                     Build Mode: production
              Debugging Symbols: no
                        Asserts: no
                      Profiling: no
             Optimization Level: high

Linking Options:
----------------
                      Libraries: static, shared
  Statically Linked Executables: 
                        LDFLAGS: 
                     H5_LDFLAGS: 
                     AM_LDFLAGS: 
                Extra libraries: -lz -ldl -lm 
                       Archiver: ar
                       AR_FLAGS: cr
                         Ranlib: ranlib

Languages:
----------
                              C: yes
                     C Compiler: /usr/bin/mpicc ( MPICH version 3.3.2 Configured with: ../src/configure -v --with-pkgversion='Ubuntu 8.4.0-3ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu built with gcc version 8.4.0 (Ubuntu 8.4.0-3ubuntu2))
                       CPPFLAGS: 
                    H5_CPPFLAGS: -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L   -DNDEBUG -UH5_DEBUG_API
                    AM_CPPFLAGS: 
                        C Flags: 
                     H5 C Flags:  -std=c99  -Wall -Wcast-qual -Wconversion -Wextra -Wfloat-equal -Wformat=2 -Winit-self -Winvalid-pch -Wmissing-include-dirs -Wno-c++-compat -Wno-format-nonliteral -Wshadow -Wundef -Wwrite-strings -pedantic -Wlarger-than=2560 -Wlogical-op -Wframe-larger-than=16384 -Wpacked-bitfield-compat -Wsync-nand -Wstrict-overflow=5 -Wno-unsuffixed-float-constants -Wdouble-promotion -Wtrampolines -Wstack-usage=8192 -Wmaybe-uninitialized -Wdate-time -Warray-bounds=2 -Wc99-c11-compat -Wduplicated-cond -Whsa -Wnormalized -Wnull-dereference -Wunused-const-variable -Walloca -Walloc-zero -Wduplicated-branches -Wformat-overflow=2 -Wformat-truncation=1 -Wrestrict -Wattribute-alias -Wcast-align=strict -Wshift-overflow=2 -fstdarg-opt  -s  -Wbad-function-cast -Wimplicit-function-declaration -Wmissing-declarations -Wmissing-prototypes -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-sign -Wpointer-to-int-cast -Wint-to-pointer-cast -Wredundant-decls -Wstrict-prototypes -Wswitch -Wunused-function -Wunused-variable -Wunused-parameter -Wcast-align -Wunused-but-set-variable -Wformat -Wincompatible-pointer-types -Wint-conversion -Wshadow -Wcast-function-type -Wmaybe-uninitialized -Wno-aggregate-return -Wno-inline -Wno-missing-format-attribute -Wno-missing-noreturn -Wno-overlength-strings -Wno-jump-misses-init -Wno-suggest-attribute=const -Wno-suggest-attribute=noreturn -Wno-suggest-attribute=pure -Wno-suggest-attribute=format -Wno-suggest-attribute=cold -Wno-suggest-attribute=malloc -O3
                     AM C Flags: 
               Shared C Library: yes
               Static C Library: yes


                        Fortran: no

                            C++: no

                           Java: no


Features:
---------
                     Parallel HDF5: yes
  Parallel Filtered Dataset Writes: yes
                Large Parallel I/O: yes
                High-level library: yes
Dimension scales w/ new references: no
                  Build HDF5 Tests: yes
                  Build HDF5 Tools: yes
                      Threadsafety: no
               Default API mapping: v114
    With deprecated public symbols: yes
            I/O filters (external): deflate(zlib)
                               MPE: 
                     Map (H5M) API: no
                        Direct VFD: no
                        Mirror VFD: no
                (Read-Only) S3 VFD: no
              (Read-Only) HDFS VFD: no
                           dmalloc: no
    Packages w/ extra debug output: none
                       API tracing: no
              Using memory checker: no
   Memory allocation sanity checks: no
            Function stack tracing: no
                  Use file locking: best-effort
         Strict file format checks: no
      Optimization instrumentation: no
Bye...
Aborted (core dumped)

按照警告顶部的注释,我添加了 #define DF5_DISABLE_VERSION_CHECK 1 并重新编译,出现了相同的警告。然后我尝试使用 export LD_LIBRARY_PATH="/usr/include/hdf5/serial/" 添加到 LD_LIBRARY_PATH 的路径,但仍然没有成功。

我不确定出了什么问题。任何帮助将不胜感激。

试试这个:

g++ `pkg-config --cflags hdf5-serial` hdf5_simple.cpp `pkg-config --libs hdf5-serial` -lhdf5_cpp -o hdf5_simple

该命令将允许您针对 C++ 单线程 版本构建程序。

将 hdf5 移植到 Node.js(我是 Node.js 中 gdal-async 的作者),我可以证明它有一些可怕的怪癖:它有两个非常重要的编译选项、线程安全选项和 C++ API。这两者不兼容,当两者都启用时,库存 tarball 将拒绝构建。 Debian/Ubuntu 团队竭尽全力让您可以同时使用它们——只要不是同时使用——它要么是 C++(他们称之为高级 API)要么多线程。

好像这还不够,还有启用 MPI 的版本,正如我从您的配置中看到的那样,您似乎混合在它之上。

不要禁用任何警告和检查,并且要非常小心您链接程序的对象,否则您将在运行时遇到一些非常讨厌的意外。