Fix “redefinition of main” & “multiple definition of main” in C
Every hosted C executable must have exactly one `main` entry point across all translation units linked together. Defining `main` twice inside one `.c` file fails during compilation (`redefinition of 'main'`), while linking two `.c` files that each have their own `main` fails during linking (`multiple definition of 'main'`).
Quick Fix Summary
Keep only one main function per executable target: rename helper test drivers into ordinary functions called from main, or compile each standalone exercise .c file into its own separate binary instead of gcc *.c.
Why This Error Happens
Why IDE "New File" inside an existing project causes multiple definition of main
Desktop IDEs (VS Code tasks using *.c, Code::Blocks, CLion, and Dev-C++ Projects) link every .c file in the project into one binary. Adding problem2.c with its own main() alongside problem1.c collides at link time.
Separate targets in Makefile / CMake
In CMake, register one add_executable(prob1 prob1.c) and add_executable(prob2 prob2.c) per standalone program rather than globbing all exercises together.
Top Root Causes & Specific Code Fixes
Pasting a second example below an existing main() in the same file
A single translation unit cannot define `main` more than once.
int main(void) { return 0; }
int main(void) { printf("second\n"); return 0; }static void run_second(void) { printf("second\n"); }
int main(void) { run_second(); return 0; }Running gcc *.c in a directory containing multiple standalone exercises
Wildcard `*.c` links every file in the folder into one executable.
gcc *.c -o homeworkgcc exercise1.c -o exercise1 && ./exercise1Complete 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 main(void) {
puts("Part 1");
return 0;
}
int main(void) {
puts("Part 2");
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
static void part1(void) { puts("Part 1"); }
static void part2(void) { puts("Part 2"); }
int main(void) {
part1();
part2();
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
Part 1
Part 2Debugging Techniques and Commands
grep -rnE '\bmain\s*\(' .Lists every source file and line defining `main` in the workspace.
gcc -std=c17 -Wall problem2.c -o problem2
Builds one standalone exercise cleanly without pulling in sibling `.c` files.
Frequently Asked Questions
Can I #include one .c file inside another .c file?
Avoid `#include "other.c"`: if both files define `main` or non-static globals, inclusion causes immediate redefinition errors. Compile `.c` files separately and share declarations via `.h` headers.
How do unit tests avoid colliding with the application main()?
Place application logic in library `.c` files (`app.c` without `main`), keep `src/main.c` as a thin entry wrapper, and link `tests/test_main.c` + `app.c` as a separate test executable.
Does static int main(void) fix multiple definition?
No—ISO C forbids declaring `main` as `static` in a hosted environment because the C runtime startup code (`_start`) needs external linkage to call `main`.