c - In function `_start': (.text+0x20): undefined reference to `main'

0

i know this has been asked before but I seem to cannot find the answer that i need. Compiler seems to work fine with other codes so I believe the problem is in the code. I'm using eclipse and first I thought it may be a configuration error but when I try in the command line it's the same error.

If you take a look I'd really appreciate it. Thanks!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#ifndef min
  #define min(x,y) (((x) < (y)) ? (x) : (y))
#endif

#define MAX_PROG_STEP_SIZE 10
#define MAX_COLS 80

static int init = 0;

#ifdef _WIN32

#include <windows.h>

void init_io(void) {
  system("cls");
  srand(time(NULL));
  init = 1;
}

CHAR GetCh(VOID) {
  HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
  INPUT_RECORD irInputRecord;
  DWORD dwEventsRead;
  CHAR cChar;

  while(ReadConsoleInputA(hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
    if (irInputRecord.EventType == KEY_EVENT) {
      cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
      ReadConsoleInputA(hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
      return cChar;
    }
  return EOF;
}

void shutdown_io(void) {
  // NOP
}

#else

#include <unistd.h>
#include <termios.h>
#define GetCh getchar

static struct termios old_termios_settings;

void init_io(void) {
  struct termios new_termios_settings;

  tcgetattr(0, &old_termios_settings);
  new_termios_settings = old_termios_settings;

  /* Disable canonical mode and echoing, and set buffer size to 1 byte */
  new_termios_settings.c_lflag &= (~ICANON & ~ECHO);
  new_termios_settings.c_cc[VTIME] = 0;
  new_termios_settings.c_cc[VMIN] = 1;

  tcsetattr(0, TCSANOW, &new_termios_settings);

  setvbuf(stdin, NULL, _IONBF, 0);
  system("clear");
  srand(time(NULL));
  init = 1;
}

void shutdown_io(void) {
  tcsetattr(0, TCSANOW, &old_termios_settings);
}

#endif

unsigned int get_progress(void) {
  static unsigned int progs = 0;
  unsigned int rand_val;
  unsigned int i;
  if (progs == 0xFFFFFFFF)
    return progs;

  rand_val = rand() % MAX_PROG_STEP_SIZE;
  i = rand() % 4;
  while (((progs >> (i * 8)) & 0xFF) == 0xFF) {
    i = (i + 1) % 4;
  }
  if (((progs >> (i * 8)) & 0xFF) + rand_val < 0xFF)
    progs += rand_val << (i * 8);
  else
    progs |= 0xFF << (i * 8);
  return progs;
}

void progressbar(char done_symbol, char active_symbol, char todo_symbol, unsigned int percent) {
  unsigned int w = MAX_COLS - 2;
  unsigned int limit = percent*w/100;
  unsigned int i;

  if (init == 0)
    exit(42);

  printf("\r[");
  for (i = 0; i < w; i++) {
    putchar(i < limit ? done_symbol : (i == limit ? active_symbol : todo_symbol));
  }
  printf("]\r");
  fflush(stdout);
}

void wait_key(void) {
  if (init == 0)
    exit(42);
  (void)GetCh();
}

and the console output is this:

make all 
Building target: progressWarmUp
Invoking: Cross GCC Linker
gcc  -o "progressWarmUp"  ./main.o   
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
makefile:29: recipe for target 'progressWarmUp' failed
collect2: error: ld returned 1 exit status
make: *** [progressWarmUp] Error

1

c
asked on Stack Overflow Sep 7, 2018 by zom

1 Answer

-1

I believe you're trying to build an object file.

gcc, when invoked by default, will try to build an executable. It cannot do that if it doesn't find a main function in one of the files you've specified to be compiled and linked.

To build just an object file, pass the -c flag to gcc when compiling your code.

gcc -o "progressWarmUp" -c ./main.o

This will tell gcc to stop after the compilation process and before the linking process. Note that if you actually want to execute this code yourself, you'll have to link it into an executable by linking it with an object file that has a defined main function.

answered on Stack Overflow Sep 7, 2018 by bool3max • edited Jun 4, 2019 by bool3max

User contributions licensed under CC BY-SA 3.0