Search code examples
cmakefilemingw32windows-xp-sp3

One rule is not run on this Makefile


I'm running this makefile in WinXP:

Package=killerapp

Sources=main.c
Resource=resource\resource.rc

Objs=$(Sources:.c=.o)
Res_obj=$(notdir $(Resource:.rc=.o))

CC_RES=windres
CC=gcc

CFLAGS=
LDFLAGS=-mwindows

%.o.c:
    $(CC) $(CFLAGS) -c $<

$(Res_obj): $(Resource)
    $(CC_RES) $< -o $@

$(Package): $(Objs) $(Res_obj)
    $(CC) $(LDFLAGS) $^ -o $@

all:
    $(MAKE) $(Package)

clean:
    @del $(Objs)
    @del $(Res_obj)
    @del $(Package).exe

My idea is to first compile the resource file and later the source files, but seems that only windres is run and the source files are not compiled, any ideas?

Updated #1: This work as expected.

Package=killerapp

Sources=main.c
Resource=resource\resource.rc

Objs=$(Sources:.c=.o)
Res_obj=$(notdir $(Resource:.rc=.o))

CC_RES=windres
CC=gcc

CFLAGS=-O2 -Wall -Werror
LDFLAGS=-mwindows

all: $(Package)

%.o: %.c
    $(CC) $(CFLAGS) -c $<

$(Res_obj): $(Resource)
    $(CC_RES) $< -o $@

$(Package): $(Res_obj) $(Objs)
    $(CC) $(LDFLAGS) $^ -o $@

clean:
    @del $(Objs)
    @del $(Res_obj)
    @del $(Package).exe

run: $(Package)
    ./$^

Solution

  • The generic rule is incorrect. For Gnu make, use:

    %.o: %.c Makefile
        $(CC) $(CFLAGS) -c $<
    

    You should also replace the all target (no command needed):

    all: $(Package)
    

    And place it before any other target rules to make it the default target.

    I also suggest you enable the compiler warnings to catch common programming errors:

    CFLAGS=-O2 -Wall -Werror