1

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.

Jim Fell
  • 13,750
  • 36
  • 127
  • 202
  • And the `main` function is in one of the files you build with? – Some programmer dude Mar 20 '17 at 14:57
  • take a look at [this answer](http://stackoverflow.com/a/29406348/3436922) – LPs Mar 20 '17 at 14:57
  • @Someprogrammerdude Yes. – Jim Fell Mar 20 '17 at 14:59
  • @LPs I looked but it doesn't help. – Jim Fell Mar 20 '17 at 14:59
  • There are also *many* other possible duplicates here on SO for this problem. Do a search for e.g. `mingw winmain site:stackoverflow.com` and you will find quite a few of them. – Some programmer dude Mar 20 '17 at 14:59
  • @Someprogrammerdude As mentioned in my post, "I've looked at the other answers on SO, but the answer continues to elude me." – Jim Fell Mar 20 '17 at 15:00
  • 1
    Do you manage to link a simple hello world program with your MinGW installation? – a3f Mar 20 '17 at 15:06
  • Does it compile at all without the make file and any libraries? – Lundin Mar 20 '17 at 15:08
  • @JimFell: The cause is that the linker actually looks for a `WinMain` entry (what is required for a GUI application), when you give a `main` one (what is required for a console application). The reasons can be multiple: wrong mingw + gcc installation (some libraries missing), wrong type of executable (where do you declare that you want a console app?), etc. – Serge Ballesta Mar 20 '17 at 15:08
  • Possible duplicate of [Undefined reference to WinMain@16 - Codeblocks](http://stackoverflow.com/questions/29404780/undefined-reference-to-winmain16-codeblocks) – JeremyP Mar 20 '17 at 15:10
  • @JeremyP Already linked. OP said (wrongly...?) that is not the case.... – LPs Mar 20 '17 at 15:17
  • @LPs My comment was autoposted when I voted to close as duplicate. However, I'm now not certain it is an exact duplicate. The underlying problem is the samebut it is not obvious from the make file why it is compiling for a Windows app. I suspect it is the mingw libraries that are being used. – JeremyP Mar 20 '17 at 15:21
  • @SergeBallesta I'm new to GNU Make and makefiles in general. For now, I'm just trying to build a console application using standard C libraries. – Jim Fell Mar 20 '17 at 15:29
  • @JimFell: then use a tutorial to build and run the famous *Hello World!* prog on mingw. Once it works, look where are the differences with your own makefile. If you still have problems, just add the new informations here (or ask a new question and delete this one is it has received no answer) – Serge Ballesta Mar 20 '17 at 15:36
  • Start by trying to compile the canonical C Hello World program without make usinfg `gcc -o hello.exe hello.c` and see if that works. – JeremyP Mar 20 '17 at 15:37
  • @JimFell As a matter of interest, which C file is your main function defined in? – JeremyP Mar 20 '17 at 15:45
  • @JimFell Also please show the entire output from make. We need to see the switches to the lines that compile the C files. – JeremyP Mar 20 '17 at 16:02
  • @JeremyP Thanks, the `main` function is in main.c, and the entire build output is listed in my original post. – Jim Fell Mar 20 '17 at 17:30

1 Answers1

5

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161