Welcome 微信登录

首页 / 操作系统 / Linux / Linux的Makefile简单实例教程

(测试版本:RedHat 9.0简体中文版)先建立以下实例程序文本:/**************************filename:main.c**************************/#include <stdio.h>#include <stdlib.h>
#include "hello.h"#include "init.h"
void aftermain(void){       printf(" ");       printf("<<<<<<<aftermain>>>>>>>>> ");       printf(".............. ");       return 0;}
int main(int argc,char *argv[]){       printf("========main======= ");       init(1234);       hello(argc,argv);       atexit(aftermain);       printf(".....exit main...... ");
       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 ",number);       printf("ro_data:%x,%s ",(unsigned int)ro_data,ro_data);       printf("rw_data:%x,%s ",(unsigned int)rw_data,rw_data);       printf("bss_data:%x,%s ",(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! ");       for(i=0;i<argc;i++)       {              printf("argv[%d]=%s ",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.oMain.o: main.c hello.h init.hHello.o:hello.c hello.hInit.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.chello.o:    hello.c hello.h        gcc -c hello.cinit.o:      init.c init.h       gcc -c init.c           注意: 以“:”结尾的后面跟的是tab制表符,而不是空格在终端输入 make –f makefile 即编译成功Make文件中的注释以#开头