https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96018
Bug ID: 96018 Summary: Optimization issue with external HDF5 library Product: gcc Version: 10.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: martin.schlipf at damnthespam dot com Target Milestone: --- Created attachment 48817 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48817&action=edit Minimal example to demonstrate the issue. On a clean Ubuntu 20.04 with the following setup sudo apt-get update sudo apt-get install gfortran-10 sudo apt-get install libhdf5-dev the attached code produces an error when compiled with optimization gfortran-10 -O2 hdf5_bug.f90 -I/usr/include/hdf5/serial/ -L/usr/lib/x86_64-linux-gnu/ -lhdf5_serial_fortran As far as I can tell, the reason this appears is that the compiler fails to recognize that the function call in line 98 has side effects and optimizes it away. If you check the status of the error code, the issue disappears. Strangely, the exact same sequence of calls with a 2d array instead of a 3d one works fine.