88

How can I create global variables that are shared in C? If I put it in a header file, then the linker complains that the variables are already defined. Is the only way to declare the variable in one of my C files and to manually put in externs at the top of all the other C files that want to use it? That sounds not ideal.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • Shared within your own source code? Put extern declaration in one header file, and definition in another (single) source. Those are shared among your process threads of course. For interprocess sharing of data, use IPC mechanisms (shared memory, pipes). – Hernán Jun 09 '10 at 23:15
  • 1
    See also [What are `extern` variables in C?](http://stackoverflow.com/questions/1433204/what-are-extern-variables-in-c/1433387#1433387) – Jonathan Leffler May 20 '13 at 15:39
  • 2
    possible duplicate of [How do I share variables between different .c files?](http://stackoverflow.com/questions/1045501/how-do-i-share-variables-between-different-c-files) – 7hi4g0 Jun 03 '15 at 06:16
  • Possible duplicate of [How do I use extern to share variables between source files in C?](http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c) – jww Feb 18 '17 at 20:46

7 Answers7

98

In one header file (shared.h):

extern int this_is_global;

In every file that you want to use this global symbol, include header containing the extern declaration:

#include "shared.h"

To avoid multiple linker definitions, just one declaration of your global symbol must be present across your compilation units (e.g: shared.cpp) :

/* shared.cpp */
#include "shared.h"
int this_is_global;
Hernán
  • 4,527
  • 2
  • 32
  • 47
  • do you have any preferred references to learn more about IPC mechanisms? – NickO Feb 10 '13 at 23:03
  • 7
    Please put more emphasis on "just one declaration of your global symbol..." Kind of tripped on that one. I had the declaration on all the c files I wanted to use the global variables on :( – AntonioCS Dec 27 '13 at 00:21
70

In the header file write it with extern. And at the global scope of one of the c files declare it without extern.

CODE-REaD
  • 2,819
  • 3
  • 33
  • 60
Dig
  • 3,850
  • 3
  • 27
  • 33
22

In the header file

header file

#ifndef SHAREFILE_INCLUDED
#define SHAREFILE_INCLUDED
#ifdef  MAIN_FILE
int global;
#else
extern int global;
#endif
#endif

In the file with the file you want the global to live:

#define MAIN_FILE
#include "share.h"

In the other files that need the extern version:

#include "share.h"
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • ah this is the solution i had a while ago - i forgot about the MAIN_FILE preprocessor variable. i thinik i like the cur accepted answer more tho – Claudiu Jun 10 '10 at 04:03
15

You put the declaration in a header file, e.g.

 extern int my_global;

In one of your .c files you define it at global scope.

int my_global;

Every .c file that wants access to my_global includes the header file with the extern in.

nos
  • 223,662
  • 58
  • 417
  • 506
6

If you're sharing code between C and C++, remember to add the following to the shared.hfile:

#ifdef __cplusplus
extern "C" {
#endif

extern int my_global;
/* other extern declarations ... */

#ifdef __cplusplus
}
#endif
bfj7
  • 3
  • 4
tritinia
  • 61
  • 1
  • 1
2

There is a cleaner way with just one header file so it is simpler to maintain. In the header with the global variables prefix each declaration with a keyword (I use common) then in just one source file include it like this

#define common
#include "globals.h"
#undef common

and any other source files like this

#define common extern
#include "globals.h"
#undef common

Just make sure you don't initialise any of the variables in the globals.h file or the linker will still complain as an initialised variable is not treated as external even with the extern keyword. The global.h file looks similar to this

#pragma once
common int globala;
common int globalb;
etc.

seems to work for any type of declaration. Don't use the common keyword on #define of course.

ivan
  • 21
  • 1
  • You can also add `#ifndef common` `#define common extern` `#endif` to the header if you want to avoid defining common every time globals.h is included. The only caveat is that common is not undefined automatically. – GDavid Dec 12 '21 at 14:04
0

There is a more elegant way to create global variables.

Just declare the variables as static inside a ".c" source file and create set/get functions.

The example below I use to override malloc, realloc and free functions during memory allocation tests.

Example:

memory-allocator.h

#ifndef MEMORY_ALLOCATOR_H_
#define MEMORY_ALLOCATOR_H_

#include <stddef.h>

void std_set_memory_allocators(void *(*malloc)(size_t size),
                               void *(realloc)(void *ptr, size_t size),
                               void (*free)(void *ptr));

void std_set_reset_allocators();

void *std_malloc(size_t size);

void *std_realloc(void *ptr, size_t size);

void std_free(void *ptr);

#endif  // MEMORY_ALLOCATOR_H_

memory-allocator.c

#include "memory-allocator.h"

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

typedef struct {
    void *(*malloc)(size_t size);
    void *(*realloc)(void *ptr, size_t size);
    void (*free)(void *ptr);
} StdMemoryAllocator;

StdMemoryAllocator memory_allocators = {&malloc, &realloc, &free};

void std_set_memory_allocators(void *(*malloc)(size_t size),
                               void *(realloc)(void *ptr, size_t size),
                               void (*free)(void *ptr)) {
    memory_allocators.malloc = malloc;
    memory_allocators.realloc = realloc;
    memory_allocators.free = free;
}

void std_set_reset_allocators() {
    memory_allocators.malloc = malloc;
    memory_allocators.realloc = realloc;
    memory_allocators.free = free;
}

void *std_malloc(size_t size) {
    return memory_allocators.malloc(size);
}

void *std_realloc(void *ptr, size_t size) {
    return memory_allocators.realloc(ptr, size);
}

void std_free(void *ptr) {
    memory_allocators.free(ptr);
}

The struct static struct StdMemoryAllocator_s memory_allocators is started automatically when the application starts, and it point to the default C memory allocators.