{ }codelper

Error Messages › C

C undefined reference to

undefined reference to `main'

This error means a function is called somewhere, but its body (definition) is nowhere to be found. It happens after compiling is done, in the link step, where the pieces are joined into an executable. That is why it does not look like error: with a line number. It is a message from ld (the linker), and collect2: error: ld returned 1 exit status is added at the end. The name in quotes is the function that could not be found.

Cause 1. You misspelled the function name

If you type prinft for printf, the linker looks for a function called prinft, finds none, and gives up. In this case the compile step's implicit declaration of function warning comes first, so look at the line that warning points to.

The /tmp/cc....o on the line from ld is the name of a temporary file the compiler made. It changes every time you compile, so you can ignore it.

Wrong code

#include <stdio.h> int main(void) {    prinft("hello\n");    return 0;}
When you run it
main.c: In function ‘main’:
main.c:4:5: warning: implicit declaration of function ‘prinft’; did you mean ‘printf’? [-Wimplicit-function-declaration]
    4 |     prinft("hello\n");
      |     ^~~~~~
      |     printf
/usr/bin/ld: /tmp/ccxY32ZF.o: in function `main':
main.c:(.text.startup+0x10): undefined reference to `prinft'
collect2: error: ld returned 1 exit status

Edit and run it →

Fixed code

Fix the spelling of the name.

#include <stdio.h> int main(void) {    printf("hello\n");    return 0;}
Output
hello

Edit and run it →

Cause 2. There is a declaration but no body

int square(int x); is only a promise that such a function exists. Compilation passes on the strength of that promise, but if there is no body, or the name on the body differs from the declaration, the link step can't find it. Below, the name on the body is misspelled as sqaure.

Wrong code

#include <stdio.h> int square(int x); int main(void) {    printf("%d\n", square(5));    return 0;} int sqaure(int x) {    return x * x;}
When you run it
/usr/bin/ld: /tmp/ccuQfk6S.o: in function `main':
main.c:(.text.startup+0xc): undefined reference to `square'
collect2: error: ld returned 1 exit status

Edit and run it →

Fixed code

Make the name the same in the declaration, the call and the body.

#include <stdio.h> int square(int x); int main(void) {    printf("%d\n", square(5));    return 0;} int square(int x) {    return x * x;}
Output
25

Edit and run it →

If you split your code across several files, you get the same error when you don't compile the .c file that holds the body along with the rest. List them all, as in gcc main.c util.c.

Cause 3. You misspelled main

A C program starts at a function called main. If you misspell it, as in mian, or there is no main at all, the code that starts the program can't find main. That is why the error message shows names you never wrote, such as crt1.o.

Wrong code

#include <stdio.h> int mian(void) {    printf("hello\n");    return 0;}
When you run it
/usr/bin/ld: /usr/lib/gcc/aarch64-amazon-linux/11/../../../../lib64/crt1.o: in function `__wrap_main':
(.text+0x38): undefined reference to `main'
collect2: error: ld returned 1 exit status

Edit and run it →

Fixed code

Change it to main. Main with a capital letter does not work either.

#include <stdio.h> int main(void) {    printf("hello\n");    return 0;}
Output
hello

Edit and run it →

What to check when you see this error

  1. Look at the name in quotes. Does it match the function you meant to call, in spelling and in upper and lower case?
  2. If there is also an implicit declaration of function warning above it, the line number in that warning is the place to fix.
  3. If the name is main, check the spelling of your main function.
  4. If it is a function you wrote, check that the body (the part with { }) really exists and that its name matches the declaration.
  5. If you get this error for a math function name such as sqrt or pow with GCC on Linux on your own computer, add -lm to the end of the compile command (gcc main.c -lm). This site already compiles with it.
  6. The message differs between compilers. Visual Studio says LNK2019: unresolved external symbol, and clang on a Mac says Undefined symbols for architecture.

Practice problems

See also