Makefile包含 目标文件、依赖文件、可运行命令三部分。

每部分的基本格式例如以下:

test: prog.o  code.o

gcc  -o  test   prog.o   code.o

当中,第一行的test是目标文件。 prog.o、code.o是依赖文件

第二行的gcc -o test prog.o code.o是可运行命令

整个Makefile文件都是这样的格式。

下面是一些example:

-----------------------Makefile example 1----------------------------------

#this line is the comment for the Makefile

test: prog.o code.o

gcc   prog.o  code.o  -o test

prog.o: prog.c  prog.h  code.h

gcc  -c  prog.c  -o  prog.o

code.o: code.c  code.h

gcc  -c  code.c  -o  code.o

clean:

rm  -f  *.o

----------------------------------------------------------------------------------------------

-------------------------------example 2(包括变量)---------------------------------

#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

CFLAGS = -Wall -g -O

test: ${OBJS}

${CC}  ${CFLAGS}  ${OBJS}  -o test

prog.o: prog.c  prog.h  code.h

${CC}  ${CFLAGS}  -c prog.c  -o prog.o

code.o: code.c  code.h

${CC}   ${CFLAGS}  -c code.c  -o code.o

clean:

rm  -f  *.o

------------------------------------------------------------------------------------------------


-------------------------------example 3(使用Makefile的隐含规则)---------------------------------

1,假设没有对应的编译命令,则使用隐含规则,全部的 ".c文件" 编译成与它名称同样的 ".o文件"。

2, 使用Makefile的自己主动变量。

#this line is the Makefile comment

OBJS = prog.o  code.o

CC = gcc

test: ${OBJS}

${CC}   -o  $@   $^

prog.o: prog.c  prog.h  code.h             #no exec command,and will generate the prog.o

code.o: code.c  code.h                        #no exec command,and will generate the code.o

clean:

rm  -f  *.o

------------------------------------------------------------------------------------------------

-----------------------------------------------------下面为我測试过的实例文件内容 :-----------------------------------------------------

========================== Makefile ===============

#this line is the comment

CC = gcc

OBJS = my_str.o

CFLAGS = -Wall -g -O





program: my_main.c ${OBJS}

               ${CC} ${CFLAGS} $^ -o $@ 

my_str.o: my_str.c my_str.h

               ${CC} ${CFLAGS} -c my_str.c -o my_str.o





clean:

               rm -f *.o

=======================================================================

==========================my_main.c=======================

#include <unistd.h>

#include <stdlib.h>

#include "my_str.h"



int 

main(int argc, const char **argv)

{

if(my_cmp(argv[1], argv[2]) == 0)

write(1, "Equal !\n", sizeof("Equal !\n"));

else

write(1, "Not Equal !\n", sizeof("Not Equal !\n"));





exit(0);

}

===============================================================

========================my_str.c=============================

#include "my_str.h"





int 

my_cmp(const char *str1, const char *str2)

{

if(!str1 || !str2)

return -1;

while(*str1 && *str2 && *str1 == *str2)

str1++, str2++;

return *str1 - *str2;

}

==================================================================

===================my_str.h============================================

#ifndef _MY_STR_H

#define _MY_STR_H





int my_cmp(const char *str1, const char *str2);





#endif

==================================================================

Makefile 文件格式的更多相关文章

  1. Makefile 文件格式;makefile伪目标

    Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o  code.o gcc  -o  test   prog.o   code.o 当中 ...

  2. C++学习笔记24:makefile文件

    makefile make命令:负责c/c++程序编译与链接 make根据指定命令进行建构 建构规则文件:GNUmakefile , makefile,Makefile makefile 文件格式 m ...

  3. makefile介绍1.0

    1.gcc参数 -o指定生成文件名 -c只编译不链接 2.makefile标准格式 CC=gcc #编译器变量,#代表注释 SRCS=main.cpp\#源文件变量 a.cpp\ b.cpp\ c.c ...

  4. Shell脚本——make命令和Makefile文件【转】

    https://blog.csdn.net/twc829/article/details/72729799 make命令是一个常用的编译命令,尤其在C/C++开发中,make命令通过makefile文 ...

  5. 使用make

    5.11 库的使用 代码的复用是计算机程序设计语言中的一个重要的概念.可以把编译好的目标文件模块统一放到一个库中,使得程序员可以在不同的程序中共享这些代码. 在Linux操作系统下,最后连接生成可执行 ...

  6. 别人的Linux私房菜(22)软件安装:源代码与Tarball

    执行make,会在当前目录查找makefile文本文件(记录了源代码如何编译的详细信息). 内核相关的函数信息放置在/usr/lib./usr/lib64里. 在Tarball(一般为xxx.tar. ...

  7. linux-2.6.22.6内核启动分析之配置

    配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...

  8. 第22章 软件安装:源码与Tarball

    开放源码的软件安装与升级简介 什么是开放源码.编译程序与可执行文件 开放源码:程序代码,写给人类看的程序语言 编译程序:将源码编译成机器能看得懂的语言 可执行文件:经过编译变成二进制程序后机器看得懂可 ...

  9. 教会你如何编写makefile文件

    最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...

随机推荐

  1. mysql更改密码与远程管理

    set password = ': #在当前用户下更改密码 grant all privileges on *.* to root@"%" identified by " ...

  2. Maven学习总结(21)——Maven常用的几个核心概念

    在使用Maven的过程中,经常会遇到几个核心的概念,准确的理解这些概念将会有莫大的帮助. 1. POM(Project Object Model)项目对象模型 POM 与 Java 代码实现了解耦,当 ...

  3. qt 闰年

    bool QDate::isLeapYear ( int year ) [static]

  4. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  5. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. Codeforces Round #313 C. Gerald&#39;s Hexagon(放三角形)

    C. Gerald's Hexagon time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. iOS 一个ViewController上显示2个tableView的方法

    1.在StoryBoard上创建2个tableView,并用autolayout约束. 2.在ViewController上拖进来. @property (weak, nonatomic) IBOut ...

  8. nyoj--38--布线问题(克鲁斯卡尔)

    布线问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件: 1.把所有的 ...

  9. 5. webservice通信调用天气预报接口实例

    转自:https://blog.csdn.net/xiejuan6105/article/details/78452605 一:环境搭建 1:新建一个java project工程weatherInf ...

  10. dubbo问题求解

    各位大牛好,小弟公司开发中遇到一个很奇怪的问题望有大神指教一下,实在是已经搞了3天了一点头绪没有,公司使用的是eclipse+maven+zookeper+dubbo主要是dubbo的问题,刚开始使用 ...