根据给定文件编写Makefile文件 两种方法编译
实例一
1、分析源文件代码依赖关系

mian.c
#include "test1.h"
#include "test2.h"
#include <stdio.h>
int main()
{
test1_func("hello test1!");
test2_func("hello test2!");
return ;
}
这里可以看出 main.o 文件的生成需要 main.c , test1.h , test2.h
test1.h
#ifndef _TEST_1_H
#define _TEST_1_H
void test1_func(char *str);
#endif
test1.h 里面有 test1.c 里面函数的声明
test2.h
fndef _TEST_2_H
#define _TEST_2_H
void test2_func(char *str);
#endif
test2.h 里面有 test2.c 里面函数的声明
test1.c
#include "test1.h"
#include <stdio.h> void test1_func(char *str)
{
printf("This is test1 : %s ",str);
}
生成 test1.o 文件,需要 test1.c , test1.h
test2.c
#include "test2.h"
#include <stdio.h> void test2_func(char *str)
{
printf("This is test2 : %s",str);
}
生成 test2.o 文件,需要 test2.c , test2.h
2、直接编写Makefile文件
[root@localhost MakefileExample]# vim Makefile
#Makefile
main:main.o test1.o test2.o
gcc -o main main.o test1.o test2.o
main.o:main.c test1.h test2.h
gcc -c main.c
test1.o:test1.c test1.h
gcc -c test1.c
test2.o:test2.c test2.h
gcc -c test2.c
.PHONY:clean
clean:
rm -f *.o main

make 编译

清除生成的 .o 文件

3、运用变量编写Makefile文件
#Makefile
OBJ=main.o test1.o test2.o
CC =gcc
test:$(OBJ)
$(CC) -o $@ $^
main.o:main.c test1.h test2.h
$(CC) -c $<
test1.o:test1.c test1.h
$(CC) -c $<
test2.o:test2.c test2.h
$(CC) -c $<
.PHONY:clean
clean:
rm -f $(OBJ) test

make编译

实例二
1、分析源文件代码依赖关系

hello.c
void showhello() {
hello();
}
生成 hello.o 文件需要依赖 hello.c , hello.h
hello.h
/*hello.h*/
#ifndef HELLO_H
#define HELLO_H void hello() {
star1();
printf("hello,my friends\n");
} #endif
hello.h 里面有 star() 函数的函数体
star.c
#include "starfun.h"
#include "hello.h"
#include <stdio.h>
int main() {
star1();
star2();
showhello();
return ;
}
生成 star.o 文件,需要依赖 star.c , hello.h , starfun.h 文件
starfun.h
/*****starfun.h*****/
#ifndef STARFUN_H
#define STARFUN_H #define NUM 4
#define NUMBER 3 int star1() {
int i,j,k;
for(k=;k<=NUM;++k) {
for(i=;i<=(NUM-k);++i)
printf(" ");
for(j=;j<=(*k-);++j)
printf("*");
printf("\n");
}
return ;
}
int star2() {
int i,j,k;
for(k=NUMBER;k>=;--k) {
for(i=;i<=(NUMBER-k+);++i)
printf(" ");
for(j=;j<=(*k-);++j)
printf("*");
printf("\n");
}
return ;
} #endif
2、直接编写Makefile文件
[root@localhost test_2]# vim Makefile
#cjj
main:hello.o star.o
gcc -o main hello.o star.o
hello.o:hello.c hello.h
gcc -c hello.c
star.o:star.c hello.h starfun.h
gcc -c star.c
.PHONY:clean
clean:
rm -f *.o main

make 编译(会出现警告,不影响运行)

运行生成的文件
[root@localhost test_2]# ./main

删除 .o 文件和 main 文件

3、运用变量编写Makefile文件
#cjj
OBJ=hello.o star.o
CC =gcc
STAR=star.c hello.h starfun.h main:$(OBJ)
$(CC) -o $@ $^
hello.o:hello.c hello.h
$(CC) -c $<
star.o:$(STAR)
$(CC) -c star.c
.PHONY:clean
clean:
rm -f $(OBJ) main

编译并运行

