View Single Post
Old 2020-12-08, 23:04   #3
bsquared's Avatar
Feb 2007

D6E16 Posts

Originally Posted by Nick View Post
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.

#ifndef HEADER1_H
#define HEADER1_H

/* defines */

/* typedefs */

/* function declarations */

/* globals */
int foo;
int bar;

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.
bsquared is offline   Reply With Quote