Makefile 基本语法

- src/
    - hellofunc.c
    - hellomake.c
    - makefile
    - obj/
- include/
    - hellomake.h
    // hellomake.h

    void myPrintHelloMake(void);

    // hellomake.c
    
    #include <hellomake.h>

    int main(int argc, char** argv)
    {
        myPrintHelloMake();
        return 0;
    }
    
    // hellofunc.c
    
    #include<stdio.h>
    #include<hellomake.h>
    
    void myPrintHelloMake(void)
    {
        printf("Hello makefiles!\n");
        return ;
    }
# makefile

IDIR =../include
CC=gcc
CFLAGS=-I$(IDIR)

ODIR=obj
LDIR =../lib

LIBS=-lm

_DEPS = hellomake.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = hellomake.o hellofunc.o 
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))

$(ODIR)/%.o: %.c $(DEPS)
    $(CC) -c -o $@ $< $(CFLAGS)

hellomake: $(OBJ)
	gcc -o $@ $^ $(CFLAGS) $(LIBS)

.PHONY: clean

clean:
	rm -f $(ODIR)/*.o *~ core $(INCDIR)/*~ 

From: http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/

posted @ 2016-08-11 22:33  >o<  阅读(655)  评论(0编辑  收藏  举报