根据给定文件编写Makefile文件 两种方法编译的更多相关文章
- Windows文件自删除的两种方法
可执行模块的自删除技术已经被讨论的很多, 有很多极富创意的思路和想法被提出, 但有些似是而非的方案往往使人误入歧途. 举个例子来说, 很多文章认为下面的一小段代码可以实现自删除:void main(v ...
- C#实现Web文件上传的两种方法
1. C#实现Web文件的上传 在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛. 那么使用C#如何实现文件上传的功能呢?下面笔 ...
- SpringBoot从入门到精通十一(SpringBoot文件上传的两种方法)
前言 在企业级项目开发过程中,上传文件是最常用到的功能.SpringBoot集成了SpringMVC,当然上传文件的方式跟SpringMVC没有什么出入. 本章目标 使用SpringBoot项目完成单 ...
- vba判断文件是否存在的两种方法(转)
方法1. 用VBA自带的dir()判断,代码如下: 在 Microsoft Windows 中, Dir 支持多字符 (*)和单字符 (?) 的通配符来指定多重文件 Function IsFileEx ...
- VC++实现获取文件占用空间大小的两种方法(非文件大小)
// GetFileSpaceSize.cpp : Defines the entry point for the console application. // /***************** ...
- 清除SQLServer日志的两种方法
日志文件满而造成SQL数据库无法写入文件时,可用两种方法:一种方法:清空日志.1.打开查询分析器,输入命令DUMP TRANSACTION 数据库名 WITH NO_LOG2.再打开企业管理器--右键 ...
- 教会你如何编写makefile文件
最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...
- 如何编写makefile文件
最近一直在学习makefile是如何编写的. 当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make ...
- 转:教会你如何编写makefile文件
最近一直在学习makefile是如何编写的.当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make命令,它让你的 ...
随机推荐
- HDU 2680 Choose the best route(多起点单终点最短路问题)题解
题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...
- JVM的反射实现
java的反射机制 java的反射机制是在运行状态中,对于任意一个类(Class)都能知道他的属性(Field)和方法(Method),对于任意一个对象都能够调用它的方法和属性:这种动态获取信息以及动 ...
- CentOS环境Docker安装教程(官方推荐的docker三种方式安装)
CentOS环境Docker安装教程(官方推荐的docker三种方式安装) 一.使用yum方式安装 1.安装依赖包 $ sudo yum install -y yum-utils device-map ...
- JQuery中width和JS中JS中关于clientWidth offsetWidth scrollWidth 等的含义
JQuery中: width()方法用于获得元素宽度: innerWidth()方法用于获得包括内边界(padding)的元素宽度: outerWidth()方法用于获得包括内边界(padding)和 ...
- 通过电信ADSL无线猫WLAN上网的方法
本教程只适合中国电信ADSL无线猫使用wifi(路由器不适合此帖)我的无线猫是电信赠送的华为[EchoLife]HG522c,亲测可用,解决网关无回应! 首先打开IE(注意,只能是IE,其他内核的浏览 ...
- ActiveStorage Overview --Rails guide (history:7-1更新)
如何attach一个或多个文件到一个记录.has_many_attach()方法. 如何删除一个附加的文件. purge方法 如何连接到一个附加的文件.url_for() 如何使用variants来转 ...
- UVA-806 Spatial Structures (四分树)
题目大意:将一块图像上的黑点在两种表示法之间转换. 题目分析:递归下去... 注意:输出时要注意细节!!! 代码如下: # include<iostream> # include<c ...
- iOS-程序启动原理和UIApplication
iOS开发UI篇—程序启动原理和UIApplication 一.UIApplication 1.简单介绍 (1)UIApplication对象是应用程序的象征,一个UIApplication对象就 ...
- mybatis----Integer = 0 刷选不出来条件原因以及sql改法
Xml写法: POJO: 当status的值为 0时该where SQLand status = 0并未正常拼接,也就是说test内的表达式为false,从而导致查询结果错误.但是,显然该值(Inte ...
- Junit4与junt3并存时产生的问题
目前的项目里用junit写单元测试,使用的是junit4,由于大部分开发之前使用的都是junit3,对junit4还不是很熟悉,所以出现了junit3和4混合使用的情况,导致发生了一些问题,这里列举一 ...