C Compiler Logo

Multiple Definition of Symbol

Multiple Definition of Symbol

linker

multiple definition of 'globalVar'

Description

The same symbol (variable or function) is defined in multiple source files.

Common Causes

Example of Error

Error Code

// In globals.h
int globalVar = 10;  // Definition in header

// In file1.c
#include "globals.h"

// In file2.c
#include "globals.h"  // globalVar is defined again

Solution

Use extern for declarations in header files and define variables only once.

Corrected Code

// In globals.h
extern int globalVar;  // Declaration only

// In globals.c
int globalVar = 10;  // Definition in one source file

// In file1.c and file2.c
#include "globals.h"  // Only gets the declaration

Additional Tips