< Gcc 10 porting notes

Gcc 10 porting notes/fno common

Overview

gcc-10 and above flipped a default from -fcommon to -fno-common.

This changed gcc code generator to emit globals without explicit initializer from .bss (via COMMON symbol type) to .data (via DEFAULT symbol type).

Example

The problem

GCC will reject multiple definitions of global variables starting from gcc-10:

CODE a.c
<syntaxhighlight lang="c">int a = 42;</syntaxhighlight>
CODE main.c
<syntaxhighlight lang="c">int a;
int main(){}</syntaxhighlight>
user $gcc a.c main.c -o main
ld: a.o:(.bss+0x0): multiple definition of `a'; main.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status

The fix (source changes, preferred)

Explicitly mark declarations as such and avoid multiple definitions in order to fix the bug.

CODE a.c
<syntaxhighlight lang="c">int a = 42;</syntaxhighlight>
CODE main.c (PATCHED)
<syntaxhighlight lang="c">extern int a;
int main(){}</syntaxhighlight>

The -fcommon workaround (discouraged)

If the problem is not that easy to fix or you are afraid it can break the program you then can report a bug upstream to make the decision on a correct fix.

As a local workaround you can revert back to old behaviour:

src_configure() {
  # discouraged, source change fix is preferred
  append-cflags -fcommon # https://link/to/upstream/bug/report
  econf ...
}

Reproducing the build failure with older gcc versions

The following command can also be used to test your patch

user $CFLAGS="-fno-common" ebuild mycat/mypkg-1.2.3 clean compile install

Links

This article is issued from Gentoo. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.