Several simple examples of makefile

Source: Internet
Author: User

Download Word: Click to open the link

 

I. Only one. c file is written in makefile.

 

 

Main. c

 

#include <stdio.h>int main(void){    printf("Makefile\n");    return 0;}

 

Makefile

CFLAGS=-WallCC=gccmain:main.o$(CC) $(CFLAGS) main.o -o mainmain.o:main.c$(CC) $(CFLAGS) -c main.c -o main.oclean:$(RM) main.o$(RM) main.PHONY:clean

 

 

The second method of makefile:

CFLAGS=-WallCC=gccmain:main.oclean:$(RM) main.o$(RM) main.PHONY:clean

 

 

 

 

Ii. makefile writing when there are two. c files

 

Main. c

#include <stdio.h>extern void fun1(void);extern void fun2(void);int main(void){    printf("main\n");    fun1();     fun2();    return 0;}

Fun 1.c

#include <stdio.h>void fun1(void){    printf("fun1\n");}fun2.c#include <stdio.h>void fun2(void){    printf("fun2\n");}

 

Makefile

CFLAGS=-WallCC=gccmain:main.o fun1.o cs2/fun2.oclean:$(RM) *.o$(RM) cs2/*.o$(RM) main.PHONY:clean

 

 

Another way to write makefile:

CFLAGS=-WallCC=gccmain:main.o fun1.o cs2/fun2.o$(CC) $(CFLAGS) $^ -o $@main.o:main.c$(CC) $(CFLAGS) -c $^ -o $@fun1.o:fun1.c$(CC) $(CFLAGS) -c $^ -o $@cs2/fun2.o:cs2/fun2.c$(CC) $(CFLAGS) -c $^ -o $@clean:$(RM) *.o$(RM) cs2/*.o$(RM) main.PHONY:clean

 

3. Writing nested makefile

The makefile in the makefiletest directory is the master makefile, which reads the master makefile using the make command, and then the master makefile reads the makefile in the subdirectories in turn.

Master makefile:

CC=gccCFLAGS=-Wall -g SUBDIR=d1 \   d2 \   d3 \   d4 \   main \   objOBJS=d1.o d2.o d3.o d4.o main.oBIN=myappOBJDIR=objBINDIR=binexport CFLAGS CC SUBDIR OBJS OBJDIR BIN BINDIRall:checkdir $(SUBDIR) checkdir:mkdir -p $(BINDIR)
// Note: $ (subdir) should be followed by a dependent file. Otherwise, make-C $ @ cannot be executed, or declare all directory names as pseudo targets.
$ (Subdir): huixianmake-C $ # Yes $ @, instead of $ (subdir) Huixian: @ echo $ (subdir) @ echo begin! Clean: @ $ (RM) $ (objdir )/*. o @ rM-RF $ (bindir ). phony: Clean note: the comments of makefile must start with # and must be written at the top.

 

Main/Main. c

#include "../d4/d4.h" extern void fun2(void);extern void fun3(void);extern void fun1(void);int main(void){    fun1();    fun2();    fun3();fun4();    return 0;}

 

Main/makefile

../$(OBJDIR)/main.o:main.c$(CC) -c $(CFLAGS) $< -o $@

 

Include/myinclude. h

#ifndef __MYINCLUDE_H__#define __MYINCLUDE_H__#include <stdio.h>#endif

 

D1/d1.c

#include "../include/myinclude.h"void fun1(void){    printf("fun1\n");    return ;}

D1/makefile

../$(OBJDIR)/d1.o:d1.c$(CC) $(CFLAGS) -c $< -o $@

 

D2/d2.c

#include "../include/myinclude.h"void fun2(void){    printf("%s\n","this is fun2");}

D2/makefile

../$(OBJDIR)/d2.o:d2.c$(CC) $(CFLAGS) -c $^ -o $@

D3/D3.c

#include "../include/myinclude.h"void fun3(void){    printf("%s\n","this is fun3");}

D3/makefile

../$(OBJDIR)/d3.o:d3.c$(CC) $(CFLAGS) -c $^ -o $@

D4/d4.h

#ifndef __D4_H__#define __D4_H__#define N 4extern void fun4(void);#endif

D4/d4.c

#include "../include/myinclude.h"#include "d4.h"void fun4(void){printf("this is fun4-->%d\n",N);}

D4/makefile

../$(OBJDIR)/d4.o:d4.c$(CC) $(CFLAGS) -c $< -o $@

Make

Enter bin/run MyApp

Directory changes after make

 

Directory changes after make clean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.