1

I have a project for school. In the root dir, I have 3 subdirs which all have a makefile (because there will be 3 executable files), and there is 1 lib dir. This dir contains my library source files (*.c, *.h).

I have a makefile in the libdir:

##                                                                                                                                                                                                                  
## Makefile for corewar in /home/rihan_a/Projets/psu/corewar/lib                                                                                                                                                    
##                                                                                                                                                                                                                  
## Made by adnan rihan                                                                                                                                                                                              
## Login   <rihan_a@epitech.net>                                                                                                                                                                                    
##                                                                                                                                                                                                                  
## Started on  Fri Dec  9 09:28:07 2011 adnan rihan                                                                                                                                                                 
## Last update Fri Dec  9 10:45:27 2011 adnan rihan                                                                                                                                                                 
##                                                                                                                                                                                                                  

NAME=           libwar

SRC=            explode.c               \
                get.c                   \
                has_char.c              \
                int_to_bigendian.c      \
                my_die.c                \
                my_getnbr_t.c           \
                my_memset.c             \
                my_power_rec.c          \
                my_putstr.c             \
                my_strcat.c             \
                my_strcmp.c             \
                my_strlen.c             \
                my_strncpy.c            \
                my_strndup.c            \
                op.c                    \
                wt_len.c                \
                xmalloc.c

OBJ=            $(SRC:.c=.o)

ARFLAGS=        rcs
CFLAGS=         -Wall

all:            pack

obj_dbg:
                $(CC) -g -c $(SRC)

debug:          obj_dbg all

pack:           $(OBJ)
                $(AR) $(ARFLAGS) $(NAME).a $(OBJ)

clean:
                $(RM) $(OBJ)

fclean:         clean
                $(RM) $(NAME).a

My problem is when I do make, everything is ok, and for make debug also. But when I do make debug a second time, it doesn't remake the .o, but does the ar rcs everytime. I want to avoid it, because when I request make -C ../lib for the other makefiles, they all relink because the lib changed (because ar was run each time).

3

You have no check on the library. One way would be to replace

pack:           $(OBJ)
                $(AR) $(ARFLAGS) $(NAME).a $(OBJ)

by

pack: $(NAME).a

$(NAME).a: $(OBJ)
        $(AR) $(ARFLAGS) $(NAME.a) $(OBJ)

so the library would not be remade if it is up to date.

  • Ah great thanks. So, I HAVE to create a rule with a filename ? Then the filename will be check for changes ...? I didn't know :/ – Max13 Dec 9 '11 at 12:33
  • 1
    @Max13, The principle of make is that it rebuilds files which are not uptodate with respects to their dependencies. I.E. all rules are file names. (Some make variant have a way to indicates that a rule hasn't the purpose to build a file, see .PHONY in GNUmake for instance, but then they are always executed) – AProgrammer Dec 9 '11 at 14:44
0
debug:          obj_dbg all

Do you need all again there? or just all?

  • I don't need all "again", I just need all. "make debug" will make "all", which is "$(NAME)" – Max13 Dec 9 '11 at 12:29
  • you right0 there. – Nikhil Mulley Dec 9 '11 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.