Skip to content

Instantly share code, notes, and snippets.

@roma-guru
Created November 26, 2015 20:27
Show Gist options
  • Save roma-guru/d190429242dfafa65b58 to your computer and use it in GitHub Desktop.
Save roma-guru/d190429242dfafa65b58 to your computer and use it in GitHub Desktop.
Makefile examples
SRC=hello.c main.c
OBJ=$(SRC:.c=.o) # replaces the .c from SRC with .o
EXE=hello
CC=gcc
CFLAGS=-Wall -O3
LDFLAGS=
RM=rm
%.o: %.c # combined w/ next line will compile recently changed .c files
$(CC) $(CFLAGS) -o $@ -c $<
.PHONY : all # .PHONY ignores files named all
all: $(EXE) # all is dependent on $(EXE) to be complete
$(EXE): $(OBJ) # $(EXE) is dependent on all of the files in $(OBJ) to exist
$(CC) $(OBJ) $(LDFLAGS) -o $@
.PHONY : clean # .PHONY ignores files named clean
clean:
-$(RM) $(OBJ) core # '-' causes errors not to exit the process
SRC=hello.c main.c
OBJ=$(SRC:.c=.o) # replaces the .c from SRC with .o
EXE=hello.exe
CC=gcc
CFLAGS=-Wall -O3
LDFLAGS=-mwindows
RM=rm
%.o: %.c # combined w/ next line will compile recently changed .c files
$(CC) $(CFLAGS) -o $@ -c $<
.PHONY : all # .PHONY ignores files named all
all: $(EXE) # all is dependent on $(EXE) to be complete
$(EXE): $(OBJ) # $(EXE) is dependent on all of the files in $(OBJ) to exist
$(CC) $(OBJ) $(LDFLAGS) -o $@
.PHONY : clean # .PHONY ignores files named clean
clean:
-$(RM) $(OBJ) core # '-' causes errors not to exit the process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment