Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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                \

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

ARFLAGS=        rcs
CFLAGS=         -Wall

all:            pack

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

debug:          obj_dbg all

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

                $(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).

share|improve this question
up vote 3 down vote accepted

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

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


pack: $(NAME).a

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

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

share|improve this answer
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
@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
Ok thanks, I'll look at that :) – Max13 Dec 9 '11 at 17:32
debug:          obj_dbg all

Do you need all again there? or just all?

share|improve this answer
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 posting your answer, you agree to the privacy policy and terms of service.

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