mersenneforum.org gcc 10.2.0 behavior
 Register FAQ Search Today's Posts Mark Forums Read

 2020-12-08, 21:47 #1 bsquared     "Ben" Feb 2007 7×491 Posts gcc 10.2.0 behavior A couple times now I have seen reports where gcc 10.2.0 failed to compile a project that older gcc versions (and icc) compiled just fine. They fail with errors in the linking stage; complaining about multiple definitions of global constants defined in headers (e.g., see here). I know that global definitions in headers is poor form, but it will be a pain to fix if that is the only solution going forward. The headers are all protected from being included twice using the usual #ifndef X #define X macros, which have worked up until now. Any thoughts on fixes, or ideas about what happened with this newer gcc version?
 2020-12-08, 22:00 #2 Nick     Dec 2012 The Netherlands 68916 Posts I tend to program defensively and avoid C's concept of "tentative definitions". If a variable declaration initializes the variable then the compiler must regard it as a definition. If a variable declaration says "extern" instead then the compiler must view it as merely a declaration (or have they changed the rules again?)
2020-12-08, 23:04   #3
bsquared

"Ben"
Feb 2007

1101011011012 Posts

Quote:
 Originally Posted by Nick I tend to program defensively and avoid C's concept of "tentative definitions". If a variable declaration initializes the variable then the compiler must regard it as a definition. If a variable declaration says "extern" instead then the compiler must view it as merely a declaration (or have they changed the rules again?)
I would think that has remained the same, but I don't keep up to date on these things.

For clarity, here is an example of what I'm talking about.

Code:
#ifndef HEADER1_H

/* defines */

/* typedefs */

/* function declarations */

/* globals */
int foo;
int bar;

#endif
Now, if two source files, say main.c and worker.c both #include header1.h, only one set of the globals should be defined. At least, that is how it has worked up to gcc 10.2.0.

If I have to update everything using externs to declare the globals and then define them once (probably in main.c), then so be it, but if there is a cheaper, hackier way to do what I want (compile the program as-is) then I'd like to at least consider it.

 2020-12-09, 01:48 #4 mathwiz   Mar 2019 3·53 Posts Possibly helpful? https://gcc.gnu.org/gcc-10/porting_to.html "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." Last fiddled with by mathwiz on 2020-12-09 at 01:49 Reason: quote relevant part of page
2020-12-09, 03:33   #5
bsquared

"Ben"
Feb 2007

1101011011012 Posts

Quote:
 Originally Posted by mathwiz Possibly helpful? https://gcc.gnu.org/gcc-10/porting_to.html "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."
Yeah, I'd say that explains it exactly. Looks like I have some work to do... in the meantime hopefully building with an earlier version of gcc is a workaround.

 2020-12-09, 05:26 #6 VBCurtis     "Curtis" Feb 2005 Riverside, CA 34×59 Posts If there's a flag that "defaults to -fno-common", can't we (well, you) just set that flag the opposite way at compile time?
2020-12-09, 12:20   #7
mathwiz

Mar 2019

100111112 Posts

Quote:
 Originally Posted by VBCurtis If there's a flag that "defaults to -fno-common", can't we (well, you) just set that flag the opposite way at compile time?
Perhaps, but it seems like sticking in the extern's is the more syntactically correct approach, and should work with all gcc versions without the need to fiddle with compiler flags...

2021-01-17, 16:38   #8
EdH

"Ed Hall"
Dec 2009

47·79 Posts

Quote:
 Originally Posted by VBCurtis If there's a flag that "defaults to -fno-common", can't we (well, you) just set that flag the opposite way at compile time?
The page referenced earlier does suggest:
Quote:
 Originally Posted by Porting to GCC 10 As a workaround, legacy C code where all tentative definitions should be placed into a common block can be compiled with -fcommon.
I have tried using this flag in a variety of places within the YAFU Makefile, with no success.

Quote:
 Originally Posted by mathwiz Perhaps, but it seems like sticking in the extern's is the more syntactically correct approach, and should work with all gcc versions without the need to fiddle with compiler flags...
The error file from a compile attempt resulted in 3876 "multiple definitions." This represents a substantial amount of work to resolve by editing to include "extern."

I am looking at a project to write a script to do all this editing based on the list of multiple definitions. But, my concern is that all the first instances of each definition did not flag the error. Should all those instances also include "extern," or, is a single point that doesn't, have to exist? If the latter, is there a correct place for the initial definition, such as the main code block?

 2021-01-17, 17:57 #9 EdH     "Ed Hall" Dec 2009 Adirondack Mtns E8116 Posts In some small playing, this appears beyond my capabilities and apparently not within my understanding, either. I tried to use extern for a couple of the "multiple definitions," but there are no second instances of declaration. If I "extern" the one reference, I get "undefined references." If I add a definition to the offended file, it does appear to fix that one issue, but I have no understanding of Ben's intricacies of the YAFU code and the editing is not scriptable from my point of view. Although not fully abandoned, I do not expect to play with this very much more, at least for now.
 2021-01-17, 18:27 #10 ldesnogu     Jan 2008 France 10001000102 Posts A trick is to play with #define extern.
 2021-01-17, 18:50 #11 bsquared     "Ben" Feb 2007 7·491 Posts I am actively working on this.

 Similar Threads Thread Thread Starter Forum Replies Last Post PageFault Software 2 2012-05-06 00:58 Chuck GPU Computing 2 2011-06-18 17:24 JuanTutors Software 0 2006-11-18 02:49 jinydu Lounge 23 2004-06-08 09:00 willmore Software 0 2002-09-09 20:17

All times are UTC. The time now is 18:16.

Mon May 10 18:16:59 UTC 2021 up 32 days, 12:57, 1 user, load averages: 3.75, 3.79, 3.56

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.