hello.c
#include<stdio.h>
#include"function.h"
int main()
{
fun1();
fun2();
fun3();
return 0;
}
function.c
#include<stdio.h>
int fun1()
{
printf("This is first function!\n");
return 0;
}
int fun2()
{
printf("This is second function!\n");
return 0;
}
int fun3()
{
printf("This is third function!\n");
return 0;
}
function.h
#ifndef _FUN_H
#define _FUN_H
int fun1(void);
int fun2(void);
int fun3(void);
#endif
一般写法
gcc -o hello hello.c function.c -I.
makefile的简单写法(建立一个名为Makefile.txt的文件)
hello: hello.c function.c
gcc -o hello hello.c function.c -I.
执行Makefile
make -f Makefile.txt
用变量
TARGET = hello
CC = gcc
CFLAGS = -I.
$(TARGET): hello.c function.c
gcc -o $(TARGET) hello.c function.c $(CFLAGS)
头文件影响,生成目标文件
这一段会生成中间文件
TARGET = hello
CC = gcc
CFLAGS = -I.
DEPS = function.h
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
$(TARGET): hello.c function.c
$(CC) -o $@ $^ $(CFLAGS)
需要清除目标文件
clean:
rm -f $(TARGET) *.o
clean不起作用,真是奇怪了(起作用,make -f Makefile.txt clean)输入的姿势不正确
TARGET = hello
IDIR = ../include
CC = gcc
CFLAGS = -I$(IDIR)
ODIR = obj
LDIR = ../lib
LIBS = -lm
_DEPS = function.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
_OBJ = hello.o function.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
$(ODIR)/%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
$(TARGET): $(OBJ)
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
.PHONY: clean
clean:
rm -f $(TARGET) $(ODIR)/*.o *~ $(IDIR)/*~
以上是最强版本,7月多的草稿,拖到现在,我已经不知道写的啥了,哈哈哈