Reputation: 580
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
Upvotes: 0
Views: 1437
Reputation:
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.
Upvotes: 5
Reputation: 33747
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.
Upvotes: 1