I have the following makefile
# See gcc/clang manual to understand all flags
CFLAGS += -std=c99 # Define which version of the C standard to use
CFLAGS += -Wall # Enable the 'all' set of warnings
CFLAGS += -Werror # Treat all warnings as error
CFLAGS += -Wshadow # Warn when shadowing variables
CFLAGS += -Wextra # Enable additional warnings
CFLAGS += -O2 -D_FORTIFY_SOURCE=2 # Add canary code, i.e. detect buffer overflows
CFLAGS += -fstack-protector-all # Add canary code to detect stack smashing
# We have no libraries to link against except libc, but we want to keep
# the symbols for debugging
LDFLAGS = -rdynamic
# external libs
# par défaut les chemins classiques
LDFLAGS += -I$(HOME)/local/include
LDFLAGS += -L$(HOME)/local/lib
# Default compiler
CC=gcc
# folders
SOURCE_FOLDER=src
TESTS_FOLDER=tests
PAQUET_FOLDER=paquet
SEND_RECEIVE_DATA_FOLDER=sendAndReceiveData
CLIENT_FOLDER=client
SERVER_FOLDER=server
# folder of sources
PAQUET_FULL_PATH=$(SOURCE_FOLDER)/$(PAQUET_FOLDER)
SEND_RECEIVE_DATA_FULL_PATH=$(SOURCE_FOLDER)/$(SEND_RECEIVE_DATA_FOLDER)
CLIENT_FULL_PATH=$(SOURCE_FOLDER)/$(CLIENT_FOLDER)
SERVER_FULL_PATH=$(SOURCE_FOLDER)/$(SERVER_FOLDER)
# sources files
# On prend tout
PACKET_SOURCES = $(wildcard $(PAQUET_FULL_PATH)/*.c)
SEND_RECEIVE_DATA_SOURCES = $(wildcard $(SEND_RECEIVE_DATA_FULL_PATH)/*.c)
CLIENT_SOURCES = $(wildcard $(CLIENT_FULL_PATH)/*.c)
SERVER_SOURCES = $(wildcard $(SERVER_FULL_PATH)/*.c)
# objects
PACKET_OBJECTS=$(PACKET_SOURCES:.c=.o)
SEND_RECEIVE_DATA_OBJECTS=$(SEND_RECEIVE_DATA_SOURCES:.c=.o)
CLIENT_OBJECTS=$(CLIENT_SOURCES:.c=.o)
SERVER_OBJECTS=$(SERVER_SOURCES:.c=.o)
# another things
# Default target
all: clean server client
client: $(CLIENT_OBJECTS) $(PACKET_OBJECTS); \
$(CC) $(CFLAGS) $(CLIENT_OBJECTS) $(LDFLAGS);
server: $(SERVER_OBJECTS) $(PACKET_OBJECTS); \
$(CC) $(CFLAGS) $(SERVER_OBJECTS) $(LDFLAGS);
$(PACKET_OBJECTS): $(PACKET_OBJECTS); \
$(CC) $(CFLAGS) -lz $(PACKET_OBJECT) $(LDFLAGS);
tests: $(PACKET_OBJECTS) $(TESTS_OBJECTS); \
$(CC) $(CFLAGS) -lcunit $(LDFLAGS);
.PHONY: clean
clean:
@rm -f *.o
I got this message :
make: Circular src/paquet/packet_implem.o <- src/paquet/packet_implem.o dependency dropped. \ gcc -std=c99 -Wall -Werror -Wshadow -Wextra -O2 -D_FORTIFY_SOURCE=2 -fstack-protector-all -lz -rdynamic -I/home/jy95/local/include -L/home/jy95/local/lib; /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function
_start': (.text+0x20): undefined reference to
main' collect2: error: ld returned 1 exit status Makefile:71: recipe for target 'src/paquet/packet_implem.o' failed make: *** [src/paquet/packet_implem.o] Error 1
What I want :
1) Build the dependancies (PACKET_SOURCES) and (SEND_RECEIVE_DATA_SOURCES)
2) Build the client and the server with the dependancies from step 1
3) The executable "client" will be at the root of the folder.
How can I correct my errors ?
g
. and if using thegdb
debugger, then instead ofg
useggdb
CC=gcc
1) use:=
rather that=
so the macro is only evaluated once rather than every time it is referenced. 2) the idea of using macros for shell functions is to specify exactly which instance of the program name to use. I.E use:CC := /usr/bin/gcc
LDFLAGS += -I$(HOME)/local/include is something you want at the end of the
gcc` command in the 'compile' recipe. NOT in thelink
recipe. Suggest:INCLUDE += -I$(HOME)/local/include
all
target, there should be the line:.PHONY : all
all: clean server client
would be better written as:all: server client