Ask coding questions

← Back to all posts
How do I go about to fix "undefined reference to 'main'"?
DiegoGuajardo2 (0)

This is my first time using C and replit and I don't know how to fix this. Thank you in advance!

Comments
hotnewtop
Coder100 (16990)

C only runs the main function. Other functions must be called.

Here ya go:

#include <stdio.h>

int hello();

int main() {
  hello();
  return 0;
}

int hello() {
  printf("Hello World! My name is Diego.");
}

don't forget to write safe C!

MocaCDeveloper (560)

In low level languages like C, the compiler needs what is called an "entry point".

In C, C++, Go, Rust etc, this entry point is called the "main function". In C/C++, you declare the main function as either an Integer, or a Void. In other languages like Rust, Go, Java, you declare with either declaring it as a static void(Java), or by putting func(Golang) in front of it, or whatever syntax the language supports for a function declaration.

Low-level languages, or should I say, compiled programming languages require a entry-point function(the main function) to compile. The main function is ALL the compiler looks it, and is ALL the compiler needs to successfully run the program.

If the main function is not found, compilation will fail, and the compiler will throw an error. If you know assembly, or have some basic common knowledge over the assembly language(whichever version you write), you'd know why a compiled programming language needs to have an entry-point function(called main).

If you don't know, here is a simple assembly program:

section .text
    global _start
    %define sys_call int 0x80

_start:
    mov eax, 1
    mov ebx, 0
    sys_call

_start indicates the "main" function in a compiled programming language. Whatever that language compiles to(some version of assembly), you can expect to see _start, main or some indication that this is the entry point of the program.

Nothing else runs but what is within the "main" function.

InvisibleOne (2686)

I believe it needs to be int main() so that you have a "main" object

DiegoGuajardo2 (0)

@InvisibleOne Thanks. When I switched it to main, it doesn't seem to find main.c

Is there a way for me to keep the file as hello.c and run it?