Linux的Makefile简单实例教程

50 篇文章 0 订阅
先建立以下实例程序文本:


/**************************filename:main.c**************************/


#include <stdio.h>


#include <stdlib.h>




#include "hello.h"


#include "init.h"




void aftermain(void)


{


       printf("\n");


       printf("<<<<<<<aftermain>>>>>>>>>\n");


       printf("..............\n");


       return 0;


}




int main(int argc,char *argv[])


{


       printf("========main=======\n");


       init(1234);


       hello(argc,argv);


       atexit(aftermain);


       printf(".....exit main......\n");




       return 0;


}


/**********************filename:init.c***************************/


#include <stdio.h>


#include "init.h"




const char ro_data[1024]={"This is readonly data"};


static char rw_data[1024]={"This is readwrite data"};


static char bss_data[1024];




int init(int number)


{


       printf("input number:%d\n",number);


       printf("ro_data:%x,%s\n",(unsigned int)ro_data,ro_data);


       printf("rw_data:%x,%s\n",(unsigned int)rw_data,rw_data);


       printf("bss_data:%x,%s\n",(unsigned int)bss_data,bss_data);


       return number;




}


/*******************filename:hello.c********************/


#include <stdio.h>


#include "hello.h"




int hello(int argc,char *argv[])


{


       int i;


       printf("Hello world!\n");


       for(i=0;i<argc;i++)


       {


              printf("argv[%d]=%s\n",i,argv[i]);


       }


       return 0;


}


/****************filename:init.h*************************/


#ifndef _INIT_H_


#define _INIT_H_




int init(int number);




#endif


/*********************filename:hello.c*********************/


#ifndef _HELLO_H_


#define _HELLO_H_




int hello(int argv,char *argc[]);




#endif


由依赖关系可以知道:


All: main.o hello.o init.o


Main.o: main.c hello.h init.h


Hello.o:hello.c hello.h


Init.o init.h init.c




建立终端,在终端输入


Vi makefile1


即建立makefile1的文本文件


all:   main.o hello.o init.o      


       gcc -o myapp main.o hello.o init.o




main.o:    main.c hello.h init.h


       gcc -c main.c


hello.o:    hello.c hello.h


       gcc -c hello.c


init.o:      init.c init.h


       gcc -c init.c          


注意: 以“:”结尾的后面跟的是tab制表符,而不是空格


在终端输入 make –f makefile 即编译成功


Make文件中的注释以#开头
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
############################################################################# # Makefile for building: sample 2011-09-26 # # Project: # Template: # Command: # ------基本上简单用法的makefile------- #1. 第一个目标为最终目标 #2. 命令以 Tab开头,可以有多个命令 #3. 分行号\ 后面不可以跟空格 #4、加@可以去掉命令显示 #5. 变量为 abc = efd 访问为 $(abc) echo $abc # # # #缺点,单文件夹 #每次都会重新生成 # #foo.o : foo.c defs.h # foo模块 #cc -c -g foo.c # #多目录 一种方法,在主目录里面include "",然后其里面OBJS += .o,这样其实就是 或用foreach ############################################################################# #target EXECUTABLE := test CC := gcc CXX := g++ STRIP := strip AR := ar cqs LINK := g++ RM := rm -f CFLAGS := -g -Wall CXXFLAGS := $(CFLAGS) CXXFLAGS += -MD LIBS := -lm LIBPATH := -L/usr/local/lib INCPATH := ####### Output directory OBJSPATH := ../Obj/ EXECUTABLEPATH := ../Execute/ #######source Files SOURCE := $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(SOURCE))) DEPS := $(patsubst %.o,%.d,$(OBJS)) #######rule .SUFFIXES: .cpp .c .o .so .a .d $(OBJSPATH)%.o:%.c $(CC) $(CFLAGS) -c $< -o $@ $(OBJSPATH)%.o:%.cpp $(CXX) $(CXXFLAGS) -c $< -o $@ $(OBJSPATH)%.d:%.cpp $(CXX) -MM $ $@ ######main .PHONY : all deps objs clean rebuild all: $(EXECUTABLE) $(CXX) $(CXXFLAGS) $(INCLUDEPATH) $(LIBS) $(LIBPATH) $(addprefix $(OBJSPATH),$(OBJS)) \ -o $(EXECUTABLEPATH)$(EXECUTABLE) deps: $(addprefix $(OBJSPATH),$(DEPS)) objs: $(addprefix $(OBJSPATH),$(OBJS)) clean: @$(RM) $(OBJSPATH)*.o @$(RM) $(OBJSPATH)*.d @$(RM) $(EXECUTABLEPATH)$(EXECUTABLE) rebuild: clean all -include $(addprefix $(OBJSPATH),$(DEPS)) ##.d里面是详细的.o rule 自己会括展开的,然后没有文件就自己去重建 $(EXECUTABLE) : objs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值