Skip to content

Commit

Permalink
Move demos to examples folder, compile multiple targets in that folder
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 19, 2024
1 parent fd2698f commit e93c791
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
37 changes: 22 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
CC = gcc
TARGET = demo
SRC_DIR = src
INC_DIR = include
# This is where the targets will be compiled from
EXAMPLES_DIR = examples
TEST_DIR = test
CFLAGS = -g -I$(INC_DIR) -Wall
LDFLAGS = -lm
DEMO_SRC = main.c
TEST_SRC = test/test.c
SRC = $(wildcard $(SRC_DIR)/*.c) $(DEMO_SRC)
TEST_SRC = $(wildcard $(TEST_DIR)/*.c)
EXAMPLES_SRC = $(wildcard $(EXAMPLES_DIR)/*.c)
# Each demo source gets a target, e.g. examples/01_demo.c -> 01_demo
EXAMPLES = $(patsubst $(EXAMPLES_DIR)/%.c,%,$(EXAMPLES_SRC))

# Compare cmd arguments; if `test`, extend it to handle unit tests
# If `test` is passed as a cmd argument, extend flags to handle unit tests
ifeq ($(MAKECMDGOALS), test)
CFLAGS += -DRUN_UNIT_TESTS
SRC = $(wildcard $(SRC_DIR)/*.c) $(TEST_SRC)
TARGET = test/test
SRC = $(wildcard $(SRC_DIR)/*.c) $(TEST_SRC)
else
TARGET = $(EXAMPLES)
SRC = $(wildcard $(SRC_DIR)/*.c)
endif

OBJECTS = $(SRC:%.c=%.o)
RM = rm -rf

all: $(TARGET)
# What to do by default (no arguments)
all: $(EXAMPLES)

$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)
$(EXAMPLES): %: $(EXAMPLES_DIR)/%.c $(wildcard $(SRC_DIR)/*.c)
$(CC) $(CFLAGS) $(SRC_DIR)/*.c $< -o $@ $(LDFLAGS)

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

# Phony to always rerun it
.PHONY: test

test: $(TARGET)
./$(TARGET)

.PHONY: clean

RM = rm -rf
clean:
$(RM) $(TARGET)
$(RM) $(SRC_DIR)/*.o
$(RM) $(TARGET).o
$(RM) test/*.o
$(RM) $(EXAMPLES) $(SRC_DIR)/*.o test/*.o
5 changes: 4 additions & 1 deletion main.c → examples/01_particle_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#define NPARTICLES 200 // make sure it's less than the visualiser's capacity

#ifndef NPARTICLES
#define NPARTICLES 300
#endif

// assuming max > 0
#define ABS(x) (x)
Expand Down

0 comments on commit e93c791

Please sign in to comment.