Home > Back-end >  Why isn't the Static Pattern Rule working in my makefile
Why isn't the Static Pattern Rule working in my makefile

Time:07-08

I'm new to Makefiles, so far I've only been doing little projects, but I just did my biggest project with 20 *.c files and put them all in a src file.

With my Makefile I wanted to create an obj file with all the corresponding *.o files and then make them all into an executable. I heard about Static Patterns and I tried to use it on my makefile but I'm probably making a very silly mistake as the output is not what I expected.

My Makefile:

CC=gcc

CFLAGS=-Wall -Wextra -Werror

MLX=mlx/libmlx_Darwin.a

LIBFT=libft/libft.a

I_LIBFT=-Ilibft -Llibft -lft

I_LIBMLX=-Imlx -Lmlx -lmlx

LIB=$(I_LIBFT) $(I_LIBMLX)

COMPATIBILITY=-lX11 -lXext

FRAMEWORK=-framework OpenGL -framework AppKit

HDR=/include/fdf.h

SRC_DIR=/src/

SRCS:=$(wildcard $(SRC_DIR)*.c)

OBJ_DIR=/obj/

OBJS=$(patsubst %.c, $(OBJ_DIR)%.o, $(SRCS))

all: $(LIBFT) app

$(LIBFT):
    make -C libft
    @echo "done libft"

$(OBJ_DIR):
    mkdir $@
 
$(OBJS): $(OBJ_DIR)%.o: $(SRC_DIR)%.c $(OBJ_DIR)
    $(CC) $(CFLAGS) -c $< -o $@ 

app: $(OBJS)
    @echo "making app"
    $(CC) $(CFLAGS) $(LIB) $(FRAMEWORK) $(OBJS) -o FdF

clean:
    make -C libft $@

fclean: clean
    make -C libft $@

re: fclean all

.PHONY: all clean fclean re so

My output:

making app
gcc -Wall -Wextra -Werror -Ilibft -Llibft -lft -Imlx -Lmlx -lmlx -framework OpenGL -framework AppKit  -o FdF
Undefined symbols for architecture arm64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [app] Error 1

when I expected something like:

gcc -Wall -Wextra -Werror -c file.c -o file.o
gcc -Wall -Wextra -Werror -c file2.c -o file2.o
gcc -Wall -Wextra -Werror -c file3.c -o file3.o
...
gcc -Wall -Wextra -Werror -Ilibft -Llibft -lft -Imlx -Lmlx -lmlx -framework OpenGL -framework AppKit file.o file2.o ... file19.o -o FdF

What am I doing wrong?

CodePudding user response:

I'm pretty sure these are wrong:

HDR=/include/fdf.h

SRC_DIR=/src/

OBJ_DIR=/obj/

Are your source and header files REALLY in a subdirectory of the root of your partition? If you run ls /src/ does it actually show any files?

I suspect that there is nothing there, which means that SRCS is empty because the wildcard expands to no matches, which means OBJS is empty since it's substituting into nothing, which means there are no prerequisites to the app target.

  • Related