为什么我的Makefile每次重新编译?

时间:2018-06-07 22:29:25

标签: c makefile

我在这个Makefile上有一点问题,每当我调用make命令时,我的makefile都会重新编译。有什么问题?

CC = gcc -Wall -Werror -Wextra
    INC=includes
    INC_LIB=libft/includes

    SRC_PATH = srcs
    OBJ_PATH = srcs

    NAME = vital
    NAME_CLIENT = client
    NAME_SERVER = server

    INCLUDES = ./includes/server.h ./includes/client.h
    INCLUDES_GCH = ./includes/server.h.gch ./includes/client.h.gch

    LIB_PATH = libft

    SRC_M_SERVER_PATH       = server
    SRC_M_CLIENT_PATH       = client

    ################################################
    ### SOURCES
    ###############################################

    SRC_M_CLIENT        = client.c main_client.c manage_put_client.c manage_get_client.c create_client.c usage_builtin.c make_client.c remaster_prompt.c my_send_and_recv.c
    SRC_M_SERVER        = server.c main_server.c  manage_builtin.c ft_put.c create_server.c server_make.c ft_cd.c ft_mkdir.c ft_ls.c ft_pwd.c ft_quit.c ft_get.c network.c get_server_make.c

    SRC_M_C = $(addprefix ./$(SRC_M_CLIENT_PATH)/,         $(SRC_M_CLIENT))
    SRC_M_S = $(addprefix ./$(SRC_M_SERVER_PATH)/,         $(SRC_M_SERVER))

    ###############################################
    ### OBJECT
    ##############################################

    OBJ_M_CLIENT = $(patsubst %.c, %.o, $(SRC_M_CLIENT))
    OBJ_M_SERVER = $(patsubst %.c, %.o, $(SRC_M_SERVER))

    OBJ_M_C  = $(addprefix ./$(OBJ_PATH)/, $(OBJ_M_CLIENT))
    OBJ_M_S =  $(addprefix ./$(OBJ_PATH)/, $(OBJ_M_SERVER))

    OBJ_ALL = $(OBJ_M_C) $(OBJ_M_S)

    ####################################################
    #################### RULES ########################
    .PHONY: all libft clean fclean re

    all: $(NAME)

    $(NAME): lib $(OBJ_M_C) $(OBJ_M_S)
        $(CC) $(OBJ_M_C) $(OBJ_M_U) -L ./libft -lft -o $(NAME_CLIENT)
        $(CC) $(OBJ_M_S) $(OBJ_M_U) -L ./libft -lft -o $(NAME_SERVER)

    $(OBJ_M_C): $(OBJ_PATH)%.o : $(SRC_PATH)/$(SRC_M_CLIENT_PATH)%.c
        $(CC) $(CFLAGS) -I$(INC) -I $(INC_LIB) -c $< -o $@

    $(OBJ_M_S) : $(OBJ_PATH)%.o : $(SRC_PATH)/$(SRC_M_SERVER_PATH)%.c
        $(CC) $(CFLAGS) -I$(INC) -I $(INC_LIB) -c $< -o $@

    lib:
        @cd $(LIB_PATH) && make

    cleanlib:
        @cd $(LIB_PATH) && make clean
        @echo "$(COL_WHITE)[ CLEAN LIBFT ]\n"

    fcleanlib: cleanlib
        @cd $(LIB_PATH) && make fclean
        @echo "$(COL_WHITE)[ FCLEAN LIBFT ]\n"

    clean: cleanlib
        rm -rf $(OBJ_ALL) $(OBJ_M_M) $(INCLUDES_GCH)

    fclean: fcleanlib clean
        rm -rf $(NAME_CLIENT) $(NAME_SERVER)

    re : fclean $(NAME)

当我使用make命令时,它运行$(NAME)并启动gcc。 我有这个结果:

