Build targets in make file
Mar 2, 2021 at 11:33am UTC
I try to create a make file for different targets.
I get the the followinfg warnings.
makefile:15: warning: overriding recipe for target 'main.o'
makefile:9: warning: ignoring old recipe for target 'main.o'
clang -c main.c
clang main.o -o main.exe
Compilation finished successfully.
How do I fix the warnings
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
CC=clang
RELEASE_FLAGS= -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99 -DNDEBUG -O3
DEBUG_FLAGS = -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99 -g
release: main.o
$(CC) $(RELEASE_FLAGS) main.o -o main.exe
main.o: main.c
$(CC) $(RELEASE_FLAGS) -c main.c
debug: main.o
$(CC) $(DEBUG_FLAGS) main.o -o main.exe
main.o: main.c
$(CC) $(DEBUG_FLAGS) -c main.c
clean:
del *.o del output.exe del *.exp del *.lib del *.pdb
Last edited on Mar 2, 2021 at 11:36am UTC
Mar 2, 2021 at 12:28pm UTC
Don't you have do type make debug or make release ?
Mar 2, 2021 at 1:09pm UTC
Yes I tried both, but get the same warnings.
make release
makefile:15: warning: overriding recipe for target 'main.o'
makefile:9: warning: ignoring old recipe for target 'main.o'
clang -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99 -DNDEBUG -O3 main.o -o main.exe
C:\Users\Vedanta Centre\Desktop\Make Demo>
make debug
makefile:15: warning: overriding recipe for target 'main.o'
makefile:9: warning: ignoring old recipe for target 'main.o'
clang -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99 -g main.o -o main.exe
Mar 2, 2021 at 1:22pm UTC
I think you want something like...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
CC:= clang
CCFLAGS:= -Wall -Wextra -Wshadow -Wpedantic -Werror -std=c99
all: main.o
$(CC) $(CCFLAGS) main.o -o main.exe
main.o: main.c
$(CC) $(CCFLAGS) -c main.c
.PHONY: all debug release clean
debug: CCFLAGS += -g
debug: all
release: CCFLAGS += -DNDEBUG -O3
release: all
clean:
del *.o del output.exe del *.exp del *.lib del *.pdb
Last edited on Mar 2, 2021 at 1:35pm UTC
Mar 2, 2021 at 1:43pm UTC
I came up with something similar but using implicit rules. The only problem is that you need to remember to
make clean
before you make release (or just make) or make debug since it considers either one to already be built since the output files have the same name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
CC := clang
CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -Wshadow -Werror
RELEASE_FLAGS := -DNDEBUG -O3
DEBUG_FLAGS := -g
.PHONY: release debug clean
release: CFLAGS += $(RELEASE_FLAGS)
release: main.exe
debug: CFLAGS += $(DEBUG_FLAGS)
debug: main.exe
clean:
-rm -f *.o main.exe
Shouldn't -Wshadow be enabled by -Wpedantic? I'm not sure so I left it in.
I also used rm instead of del, which I know nothing about since I use linux.
Last edited on Mar 2, 2021 at 1:52pm UTC
Mar 2, 2021 at 3:49pm UTC
@Grey Wolf,
thanks, it's working.
@dutch,
it doesn't work.
make clean
del *.o main.exe
Could Not Find *.o
C:\Users\Vedanta Centre\Desktop\Make Demo>
make release
make: *** No rule to make target 'main.exe', needed by 'release'. Stop.
make debug
make: *** No rule to make target 'main.exe', needed by 'debug'. Stop.
C:\Users\Vedanta Centre\Desktop\Make Demo>
Mar 2, 2021 at 5:03pm UTC
I guess you need an explicit rule on windows for main.exe.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
CC := clang
CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -Wshadow -Werror
RELEASE_FLAGS := -DNDEBUG -O3
DEBUG_FLAGS := -g
.PHONY: release debug clean
release: CFLAGS += $(RELEASE_FLAGS)
release: main.exe
debug: CFLAGS += $(DEBUG_FLAGS)
debug: main.exe
main.exe: main.o
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f *.o main.exe
And remember that you can just say
make
to build the first target (release, in this case, since targets starting with a dot don't count).
A variable for main.exe might be a good idea:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
CC := clang
CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -Wshadow -Werror
RELEASE_FLAGS := -DNDEBUG -O3
DEBUG_FLAGS := -g
src := main.exe
obj := $(src:.exe=.o)
.PHONY: release debug clean
release: CFLAGS += $(RELEASE_FLAGS)
release: $(src)
debug: CFLAGS += $(DEBUG_FLAGS)
debug: $(src)
$(src): $(obj)
$(CC) $(CFLAGS) -o $@ $<
clean:
rm -f $(obj) $(src)
Last edited on Mar 2, 2021 at 5:43pm UTC
Mar 3, 2021 at 8:16am UTC
Thanks @dutch,
it's working now.
Topic archived. No new replies allowed.