skip to Main Content

I’m trying to get stated with programmming in C, but when I try to compile "HelloWorld" to run I get a (.text+0x1b): undefined reference to `main’error. Edit: I am using wsl.

Ubuntu window:

dodojesu@DumbshitsINC:~$ dir
hello.c  hello.c.save
dodojesu@DumbshitsINC:~$ gcc hello.c -o hello
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/13/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
dodojesu@DumbshitsINC:~$

This is the hello world file:

#include <stdio.h>
int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

I’ve tried fixing it with the MS tutorials etc. but none of what I’ve found addresses this problem

Edit:

dodojesu@DumbshitsINC:~$ gcc –version

gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0

Copyright (C) 2023 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO

warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Edit: ld –version

GNU ld (GNU Binutils for Ubuntu) 2.42

Copyright (C) 2024 Free Software Foundation, Inc.

This program is free software; you may redistribute it under the terms of

the GNU General Public License version 3 or (at your option) a later version.

This program has absolutely no warranty.

2

Answers


  1. The reason of undefined reference to 'main' error is that compiler can’t find int main() function in your file. I have two suggestions of why this would happened.

    1. The hello.c have an unusual encoding (e.g. UTF16) and gcc fail to read it properly. Please use code editing tools, like VS Code or Emacs, to properly encode your files.
    2. hello.c have not been saved with the content you believe. Check the file with type hello.c on cmd or cat hello.c on powershell.
    Login or Signup to reply.
  2. The Error clearly says that undefined reference to `main' collect2: error: ld returned 1 exit status collect2: error: ld returned 1 exit status
    which means main was not defined in your hello.c and retuned 1 to the operating system

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search