您的位置:首页 > 其它

Makefile 编写 简易教程 (实例)

2016-07-08 08:48 211 查看
一、文件准备

main.c

#include <stdlib.h>
#include "a.h"

extern void function_two();
extern void function_three();

int main()
{
function_two();
function_three();
exit (EXIT_SUCCESS);
}2.c
/* 2.c */
#include "a.h"
#include "b.h"

void function_two() {
}3.c
/* 3.c */
#include "b.h"
#include "c.h"

void function_three() {
}三个空的头文件 a.h , b.h , c.h
二、 各种编译情况

MakeFile 编译文件 MakeFile:

all: myapp

# Which compiler
CC = gcc

# Where to install
INSTDIR = /usr/local/bin

# Where are include files kept
INCLUDE = .

# Options for development
CFLAGS = -g -Wall -ansi

# Options for release
# CFLAGS = -O -Wall -ansi

myapp: main.o 2.o 3.o
$(CC) -o myapp main.o 2.o 3.o

main.o: main.c a.h
$(CC) -I$(INCLUDE) $(CFLAGS) -c main.c

2.o: 2.c a.h b.h
$(CC) -I$(INCLUDE) $(CFLAGS) -c 2.c

3.o: 3.c b.h c.h
$(CC) -I$(INCLUDE) $(CFLAGS) -c 3.c

clean:
-rm main.o 2.o 3.o

install: myapp
@if [ -d $(INSTDIR) ]; \
then \
cp myapp $(INSTDIR);\
chmod a+x $(INSTDIR)/myapp;\
chmod og-w $(INSTDIR)/myapp;\
echo "Installed in $(INSTDIR)";\
else \
echo "Sorry, $(INSTDIR) does not exist";\
fi


 2.c和3.c先编译生成mylib.a ,再和main.c 编译生成 myapp 的MakeFile:

all: myapp

# Which compiler
CC = gcc

# Where to install
INSTDIR = /usr/local/bin

# Where are include files kept
INCLUDE = .

# Options for development
CFLAGS = -g -Wall -ansi

# Options for release
# CFLAGS = -O -Wall -ansi

# Local Libraries
MYLIB = mylib.a

myapp: main.o $(MYLIB)
$(CC) -o myapp main.o $(MYLIB)

$(MYLIB): $(MYLIB)(2.o) $(MYLIB)(3.o)
main.o: main.c a.h
2.o: 2.c a.h b.h
3.o: 3.c b.h c.h

clean:
-rm main.o 2.o 3.o $(MYLIB)

install: myapp
@if [ -d $(INSTDIR) ]; \
then \
cp myapp $(INSTDIR);\
chmod a+x $(INSTDIR)/myapp;\
chmod og-w $(INSTDIR)/myapp;\
echo "Installed in $(INSTDIR)";\
else \
echo "Sorry, $(INSTDIR) does not exist";\
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: