Skip to content

Instantly share code, notes, and snippets.

@snim2
Last active April 23, 2022 05:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save snim2/1419118 to your computer and use it in GitHub Desktop.
Save snim2/1419118 to your computer and use it in GitHub Desktop.
Makefile for multiple single file C programs
#
# Minimal Makefile which compiles multiple C files into individual executables.
#
#
# - Sarah Mount, November 2011
#
CC=gcc
RM=rm
CFLAGS=-c -Wall -O3
LDFLAGS=-lm
SOURCES=$(wildcard *.c)
OBJECTS=$(SOURCES:.c=.o)
EXECS=$(SOURCES:%.c=%)
.PHONY: all
all: $(OBJECTS) $(EXECS)
.c.o:
$(CC) $(CFLAGS) $< -o $@
.o.:
$(CC) $^ $(LDFLAGS) -o $@
.PHONY: clean
clean:
-@ $(RM) *.o
-@ $(RM) $(EXECS)
@ahpohl
Copy link

ahpohl commented Nov 20, 2019

Great, this is exactly what I was looking for!

@jv813yh
Copy link

jv813yh commented Mar 5, 2021

I'm trying to compile this project, but it's more complicated and I'm still getting better at writing makefiles. Do you think your makefile will help me compile it?

https://github.com/assaabloy-ppi/salt-channel-c/tree/master/examples

@snim2
Copy link
Author

snim2 commented Mar 5, 2021

This Makefile is designed for situations where you have a number of .c files and each of them needs to be compiled into a single executable, without any .h files. It doesn't look like your code fits that model, so you would need to make some changes first.

@jv813yh
Copy link

jv813yh commented Mar 5, 2021

Oh, I understand. So nothing else to embarrass with it. Well thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment