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


转载于:https://my.oschina.net/kimiz/blog/168610

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值