feat: Makefile and test program

This commit is contained in:
gbrochar 2025-07-03 17:38:58 +02:00
parent 6dbb5cf872
commit e8e913f3a1
5 changed files with 76 additions and 0 deletions

48
Makefile Normal file
View File

@ -0,0 +1,48 @@
# ---------------------------------------------------------------------------- #
# VARIABLES #
# ---------------------------------------------------------------------------- #
NAME_BASE := libft_malloc
HOSTTYPE ?= $(shell uname -m)_$(shell uname -s)
NAME := $(NAME_BASE)_$(HOSTTYPE).so
SYMLINK := $(NAME_BASE).so
CC := cc
CFLAGS := -Wall -Wextra -Werror -fPIC
LDFLAGS := -shared
SRC_DIR := src
INC_DIR := inc
OBJ_DIR := obj
SRCS := $(wildcard $(SRC_DIR)/*.c)
OBJS := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
INCLUDES := -I$(INC_DIR)
# ---------------------------------------------------------------------------- #
# RULES #
# ---------------------------------------------------------------------------- #
all: $(NAME) symlink
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
$(NAME): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^
symlink: $(NAME)
@ln -sf $(NAME) $(SYMLINK)
clean:
@rm -rf $(OBJ_DIR)
fclean: clean
@rm -f $(NAME) $(SYMLINK)
re: fclean all
.PHONY: all clean fclean re symlink

11
inc/malloc.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef MALLOC_H
# define MALLOC_H
#include <stdlib.h>
#include <unistd.h>
#define PROUT "42\n"
void *malloc(size_t size);
#endif

6
main.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdlib.h>
int main(void) {
malloc(42);
return 0;
}

7
src/malloc.c Normal file
View File

@ -0,0 +1,7 @@
#include "malloc.h"
void * malloc(size_t size) {
(void)size;
write(1, PROUT, 3);
return 0;
}

4
test.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
make
gcc main.c -o test_malloc.out
LD_PRELOAD=./libft_malloc.so ./test_malloc.out