![]() |
![]() |
#1 |
"Ben"
Feb 2007
65248 Posts |
![]()
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? |
![]() |
![]() |
![]() |
#2 |
Dec 2012
The Netherlands
11·151 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?) |
![]() |
![]() |
![]() |
#3 | |
"Ben"
Feb 2007
22×853 Posts |
![]() Quote:
For clarity, here is an example of what I'm talking about. header1.h: Code:
#ifndef HEADER1_H #define HEADER1_H /* defines */ /* typedefs */ /* function declarations */ /* globals */ int foo; int bar; #endif 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. |
|
![]() |
![]() |
![]() |
#4 |
Mar 2019
14910 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 |
![]() |
![]() |
![]() |
#5 | |
"Ben"
Feb 2007
22·853 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
#6 |
"Curtis"
Feb 2005
Riverside, CA
128A16 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?
|
![]() |
![]() |
![]() |
#7 |
Mar 2019
9516 Posts |
![]()
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...
|
![]() |
![]() |
![]() |
#8 | |||
"Ed Hall"
Dec 2009
Adirondack Mtns
24×3×7×11 Posts |
![]() Quote:
Quote:
Quote:
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? |
|||
![]() |
![]() |
![]() |
#9 |
"Ed Hall"
Dec 2009
Adirondack Mtns
24×3×7×11 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.
|
![]() |
![]() |
![]() |
#10 |
Jan 2008
France
3×181 Posts |
![]()
A trick is to play with #define extern.
|
![]() |
![]() |
![]() |
#11 |
"Ben"
Feb 2007
1101010101002 Posts |
![]()
I am actively working on this.
|
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
P-1, 4GB RAM on 32 bit OS, odd behavior | PageFault | Software | 2 | 2012-05-06 00:58 |
Don't understand this mfaktc behavior | Chuck | GPU Computing | 2 | 2011-06-18 17:24 |
Question about P-1 behavior | JuanTutors | Software | 0 | 2006-11-18 02:49 |
Strange Computer Behavior | jinydu | Lounge | 23 | 2004-06-08 09:00 |
Strange behavior on 1.7G Celeron | willmore | Software | 0 | 2002-09-09 20:17 |