aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Liodden <eriklio@stud.ntnu.no>2017-07-07 16:21:09 +0200
committerErik Liodden <eriklio@stud.ntnu.no>2017-07-07 17:14:46 +0200
commit8c614e8f472e04b439bb46d3630484891c639668 (patch)
tree6491a2a37af3b8ce00da07d1ef3e37d1fd044137
parent75fac30a9913bd50de2f7ef78633fd20e15aa059 (diff)
downloadc-project-template-8c614e8f472e04b439bb46d3630484891c639668.tar.gz
Add makefiles, folders and test files
This is the actual template, containing folders and files. Both source and make files. The whole project is built form the top directory. To build the program in src/, simply run `make` or `make prog` in the top directory. To build the test program in test/, run `make test`. Both `make prog` and `make test` will create binaries in the bin/ directory. The structure of the template is given below: . |-- .gitignore |-- Makefile |-- README |-- include/ | `-- implementation.h |-- src/ | |-- Makefile | |-- main.c | `-- implementation/ | |-- implementation.c | `-- module.mk `-- test/ |-- Makefile `-- implementation/ |-- implementation.test `-- module.mk The source files are placed in modules under the src/ directory and is organised in modules. The template contains one module: 'implementation'. Every module must contain a module.mk file that states the source files in this directory and other necessary information such as libraries required to build the module. The test files are located in the test/ directory. They follow the same module structure as in src/. Header files are placed in the include/ folder. This folder are included in all compilations. 'local' header files can be placed in the module folder. The module.mk file under the test/ directory also contains a DEP_OBJ variable, indicating required object to be linked to the test file in this module. To build with debug flags, run `make debug`. To analyse memory leaks and such, run `make valgrind` which will run the program through valgrind. Object and depend files are placed all over the place. To clean up, run `make clean`.
-rw-r--r--Makefile30
-rw-r--r--include/implementation.h6
-rw-r--r--src/Makefile31
-rw-r--r--src/implementation/implementation.c6
-rw-r--r--src/implementation/module.mk1
-rw-r--r--src/main.c9
-rw-r--r--test/Makefile31
-rw-r--r--test/implementation/implementation.test7
-rw-r--r--test/implementation/module.mk3
9 files changed, 124 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..90abba8
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,30 @@
+all: prog
+
+prog:
+ @mkdir -p bin
+ @make -C src
+ @cp src/prog ./bin
+
+debug:
+ @mkdir -p bin
+ @make -C src debug
+ @cp src/prog ./bin/prog-debug
+
+valgrind:
+ @mkdir -p bin
+ @make -C src debug
+ @cp src/prog ./bin/prog-debug
+ valgrind --leak-check=full --track-origins=yes ./bin/prog-debug
+
+test:
+ @make -C test/
+ @mkdir -p bin
+ @cp test/test ./bin
+ @./bin/test
+
+.PHONY : clean test
+
+clean :
+ make clean -C test
+ make clean -C src
+ rm -rf bin
diff --git a/include/implementation.h b/include/implementation.h
new file mode 100644
index 0000000..578bd3e
--- /dev/null
+++ b/include/implementation.h
@@ -0,0 +1,6 @@
+#ifndef _IMPLEMENTATION_H
+#define _IMPLEMENTATION_H
+
+int sum(int a, int b);
+
+#endif
diff --git a/src/Makefile b/src/Makefile
new file mode 100644
index 0000000..383e3e6
--- /dev/null
+++ b/src/Makefile
@@ -0,0 +1,31 @@
+CC=gcc-6
+OUTPUT_OPTION=-MMD -MP -o $@
+
+CFLAGS += -Wall -Ofast --std=c99 -Wvla -I../include
+MODULES := implementation
+CFLAGS += $(patsubst %,-I%,$(MODULES))
+
+LIBS :=
+SRC := main.c
+include $(patsubst %,%/module.mk,$(MODULES))
+OBJ := $(patsubst %.c,%.o,$(filter %.c,$(SRC)))
+DEP := $(patsubst %.c,%.d,$(filter %.c,$(SRC)))
+
+all : prog
+
+prog : $(OBJ)
+ $(CC) -o $@ $(OBJ) $(LIBS)
+
+debug : CFLAGS += -O0 -g
+debug : clean prog
+
+valgrind: debug
+ valgrind ./prog
+
+-include $(DEP)
+
+.PHONY: clean
+
+clean:
+ rm -f $(OBJ) $(DEP) prog
+ rm -rf *.dSYM
diff --git a/src/implementation/implementation.c b/src/implementation/implementation.c
new file mode 100644
index 0000000..098cd22
--- /dev/null
+++ b/src/implementation/implementation.c
@@ -0,0 +1,6 @@
+#include "implementation.h"
+
+int sum(int a, int b)
+{
+ return a + b;
+}
diff --git a/src/implementation/module.mk b/src/implementation/module.mk
new file mode 100644
index 0000000..a59172e
--- /dev/null
+++ b/src/implementation/module.mk
@@ -0,0 +1 @@
+SRC += implementation/implementation.c
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..70173b3
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+#include "implementation.h"
+
+int main(void)
+{
+ printf("sum: 5 + 2 = %d\n", sum(5, 2));
+ return 0;
+}
+
diff --git a/test/Makefile b/test/Makefile
new file mode 100644
index 0000000..51007f5
--- /dev/null
+++ b/test/Makefile
@@ -0,0 +1,31 @@
+CC=gcc-6
+OUTPUT_OPTION=-MMD -MP -o $@
+
+CFLAGS += -Wall -Ofast --std=c99 -Wvla -I../include
+MODULES := implementation
+CFLAGS += $(patsubst %,-I%,$(MODULES))
+
+LIBS :=
+TEST :=
+DEP_OBJ :=
+include $(patsubst %,%/module.mk,$(MODULES))
+SRC := $(patsubst %.test,%-test.c,$(filter %.test,$(TEST)))
+OBJ := $(patsubst %.c,%.o,$(filter %.c,$(SRC)))
+DEP := $(patsubst %.c,%.d,$(filter %.c,$(SRC)))
+
+all: test
+
+test: $(OBJ)
+ @make -C ../src
+ $(CC) -o test $(OBJ) $(DEP_OBJ) $(LIBS)
+
+$(SRC) : $(TEST)
+ checkmk $< > $@
+
+-include $(DEP)
+
+.PHONY: clean
+
+clean:
+ rm -f $(OBJ) $(DEP) $(SRC) test
+ rm -rf *.dSYM
diff --git a/test/implementation/implementation.test b/test/implementation/implementation.test
new file mode 100644
index 0000000..c6bee61
--- /dev/null
+++ b/test/implementation/implementation.test
@@ -0,0 +1,7 @@
+#include "implementation.h"
+
+#test sum2test
+ fail_unless(sum(3, 2) == 5, "sum function borked");
+ fail_unless(sum(-3, 2) == -1, "sum function borked");
+ fail_unless(sum(3, -2) == 1, "sum function borked");
+ fail_unless(sum(-3, -2) == -5, "sum function borked");
diff --git a/test/implementation/module.mk b/test/implementation/module.mk
new file mode 100644
index 0000000..25c64d5
--- /dev/null
+++ b/test/implementation/module.mk
@@ -0,0 +1,3 @@
+TEST += implementation/implementation.test
+DEP_OBJ += ../src/implementation/implementation.o
+LIBS += -lcheck