Makefile 文件格式
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 文件格式的更多相关文章
- Makefile 文件格式;makefile伪目标
Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o code.o gcc -o test prog.o code.o 当中 ...
- C++学习笔记24:makefile文件
makefile make命令:负责c/c++程序编译与链接 make根据指定命令进行建构 建构规则文件:GNUmakefile , makefile,Makefile makefile 文件格式 m ...
- makefile介绍1.0
1.gcc参数 -o指定生成文件名 -c只编译不链接 2.makefile标准格式 CC=gcc #编译器变量,#代表注释 SRCS=main.cpp\#源文件变量 a.cpp\ b.cpp\ c.c ...
- Shell脚本——make命令和Makefile文件【转】
https://blog.csdn.net/twc829/article/details/72729799 make命令是一个常用的编译命令,尤其在C/C++开发中,make命令通过makefile文 ...
- 使用make
5.11 库的使用 代码的复用是计算机程序设计语言中的一个重要的概念.可以把编译好的目标文件模块统一放到一个库中,使得程序员可以在不同的程序中共享这些代码. 在Linux操作系统下,最后连接生成可执行 ...
- 别人的Linux私房菜(22)软件安装:源代码与Tarball
执行make,会在当前目录查找makefile文本文件(记录了源代码如何编译的详细信息). 内核相关的函数信息放置在/usr/lib./usr/lib64里. 在Tarball(一般为xxx.tar. ...
- linux-2.6.22.6内核启动分析之配置
配置过程最终结果是生成.config文件,我们想要对配置的目的有很清楚的了解,必须先对.config文件进行分析.通过cd命令切换到linux-2.6.22.6内核目录,输入vi .config 可以 ...
- 第22章 软件安装:源码与Tarball
开放源码的软件安装与升级简介 什么是开放源码.编译程序与可执行文件 开放源码:程序代码,写给人类看的程序语言 编译程序:将源码编译成机器能看得懂的语言 可执行文件:经过编译变成二进制程序后机器看得懂可 ...
- 教会你如何编写makefile文件
最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...
随机推荐
- caioj 1071 动态规划入门(二维一边推4:相似基因) (最长公共子序列拓展)
复制上一题总结 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽 (1) 字符串下标从1开始,因为0用来表示字符为空的情况,而不是第一个字符 (2) ...
- CF546E Soldier and Traveling(网络流,最大流)
CF546E Soldier and Traveling 题目描述 In the country there are \(n\) cities and \(m\) bidirectional road ...
- 【codeforces 821E】Okabe and El Psy Kongroo
[题目链接]:http://codeforces.com/problemset/problem/821/E [题意] 一开始位于(0,0)的位置; 然后你每次可以往右上,右,右下3走一步; (x+1, ...
- Rsyslog比较详细的
http://blog.csdn.net/fishmai/article/details/51842340
- 换主页轮播的主题图片(4、删除)---轻开电子商务系统(企业入门级B2C站点)
接( 换主页轮播的主题图片1 ) 文件:site/links/img0.html中的表单(第11行最后一个td) <td><if x="@{sys:canDo}" ...
- 25.Detours劫持技术
Detours可以用来实现劫持,他是微软亚洲研究院开发出来的工具,要实现它首先需要安装Detours. 安装地址链接:https://pan.baidu.com/s/1eTolVZs 密码:uy8x ...
- java引用被设置为null的疑惑
a=null; public class C { protected A webDigester = new A(" first one "); public void test( ...
- C++中的namespace详解
原文链接:http://blog.csdn.net/yao_zhuang/article/details/1853625 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的 ...
- Codefroces B. Hamming Distance Sum
Genos needs your help. He was asked to solve the following programming problem by Saitama: The lengt ...
- 打印机共享 : 客户端 连接服务器打印机时提示"无法连接到打印机“
1.就是重启一下服务器端的Print Spooler服务就行了,这么简单! 2.修改打印机的共享名 操作无法完成(错误0x00000709).再次检查打印机名称,并确保打印机已连接到网络.(xp系统本 ...