0

could anyone help me create a makefile for my project? I need 3 binaries to be created from the makefile.

The binaries compile individually like this:

gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o serverThreaded

gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o server

(So it creates 2 identical binaries but with different names, serverThreaded and server)

and also I need this from the makefile too:

gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread client.c -o client

Edit: I think this is what I need?

all: serverThreaded server client

    gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o serverThreaded 
    gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o server 
    gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread client.c -o client 
    .PHONY: all
toastedDeli
  • 570
  • 5
  • 28
  • 2
    This question made the impression that you already have a `makefile` that creates one file, and now you want to change it so it creates 3 files. Now (after the edit) it seems that you don't have any `makefile` at all! You might want to consider not using `makefile`, and using just a shell-script to do your compilation. Or look [here](https://stackoverflow.com/a/2481307/509868) for an example of a simple `makefile`. – anatolyg Oct 24 '17 at 13:01
  • @anatolyg My assignment is going to be automatically compiled from the makefile, so it's unfortunately a requirement for me to make one. I worked out how to use it to compile one file but that's all I can get to work – toastedDeli Oct 24 '17 at 13:04

2 Answers2

5

You can add a phony rule (that is a rule that doesn't build a file itself) which has whatever you want to build as its prerequisites. By convention, this rule is called all in most Makefiles. For your case, it would look like this:

all: serverThreaded server client

It's also common to put this as the very first rule, so if you just type make without a target, it is automatically selected.

An important thing to do is to let make know this rule actually is "phony" by putting it in the prerequisites of the special target .PHONY like this:

.PHONY: all

This is necessary because otherwise, make would expect it to build a file called all. If you'd ever have a file all in your current directory which is newer than all files you actually build, make wouldn't do anything.


Regarding the edit, it doesn't make any sense. Explaining make entirely isn't possible in this Q&A format, so I'll just give you an example how a very basic Makefile could look like, as a start:

CC:= gcc
CFLAGS:= -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread
LIBS:= -pthread

serverThreaded_OBJS:= main.o service_client_socket.o service_listen_socket.o get_listen_socket.o
server_OBJS:= main.o service_client_socket.o service_listen_socket.o get_listen_socket.o
client_OBJS:= client.o

all: serverThreaded server client

serverThreaded: $(serverThreaded_OBJS)
    $(CC) -o$@ $^ $(LIBS)

server: $(server_OBJS)
    $(CC) -o$@ $^ $(LIBS)

client: $(client_OBJS)
    $(CC) -o$@ $^ $(LIBS)

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

clean:
    rm -f *.o

.PHONY: all clean

As a side note, it's strange how your serverThreaded and server are built from the exact same sources with the exact same flags -- you will end up with the exact same binaries.

  • My knowledge of make files is poor. Would my complete makefile look like this then? `all: serverThreaded server client gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o serverThreaded` `gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread main.c service_client_socket.c service_listen_socket.c get_listen_socket.c -o server` `gcc -D_POSIX_SOURCE -Wall -Werror -pedantic -std=c99 -D_GNU_SOURCE -pthread client.c -o client` `.PHONY: all` – toastedDeli Oct 24 '17 at 12:54
  • 1
    @toastedDeli But I guess it's completely wrong. Your `all` rule typically shouldn't have a recipe. Instead, have three other rules `serverThreaded`, `server` and `client` which have a recipe that builds the respective binary. You should go one step further and have a pattern rule `%.o: %.c` building the individual object files to take full advantage of `make` ... please read some manuals/books etc first. –  Oct 24 '17 at 12:58
  • Thank you. Yes, the assignment has 2 parts; creating a single threaded server and creating a multithreaded server. I just implemented the multi t hreaded one only to save time. – toastedDeli Oct 24 '17 at 13:10
1

You can add an artificial target as the first one. By convention, this target is usually called all. Then you can list all the dependencies you actually want to build:

all: serverThreaded server client

See this discussion of the default goal.

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92