Linux下makefile的一个简单框架

  • 目录结构
tree
.
|-- Makefile
`-- src
    |-- Makefile
    |-- bar
    |   |-- Makefile
    |   `-- bar.c
    `-- foo
        |-- Makefile
        `-- foo.c

3 directories, 6 files
  • 顶层Makefile
# Makefile for top directory

# phony target
.PHONY: all debug release clean

#
all: release

#
debug:
        $(MAKE) -C src debug

#
release:
        $(MAKE) -C src release

#
clean:
        $(MAKE) -C src clean

#EOF
  • src目录的Makefile
# Makefile for src directory

##############################
DIRS= foo \
        bar
##############################

# phony target
.PHONY: all debug release clean

#
all: 
        set -e; for d in $(DIRS); do $(MAKE) -C $$d; done

#
debug:
        set -e; for d in $(DIRS); do $(MAKE) DEBUG=1 -C $$d; done

#
release:
        set -e; for d in $(DIRS); do $(MAKE) RELEASE=1 -C $$d; done

#
clean:
        set -e; for d in $(DIRS); do $(MAKE) -C $$d clean; done

#EOF
  • bar目录的Makefile和bar.c
# Makefile for bar directory

##############################

TARGET=bar
SRCS=$(wildcard *.c)
OBJS=$(patsubst %.c,%.o,$(SRCS))
INCS=
ifeq ($(DEBUG),1)
        CFLAGS=-Wall -DDEBUG
else
        CFLAGS=-Wall -DRELEASE
endif
LDFLAGS=
LIBS=-lm

##############################

# phony target
.PHONY: all clean

#
all:$(TARGET)
#       @echo "DEBUG="$(DEBUG)
#       @echo "RELEASE="$(RELEASE)
#       @echo "make " $(TARGET)

$(TARGET):$(OBJS)
#       @echo $(OBJS)
        $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

$(OBJS):$(SRCS)
        $(CC) $(CFLAGS) -c $< -o $@

#
clean:
        @echo "make " $(TARGET) "clean"
        rm -f $(TARGET) $(OBJS)

#EOF
#include <stdio.h>
#include <math.h>


int main(int argc, char *argv[])
{
#ifdef DEBUG
        printf("debug begin\n");
#endif
        const double pi = 3.1415926;
        printf("sin(30*pi/180) is %f\n", sin(30*pi/180));

#ifdef DEBUG
        printf("debug end\n");
#endif
        return 0;
}
  • foo目录的Makefile和foo.c
# Makefile for foo directory

##############################

TARGET=foo
SRCS=$(wildcard *.c)
OBJS=$(patsubst %.c,%.o,$(SRCS))
INCS=
ifeq ($(DEBUG),1)
        CFLAGS=-Wall -DDEBUG
else
        CFLAGS=-Wall -DRELEASE
endif
LDFLAGS=
LIBS=-lm -lpthread

##############################

# phony target
.PHONY: all clean

#
all:$(TARGET)
#       @echo "DEBUG="$(DEBUG)
#       @echo "RELEASE="$(RELEASE)
#       @echo "make " $(TARGET)

$(TARGET):$(OBJS)
#       @echo $(OBJS)
        $(CC) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)

$(OBJS):$(SRCS)
        $(CC) $(CFLAGS) -c $< -o $@

#
clean:
        @echo "make " $(TARGET) "clean"
        rm -f $(TARGET) $(OBJS)

#EOF
#include <stdio.h>
#include <math.h>

#include <unistd.h>
#include <pthread.h>

void *fun(void *arg)
{
        pthread_detach(pthread_self());
#ifdef DEBUG
        printf("pthread debug begin\n");
#endif
        const double pi = 3.1415926;
        printf("sin(30*pi/180) is %f\n", sin(30*pi/180));

#ifdef DEBUG
        printf("pthread debug end\n");
#endif

        return NULL;
}


int main(int argc, char *argv[])
{
        int ret;
        pthread_t tid;
        ret = pthread_create(&tid, NULL, fun, NULL);
        sleep(1);
        return 0;
}
  • bar目录make debug的结果
debug begin
sin(30*pi/180) is 0.500000
debug end
  • foo目录make debug的结果
pthread debug begin
sin(30*pi/180) is 0.500000
pthread debug end


你可能感兴趣的:(makefile)