0

Currently I have a very simple Makefile. When I run it, it compiles my code with some specific options so I can debug it later on. I would like to be able to execute it with a normal "make" without option that creates my code withouth debug option, and one more complex, that add such as debuging. Currently my makefile only compiles with the debug options avaiable, see here the makefile:

main: main.o comandos.o utils.o
main.o:
    gcc -Wall -gstabs -DDEBUG -c src/main.c
comandos.o:
    gcc -Wall -gstabs -DDEBUG -c src/comandos.c
utils.o:
    gcc -Wall -gstabs -DDEBUG -c src/utils.c
clean:
    rm main \
    *.o

Any advise on how to customize it for both debugging and not debugging options? I was thinking to add a CFLAGS variable at the start, and supressing the variables at gcc lines, but not sure on how I can run on easy way differents values for CFLAGS, one for debuging, with -gstabs -DDEBUG, and only for normal, with only -Wall.

EDIT I: I am aware I can do different targets, and do a specific TARGET called Debug, but the issue I see there is that when I am generating a commands.o, for example, that will be used later on in target main, I already have commands.o precompiled with some specific flags. I am looking for away to have 2 differents targets generatings the same "commands.o", one with debbuging flags other with normal, and have the main target being able to use the correct one.

AndresG
  • 69
  • 6
  • How to compile with various options is a very classic question. Did you really search? – Renaud Pacalet Nov 06 '22 at 06:57
  • Does this answer your question? [How can I configure my makefile for debug and release builds?](https://stackoverflow.com/questions/1079832/how-can-i-configure-my-makefile-for-debug-and-release-builds) – tripleee Nov 06 '22 at 07:55
  • Creating the same output file with different options at different times defies the purpose of using `make`. Now how do you know if you need to recompile `main` to get a non-debug version? – tripleee Nov 06 '22 at 07:57
  • @RenaudPacalet I think I am able to do it using multiple targets (one target all, other debug) but on that way I would have duplicate targets also for commands.o, and have also that target generating different outputs (command_normal.o, and command_debug.o). Is there not a more easy way that avoid me using duplicate targets ? – AndresG Nov 06 '22 at 10:32

1 Answers1

0

I think I found the solution, my main problem was to define two different $FLAGS depending on the target, so I can have a default FLAGS when doing a "normal" release and different flags when I had a debug. I did it this way:

CFLAGS = -Wall
CC = gcc

main: main.o comandos.o utils.o
debug: CFLAGS = -Wall -gstabs -DDEBUG
debug: main
main.o:
    $(CC) $(CFLAGS) -c src/main.c
comandos.o:
    $(CC) $(CFLAGS) -c src/comandos.c
utils.o:
    $(CC) $(CFLAGS) -c src/utils.c
clean:
    rm main \
    *.o

Not sure if this is the correct way to do it, but seems is working. Thks to @tripple to redirect me to How can I configure my makefile for debug and release builds?.

AndresG
  • 69
  • 6