0

I'm trying to make a makefile that could run multiple commonds and create objects in different directories

all: clean debug release 

debug:
gcc -g -o debug/client.o client.c
gcc -g -o debug/server server.c -pthread
gcc -g -Wall -o debug/serverLog -DLOGFILE server.c -pthread

release:

gcc -Wall -o release/client client.c
gcc -Wall -o release/server server.c -pthread
gcc -Wall -o release/serverLog -DLOGFILE server.c -pthread

clean: 
-rm debug/client 
-rm debug/server 
-rm debug/serverLog
M Reza
  • 18,350
  • 14
  • 66
  • 71
  • 1
    In a [Makefile](https://en.wikipedia.org/wiki/Makefile) tabs are *very* significant (and often mandatory). Use several spaces to show them in your question – Basile Starynkevitch Jan 21 '19 at 03:10
  • 1
    On top of what Basile recommends, please state explicitly where you are using tabs and where spaces. I.e. demonstrate that the multispaces you use are actually intentional representations of tabs in your actual make file. – Yunnosch Jan 21 '19 at 04:11
  • 1
    Do you have trouble understanding the comments? – Yunnosch Jan 21 '19 at 04:39
  • 1
    Possible duplicate of [Make error: missing separator](https://stackoverflow.com/questions/920413/make-error-missing-separator) – tripleee Jan 21 '19 at 04:45
  • The rule `release` has a blank line. A blank line ends the rule, so the calls to `gcc` for the release versions will never be executed – user3629249 Jan 21 '19 at 06:25

1 Answers1

1

I figure it out

# "Debug" build - no optimization, and debugging symbols
DBG_FLAGS=-Wall -O0 -g

# "Release" build - optimization, and no debug symbols
REL_FLAGS =-Wall

all: debug release

debug: mkdirs
   gcc $(DBG_FLAGS) -o debug/client client.c
    gcc $(DBG_FLAGS) -o debug/server server.c
    gcc $(DBG_FLAGS) -o debug/serverLog -DLOGFILE server.c


release:mkdirs
    gcc $(REL_FLAGS) -o release/client client.c
    gcc $(REL_FLAGS) -o release/server server.c
    gcc $(REL_FLAGS) -o release/serverLog -DLOGFILE server.c

mkdirs:
   -mkdir debug
   -mkdir release


clean:
    -rm debug/* release/*
  • 1
    `mkdirs` should be marked `.PHONY`. And you should add `-O2` to `REL_FLAGS`, because the default for `gcc` is `-O0`. Also, put a space after the `=` – Basile Starynkevitch Jan 21 '19 at 05:52
  • At last, run `make -p` and take advantage of the many builtin rules of GNU `make` – Basile Starynkevitch Jan 21 '19 at 05:53
  • When compiling with `gcc` always enable the warnings (and then fix any problems that are exposed by the compiler. At a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` – user3629249 Jan 21 '19 at 06:27
  • in general, (and especially in a makefile) have separate rules for compile operations and for link operations – user3629249 Jan 21 '19 at 06:28
  • the posted makefile will fail if the user and/or the parent directory do not have the necessary permissions to create a directory or the directory already exists. Telling the makefile to ignore any problems with the call(s) to `mkdir` is a very bad idea – user3629249 Jan 21 '19 at 06:32
  • this target statement: `all: debug release` should be: `all: debug release clean` And there should be a statement before the `all` target that says: `.PHONY: all debug release clean` – user3629249 Jan 21 '19 at 06:36
  • using `MACRONAME = contents` results in the macro being re-evaluated every time it is referenced. (not to much of a problem in the posted makefile) however, in general, it should be: `MACRONAME := contents` so it is only evaluated once – user3629249 Jan 21 '19 at 06:41