diff --git a/libadd/add.c b/libadd/add.c new file mode 100644 index 0000000..1f76448 --- /dev/null +++ b/libadd/add.c @@ -0,0 +1 @@ +int add(int a, int b) { return a + b; } diff --git a/libadd/include/add.h b/libadd/include/add.h new file mode 100644 index 0000000..465d504 --- /dev/null +++ b/libadd/include/add.h @@ -0,0 +1,6 @@ +#ifndef ADD_H +#define ADD_H + +int add(int a, int b); + +#endif // ADD_H \ No newline at end of file diff --git a/libadd/makefile b/libadd/makefile new file mode 100644 index 0000000..58c02a9 --- /dev/null +++ b/libadd/makefile @@ -0,0 +1,34 @@ +# Makefile for the add library +# Builds a static library from the add.c file + +# Library name +LIB = libadd.a + +# Compiler +CC = gcc + +# Compiler flags +CFLAGS = -Wall -Werror -Wextra -pedantic +CFLAGS += -std=c99 -O3 + +# Source file +SRC = add.c + +# Object file +OBJ = $(SRC:.c=.o) + +# Build the library +$(LIB): $(OBJ) + ar rcs $(LIB) $(OBJ) + +# Build the object file +$(OBJ): $(SRC) + $(CC) $(CFLAGS) -c $(SRC) + +format: + clang-format -i *.c include/*.h + +# Clean the object files +clean: + rm -f $(OBJ) + rm -f $(LIB) diff --git a/makefile b/makefile index 419b675..cee5329 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -# Compiler and assembler +#dd Compiler and assembler CC = gcc AS = as LD = ld @@ -20,6 +20,7 @@ CFLAGS += -Wno-unused-local-typedefs CFLAGS += -Wno-unused-const-variable CFLAGS += -Wno-unused-macros CFLAGS += -O3 +CFLAGS += -Ilibadd/include CFLAGS += -g GITHASH = $(shell git rev-parse --short HEAD) @@ -36,6 +37,9 @@ OBJS := $(patsubst src/%.c,build/%.o,$(SRCS)) all: $(TARGET) +libadd/libadd.a: + $(MAKE) -C libadd + # For convenience run: $(TARGET) @./$(TARGET) @@ -45,7 +49,7 @@ mkbuilddir: @mkdir -p build # Link the object files into the target binary -$(TARGET): $(OBJS) +$(TARGET): $(OBJS) libadd/libadd.a @$(CC) $(CFLAGS) -o $@ $^ @echo -e "LD \t$^" @@ -67,6 +71,7 @@ clean: rm -r build rm -f build/*.o rm -f $(TARGET)* + $(MAKE) -C libadd clean --no-print-directory # Create a signed release tar: $(TARGET_TAR) @@ -81,4 +86,4 @@ $(TARGET_TAR): $(TARGET) strip $< tar --zstd -cvf $@ $< -.PHONY: all clean size mkbuilddir run tar sign \ No newline at end of file +.PHONY: all clean size mkbuilddir run tar sign diff --git a/src/main.c b/src/main.c index d0c89ee..459570f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,8 @@ +#include #include int main(int argc, const char **argv) { printf("Hello, World!\n"); + printf("add(1, 2) = %d\n", add(1, 2)); return 0; }