Search code examples
clinker-errorsgnu-makewinmain

Make Failed on Undefined Reference to WinMain


I keep getting this linker error when building my application with GNU Make (v3.82.90). I've looked at the other answers on SO, but the answer continues to elude me.

gcc -o build obj/teos_init.o obj/teos_event.o obj/teos_task.o obj/teos_sem.o obj
/teos_linkedlist.o obj/teos_log.o  -Iinc/pvt  -Iinc/pub -fmax-errors=3 -std=c11
c:/libs/mingw/bin/../lib/gcc/mingw32/4.9.3/../../../libmingw32.a(main.o):(.text.
startup+0xa7): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
makefile:14: recipe for target 'build' failed
make: *** [build] Error 1

My makefile is such...

INCDIR = inc/pvt inc/pub
SRCDIR = src
OBJDIR = obj
LIBDIR = lib

CC=gcc
CFLAGS := $(foreach d, $(INCDIR), -I$d) -fmax-errors=3 -std=c11

_SRC = teos_init.c teos_event.c teos_task.c teos_sem.c teos_linkedlist.c teos_log.c
_OBJ := $(subst $(SRCDIR),$(OBJDIR),$(_SRC:%.c=%.o))
OBJ := $(patsubst %,$(OBJDIR)/%,$(_OBJ))

build: $(OBJ)
    $(CC) -o $@ $^ $(CFLAGS)

$(OBJDIR)/%.o: $(SRCDIR)/%.c
    @mkdir -p $(OBJDIR)
    $(CC) -c -o $@ $< $(CFLAGS)

$(OBJ): $(DEPS)

.PHONY: clean
clean:
    -rm -r $(OBJDIR)/*

This is my main function:

int main( int argc, char *argv[] )
{
   TEOS_ERROR err = TEOS_ERR_NO_ERROR;

   err = TEOS_TaskPoolInit();

   return err;
}

Any suggestions are much appreciated. Thanks.

UPDATE

This is not a duplicate. As indicated when I made this post, "I've looked at the other answers on SO, but the answer continues to elude me." I'm trying to build a console application using standard C libraries, not a Windows application.


Solution

  • Given that in a comment, you claim:

    the main function is in main.c

    and the output of the link operation (formatted for clarity) is

    gcc -o build 
        obj/teos_init.o 
        obj/teos_event.o 
        obj/teos_task.o 
        obj/teos_sem.o 
        obj/teos_linkedlist.o 
        obj/teos_log.o  
        -Iinc/pvt  -Iinc/pub -fmax-errors=3 -std=c11
    

    it's clear that your Makefile is not linking main.o so you don't have a main function.

    The reason for this is that main.c is not listed in your list of source files from which the list of object files to link is built..

    _SRC = teos_init.c teos_event.c teos_task.c teos_sem.c teos_linkedlist.c teos_log.c 
    # No main.c here
    

    Add main.c to the end of that line and see if that fixes anything.