-
Notifications
You must be signed in to change notification settings - Fork 791
Description
Hello everyone,
When building WRF on Rocky Linux (RHEL 8/9), I found errors related to PIE defaults in modern GCC. The issue does not occur on CentOS 7 or other versions, but appears consistently on Rocky Linux. I see that Rocky enables Position Independent Executable (PIE) by default.
Here is my environment:
OS: Rocky Linux 8.10 (also reproducible on Rocky 9.x)
Compiler: GCC 11.4 / 12.x
MPI: OpenMPI 4.x with mpif90 and mpicc wrappers
NetCDF: NetCDF-C 4.9.x, NetCDF-Fortran 4.6.x
./configure opt 34 and 1
errors appear after running ./compile em_real >& log.compile
( cd run ; if test -f namelist.input ; then
/bin/cp -f namelist.input namelist.input.backup.date +%Y-%m-%d_%H_%M_%S ; fi ;
/bin/rm -f namelist.input ; cp ../test/em_real/namelist.input . )
==========================================================================
build started: Fri Sep 5 12:53:49 EDT 2025
build completed: Fri Sep 5 12:56:47 EDT 2025
---> Problems building executables, look for errors in the build log <---
I looked around the log file and saw something unusual about the compiler:
grep -nEi "error:|fatal error|undefined reference|No such file|cannot find|ld: cannot" log.compile | head -n 20
1971:collect2: error: ld returned 1 exit status
4908:collect2: error: ld returned 1 exit status
5041:collect2: error: ld returned 1 exit status
5127:collect2: error: ld returned 1 exit status
5213:collect2: error: ld returned 1 exit status
around line 1971:
/usr/bin/ld: wrf_io.o: relocation R_X86_64_32 against .rodata' can not be used when making a PIE object; recompile with -fPIC /usr/bin/ld: field_routines.o: relocation R_X86_64_32 against .rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: module_wrfsi_static.o: relocation R_X86_64_32 against `.rodata' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
So I asked chatgpt for a while, here is its answer:
Cause
On Rocky Linux (and other RHEL ≥8 distros), GCC builds executables as PIE by default (-fPIE -pie). WRF’s configure system does not consistently apply -fPIC to all object files, so linking fails when PIE is enforced. On CentOS 7 (without PIE defaults), the same source builds successfully.
Workaround
Manually patch configure.wrf to: Add -fPIC to: SFC, DM_FC, CFLAGS_LOCAL, FCBASEOPTS_NO_G, and FCDEBUG.
Disable PIE when linking: LD = $(FC) -fno-pie -no-pie LDFLAGS_LOCAL = -fno-pie -no-pie With these adjustments, the build succeeds and all executables (real.exe, wrf.exe, ndown.exe, tc.exe) are produced correctly.
Suggested Fix To
improve compatibility with Rocky/RHEL ≥8 and modern GCC: Update arch/configure.defaults for gfortran/gcc targets to: Apply -fPIC consistently across Fortran and C compilation. Add -no-pie to linker flags when GCC ≥6 is detected or when building on PIE-enabled distros. This change would ensure out-of-the-box builds work on Rocky/Alma/RHEL clusters, which are becoming standard in HPC environments.
then it gave me the configure.wrf file (attachment) that works properly during the compile processes.
I wonder is there other ways to solve the problem instead of manually forcing the compiler option in configure.wrf?