Fix “redefinition of” & Multiple Definition Errors in C
C compilers abort with `redefinition of` when the same `.h` file is included twice without include guards, and linkers fail with `multiple definition of` when non-`inline` functions or non-`extern` global variables are defined directly inside headers included by two `.c` files.
Quick Fix Summary
Wrap every .h header with #ifndef HEADER_H / #define HEADER_H / #endif, put extern on shared header globals, and move function bodies or variable definitions into a single .c file.
Why This Error Happens
Why include guards alone do not fix multiple definitions
Include guards (#ifndef FOO_H) stop one .c file from seeing a header twice, but if a.c and b.c both #include "foo.h", any function body or initialized global in foo.h creates two identical symbols at link time (-fno-common is default in modern GCC/Clang).
Declaring vs defining across modules
Write extern int max_users; in config.h, and write int max_users = 100; once in config.c.
Top Root Causes & Specific Code Fixes
Header file included multiple times without #ifndef guards
Duplicate struct definitions inside the same translation unit trigger a compile error.
// user.h (no guard)
struct User { int id; };#ifndef USER_H
#define USER_H
struct User { int id; };
#endifNon-inline function or global definition inside a .h header
Both main.o and helper.o export the same global symbol, causing ld to abort.
// shared.h
int global_limit = 10;// shared.h
extern int global_limit;
// shared.c
int global_limit = 10;Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
int add(int a, int b) { return a + b; }
int add(int a, int b) { return a + b; }
int main(void) { printf("%d\n", add(1, 2)); return 0; }Open broken program in compilerRead the corrected program
#include <stdio.h>
static int add(int a, int b) { return a + b; }
int main(void) {
printf("%d\n", add(1, 2));
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
3Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| redefinition of `struct S` | Missing `#ifndef` include guard in `.h` | Add header guard macro | Wrap header contents in `#ifndef NAME_H ... #endif` |
| multiple definition of `g_var` / `fn` | Non-inline symbol defined directly in header | -Wall -fno-common | Use `extern` in `.h` and define once in `.c` |
Debugging Techniques and Commands
gcc -std=c17 -Wall -Wextra -c a.c b.c && gcc a.o b.o -o app
Exposes multiple definition symbols across translation units cleanly.
nm -g --defined-only *.o
Shows which object files export duplicate strong global symbols.
Frequently Asked Questions
Should helper functions in headers use static inline?
Yes, tiny header helpers should be marked `static inline`; larger functions belong as prototypes in `.h` and definitions in `.c`.
Why did old code compile until modern GCC enabled -fno-common?
Older C compilers silently merged tentative definitions (`int x;` in a header) as common symbols; modern compilers enforce strict one-definition linkage by default.
Is #ifndef portable vs #pragma once?
`#ifndef` include guards are 100% standardized ISO C and work reliably across every compiler and filesystem.