Hi,

I've a multifile program in C and I need to use global variables. I've one header file that is called "global.h" where my global variable is defined. In the other files, there's an include directive to "global.h".

At the moment, I'm using my variable in the C files without declaration in the other files using the word extern and the program works. My question is: is that correct because I've read that global variables should be defined once and declared in evry file using those variables using the keyword extern?

global.h

int myVar;

main.c

include "global.h"
...
int someFct()
{
  myVar = something;
}

otherfile.c

include "global.h"
...
int otherFct()
{
  if (myVar == myCondition)
  {
  }
}

Thank you!

>I've read that global variables should be defined once and declared
>in evry file using those variables using the keyword extern?

Yes, that's a safe guideline. I'd do it like the following though, because it's easier to be explicit than get bitten by some of the more obscure declaration/definition rules:

/* globals.h */
extern int myVar; /* Declaration for use everywhere */
/* globals.c */
#include "globals.h"

extern int myVar = 0; /* Explicit definition with an initializer */
/* main.c */
include "global.h"
...
int someFct()
{
  myVar = something;
}
include "global.h"
...
int otherFct()
{
  if (myVar == myCondition)
  {
  }
}

Thank you.

Another small question plz. In global.h, I've to declare my variable as you mentionned using extern or without using extern?

/* globals.h */
extern int myVar; /* Declaration for use everywhere */

The extern keyword must be used in header files like global.h to avoid duplicate declaration errors when you include the header file in two or more .c files.

In my case, I obtain errors (undefined reference to `myVar') when I use extern in "global.h."

So I prefer declaring myVar without the keyword extern in "global.h"

This may not be 100% but I always though of global and external variables as indicators to the linker such that:

global variable is available out side of the file its defined in.
external variable is defined out side of the file its used in.

Edited 6 Years Ago by gerard4143: n/a

>In my case, I obtain errors (undefined reference to `myVar') when I use extern in "global.h."
Are you forgetting the definition? I added a globals.c file that forces an absolute definition for myVar. If you don't have that definition somewhere, the linker won't be able to reference the object.

>This may not be 100% but I always though of global
>and external variables as indicators to the linker

Technically there's no such thing as a global variable. What people usually mean by "global" is an object with file scope and external linkage which can be made visible in other scopes using an external declaration.

I don't have the file global.c ;-) Maybe for this reason I obtain the error.

I define myVar in "global.h" using the keyword extern and declare it in main.c also using the keyword extern. In main.c, I initialize myVar. These are setting in which it fives me error (undefined reference to `myVar').

When I remove extern from "global.h", it works perfectly.

>In my case, I obtain errors (undefined reference to `myVar') when I use extern in "global.h."
Are you forgetting the definition? I added a globals.c file that forces an absolute definition for myVar. If you don't have that definition somewhere, the linker won't be able to reference the object.

>This may not be 100% but I always though of global
>and external variables as indicators to the linker

Technically there's no such thing as a global variable. What people usually mean by "global" is an object with file scope and external linkage which can be made visible in other scopes using an external declaration.

I'm not arguing with you but global variables must be an accepted misconception..
Try gooling global variables in C

Edited 6 Years Ago by gerard4143: n/a

>I'm not arguing with you but global variables must be an accepted misconception..
It's well understood terminology, and everyone knows well enough what you mean when you say "global variable", so there's no point in being strictly correct when talking about them. It's like how dynamic arrays aren't really arrays. But everyone knows that when you say "dynamic array", you mean a simulated array with pointers and dynamic memory, so it's a convenient way to say the same thing with fewer words.

However, something unconventional like differentiating between "external variables" and "global variables" is an entirely different matter. It's the first time I've heard that particular distinction, so I felt the need to clarify. ;)

Edited 6 Years Ago by Narue: n/a

>I'm not arguing with you but global variables must be an accepted misconception..
It's well understood terminology, and everyone knows well enough what you mean when you say "global variable", so there's no point in being strictly correct when talking about them. It's like how dynamic arrays aren't really arrays. But everyone knows that when you say "dynamic array", you mean a simulated array with pointers and dynamic memory, so it's a convenient way to say the same thing with fewer words.

However, something unconventional like differentiating between "external variables" and "global variables" is an entirely different matter. It's the first time I've heard that particular distinction, so I felt the need to clarify. ;)

It must be a throw back from Linux/Unix assembler

7.54 `.global SYMBOL', `.globl SYMBOL'
======================================

`.global' makes the symbol visible to `ld'. If you define SYMBOL in
your partial program, its value is made available to other partial
programs that are linked with it. Otherwise, SYMBOL takes its
attributes from a symbol of the same name from another file linked into
the same program.

If we look at this simple program and compile it but do not assemble we get

#include <stdio.h>
#include <stdlib.h>

char ch[] = "Hello, World!\n";
int x = 89;
static int y = 90;

int main(int argc, char**argv)
{
	fputs(ch, stdout);
	fprintf(stdout, "ans x->%d\n", x);
	fprintf(stdout, "ans y->%d\n", y);
	exit(EXIT_SUCCESS);
}
.file	"testfile.c"
.globl ch
	.data
	.type	ch, @object
	.size	ch, 15
ch:
	.string	"Hello, World!\n"
.globl x
	.align 4
	.type	x, @object
	.size	x, 4
x:
	.long	89
	.align 4
	.type	y, @object
	.size	y, 4
y:
	.long	90
	.section	.rodata
.LC0:
	.string	"ans x->%d\n"
.LC1:
	.string	"ans y->%d\n"
	.text
.globl main
	.type	main, @function
main:

Note the globl symbols and especially y which doesn't have this symbol because its defined as static.

This question has already been answered. Start a new discussion instead.