Internal Error: get_unit():Bad internal unit KIND

Does anyone know why I’m getting this runtime error? I created a small test program based on a larger program but wasn’t able to recreate the error.

program test
  character t*15
  integer c
  c = 1490000
  write(t,'(I15)')c
  print *,"test is ", t
End Program test

I did a search on the Internet for this error which said to upgrade your compiler.

I ran “yum update” to update all the packages on my RHEL 7 64 bit virtual machine, but I’m still getting the same runtime error on the “above” write statement.

In the write statement, add iostat= and iomsg= clauses and the iomsg should tell you, the iostat will prevent the crash, hopefully

You report an error when running a program different from the short codes that you included. I suspect that get_unit() is a function in your compiler’s support library, and the error message may be caused by an error in your larger code or a bug in the support library itself.

1 Like

I traced the error message to the libgfortran source file unit.c. Line 551 of the file reads:

internal_error (&dtp->common, "get_unit(): Bad internal unit KIND");

Mysterious errors in Fortran are often the result of assigning values out of bounds of an array, or mismatched subroutine arguments. The error message rarely points to the a location near the actual error or mentions the actual cause. The only practical debugging techniques are to turn on bounds checking, use valgrind with gcc or use the Silverfrost compiler that can check for these at runtime. The alternative is to desk-check the entire program, as the actual error could be anywhere in the code.

Fortran compiler writers have a deep predudice against runtime checks, but the performance hit is often much smaller than anticipated and checks can be turned off in production (if necessary).

But all compilers I know of allow for such runtime checking. Just use the correct options. And sometimes you can ask them to check the interfaces even if the implementation is in some other file.

-fcheck=all already implies -fcheck=bounds, for which -fbounds-check is a deprecated alias. See Code Gen Options (The GNU Fortran Compiler).

With gfortran 8.5.0 on CentOS 8 I get this:

 Routine TEST Passed in:      1490000
 Routine TEST TEST1 is :         1490000
 Routine TEST: File create_lib.f, returning 1

Edit: I did not compile with explicit paths but:

cc -o lastinlastout step_1.c step_2.c ../library/the_library.a -lgfortran -lpthread -lf2c

I installed the library:

sudo dnf install f2c

With apt it’s the same package name (checked on Ubuntu):

sudo apt install f2c

According to the your earlier instructions, you managed to install something at /opt/libf2c at some point. So you could also just point your compiler to this directory -L/opt/libf2c. But giving the total path should also work.

Either /opt/libf2c/libf2c.a or -L/opt/libf2c -lf2c.

1 Like