Skip to content
multiple definition of '...'

multiple definition of '...'

DodaTech 3 min read

The multiple definition of '...' error means a symbol is defined more than once across translation units. The linker sees multiple definitions and cannot choose.

What It Means

You violated the One Definition Rule (ODR). In C++, every non-inline function and every object can have exactly one definition across the entire program. When you compile multiple source files that each contain a definition, the linker reports a conflict.

Why It Happens

  • A function is defined in a header that is included in multiple .cpp files
  • A global variable is defined in a header without extern
  • The same source file is compiled twice and both object files are linked
  • Missing header guards combined with multiple includes in the same translation unit
  • A non-inline function is defined inside a class definition

How to Fix It

Step 1: Separate declarations from definitions

// math.h — declaration only
#ifndef MATH_H
#define MATH_H
int add(int a, int b);
#endif
// math.cpp — single definition
#include "math.h"
int add(int a, int b) {
    return a + b;
}
// main.cpp
#include "math.h"

int main() {
    return add(2, 3);
}
g++ -c math.cpp -o math.o
g++ -c main.cpp -o main.o
g++ main.o math.o -o program

Step 2: Use header guards

// utils.h
#ifndef UTILS_H
#define UTILS_H

int process(int x);

#endif

Without the guard, including utils.h twice in the same file causes redefinition errors.

Step 3: Use inline for functions in headers

// config.h
#ifndef CONFIG_H
#define CONFIG_H

inline int getDefaultPort() {
    return 8080;
}

#endif

inline allows the same function definition to appear in multiple translation units without violating ODR.

Step 4: Use static or anonymous namespaces

// helper.h — internal linkage, each TU gets its own copy
static int helperFunction(int x) {
    return x * 2;
}

Or with anonymous namespace (preferred in C++):

namespace {
    int helperFunction(int x) {
        return x * 2;
    }
}

Step 5: Use extern for global variables

// globals.h
#ifndef GLOBALS_H
#define GLOBALS_H
extern int globalCounter; // declaration only
#endif
// globals.cpp
#include "globals.h"
int globalCounter = 0; // single definition
What is the One Definition Rule (ODR)?
The ODR states that every non-inline function, variable, type, class, and template can have at most one definition in the entire program. Declarations (like function prototypes) can appear multiple times. The linker enforces this rule when combining object files.
Can I have the same function in multiple .cpp files?
Yes, if you mark it static (internal linkage) or inline (permitted multiple definitions as long as they are identical). Without these keywords, each definition must appear in exactly one translation unit.
How do I find where a symbol is defined?
Use nm -C object.o | grep symbolName to list symbols in object files. Use objdump -t object.o | grep symbolName or readelf -s object.o | grep symbolName. To search across all object files in a project: nm -C *.o | grep "T symbolName" (T means the symbol is defined in the text/code section).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro