Linking fails with gcc 11.2.1 - multiple definition of many variables |
- Date: 2021/11/08 16:31
- Name: Pavel Ondracka
<pavel.ondracka@email.cz>
- Dear OpenMX developers,
I'm trying to compile OpenMX 3.9.9 with gcc 11.2.1 and the linking fails. Example end of very long output: /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:235: multiple definition of `SO_factor'; openmx.o:(.bss+0x46250): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:226: multiple definition of `EH0_scaling'; openmx.o:(.bss+0x46258): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:216: multiple definition of `Spe_Num_CBasis'; openmx.o:(.bss+0x46260): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:206: multiple definition of `Spe_Num_Basis'; openmx.o:(.bss+0x46268): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:196: multiple definition of `Spe_MaxL_Basis'; openmx.o:(.bss+0x46270): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:186: multiple definition of `Spe_AtomicMass'; openmx.o:(.bss+0x46278): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:177: multiple definition of `SpeVPS'; openmx.o:(.bss+0x46280): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:168: multiple definition of `SpeBasisName'; openmx.o:(.bss+0x46288): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:159: multiple definition of `SpeBasis'; openmx.o:(.bss+0x46290): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:150: multiple definition of `SpeName'; openmx.o:(.bss+0x46298): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/exx_interface_openmx.h:10: multiple definition of `g_exx'; openmx.o:(.bss+0x462a0): first defined here /usr/bin/ld: Cluster_DFT_Optical_ScaLAPACK.o:/home/ondracka/software/openmx3.9/source/openmx_common.h:118: multiple definition of `MPI_COMM_WORLD1'; openmx.o:(.bss+0x462a8): first defined here
The issue seems to be that the variables defined in header files need the extern keyword, and it seems the default gcc behavior will be now more strict than in the past in this regard. From the "Porting to GCC 10" https://gcc.gnu.org/gcc-10/porting_to.html:
Default to -fno-common
A common mistake in C is omitting extern when declaring a global variable in a header file. If the header is included by several files it results in multiple definitions of the same variable. In previous GCC versions this error is ignored. GCC 10 defaults to -fno-common, which means a linker error will now be reported. To fix this, use extern in header files when declaring global variables, and ensure each global is defined in exactly one C file. If tentative definitions of particular variables need to be placed in a common block, __attribute__((__common__)) can be used to force that behavior even in code compiled without -fcommon. As a workaround, legacy C code where all tentative definitions should be placed into a common block can be compiled with -fcommon.
int x; // tentative definition - avoid in header files extern int y; // correct declaration in a header file
| |