make[1]: Nothing to be done for `all'. (ma lib)
gcc -Wall -Werror -Wextra ./srcs/client.o ./srcs/main_client.o ./srcs/manage_put_client.o ./srcs/manage_get_client.o ./srcs/create_client.o ./srcs/usage_builtin.o ./srcs/make_client.o ./srcs/remaster_prompt.o ./srcs/my_send_and_recv.o  -L ./libft -lft -o client
gcc -Wall -Werror -Wextra ./srcs/server.o ./srcs/main_server.o ./srcs/manage_builtin.o ./srcs/ft_put.o ./srcs/create_server.o ./srcs/server_make.o ./srcs/ft_cd.o ./srcs/ft_mkdir.o ./srcs/ft_ls.o ./srcs/ft_pwd.o ./srcs/ft_quit.o ./srcs/ft_get.o ./srcs/network.o ./srcs/get_server_make.o  -L ./libft -lft -o server

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

更改此行:

.PHONY: all libft clean fclean re

.PHONY: clean fclean re

.PHONY禁用文件关联检查

答案 1 :(得分:0)

你还没有“明确”所有细节;但是,以下Makefile应该让您朝着正确的方向前进。

注意:用标签

替换所有<tab>

注意:使用:=,因此宏只评估一次

CC       := /usr/bin/gcc -Wall -Werror -Wextra
RM       := /bin/rm -rf
MAKE     := /usr/bin/make

INC      := includes
INC_LIB  := libft/includes

SRC_PATH := srcs
OBJ_PATH := srcs


NAME_CLIENT := client
NAME_SERVER := server

SERVER_INCLUDES := includes/server.h
CLIENT_INCLUDES := includes/client.h


LIB_PATH := libft

SRC_M_SERVER_PATH  := server
SRC_M_CLIENT_PATH  := client

################################################
### SOURCES
###############################################

SRC_M_CLIENT := client.c \
                main_client.c \
                manage_put_client.c \
                manage_get_client.c \
                create_client.c \
                usage_builtin.c \
                make_client.c \
                remaster_prompt.c \
                my_send_and_recv.c

SRC_M_SERVER := server.c \
                main_server.c  \
                manage_builtin.c \
                ft_put.c \
                create_server.c \
                server_make.c \
                ft_cd.c \
                ft_mkdir.c \
                ft_ls.c \
                ft_pwd.c \
                ft_quit.c \
                ft_get.c \
                network.c \
                get_server_make.c

SRC_M_C := $(addprefix ./$(SRC_M_CLIENT_PATH)/,         $(SRC_M_CLIENT))
SRC_M_S := $(addprefix ./$(SRC_M_SERVER_PATH)/,         $(SRC_M_SERVER))

###############################################
### OBJECT
##############################################

OBJ_M_CLIENT := $(patsubst %.c, %.o, $(SRC_M_CLIENT))
OBJ_M_SERVER := $(patsubst %.c, %.o, $(SRC_M_SERVER))

OBJ_M_C  :=  $(addprefix ./$(OBJ_PATH)/, $(OBJ_M_CLIENT))
OBJ_M_S  :=  $(addprefix ./$(OBJ_PATH)/, $(OBJ_M_SERVER))


####################################################
#################### RULES ########################
.PHONY: all  clean libclean

all: $(NAME_CLIENT) $(NAME_SERVER)

# link client
$(NAME_CLIENT) : $(OBJ_M_C)
<tab>$(CC) $^ -o $@ -L$(LIB_PATH) -lft 

# link server
$(NAME_SERVER) : $(OBJ_M_S)
<tab>$(CC) $^ -o $@ -L$(LIB_PATH) -lft 

# compile sources
%.o : %.c $(INC_CLIENT) $(INC_SERVER) $(INC)
<tab>$(CC) $(CFLAGS) -c $< -o $@ -I$(INC_CLIENT) $(INC_SERVER) -I$(INC)


# is there another Makefile in the $(LIB_PATH) directory?
cleanlib: clean
<tab>(@cd $(LIB_PATH) && $(MAKE) clean)
<tab>@echo "$(COL_WHITE)[ CLEAN LIBFT ]\n"

clean:
<tab>$(RM)  $(OBJ_M_C) $(OBJ_M_S)