There appears to be a bug that the pgfortran compiler is optimizing away unused variables even when they are bind(c):
> pgfortran -c test.f90
> pgcc main.c
/usr/bin/ld: /tmp/andy/pgccFUIeD6ja2FdJ.o: in function `main':
/home/andy/pgi/a/main.c:4: undefined reference to `global_var'
> gfortran -c test.f90
> gcc main.c test.o
> ./a.out
9
> cat test.f90
module m
use iso_c_binding, only : c_int
integer(c_int), bind(c) :: global_var=9
end module
> cat main.c
#include <stdio.h>
extern int global_var;
int main () {
fprintf(stdout,"%d\n",global_var);
}
> pgfortran -V
pgfortran 20.4-0 LLVM 64-bit target on x86-64 Linux -tp nehalem
PGI Compilers and Tools
Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
> pgcc -V
pgcc (aka pgcc18) 20.4-0 LLVM 64-bit target on x86-64 Linux -tp nehalem
PGI Compilers and Tools
Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
Of course this is a highly unusual case in that the variable is not referenced at all from the Fortran.