Ubuntu下makefile的简单使用

Ubuntu下makefile的简单使用

转   https://www.cnblogs.com/chenguanfu/p/4415072.html

在Windows下,只需要简单的点击以下make,rebuild即可。而在Linux下,这样的IDE环境并没有提供,难道必须每一步都执行一遍吗?比较ok的做法自然是能够利用批处理脚本来进行操作了,这样,只需要修改脚本中需要编译的文件即可。在Linux下,提供了这么一个方便的工具,make。那么接下来我们来利用make进行程序的组织编译吧。

1. 编写tool.h

1

2

3

4

5

6

#ifndef __TOOL_H

#define __TOOL_H

 

void printInteger(int number);

 

#endif // end of tool.h

 2. 编写tool.c

1

2

3

4

5

6

7

#include "tool.h"

#include "stdio.h"

 

void printInteger(int number)

{

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

}

 3. 编写main.c

1

2

3

4

5

6

7

8

9

10

11

#include "tool.h"

#include "stdio.h"

 

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

{

        int number;

        number=10;

        printf("Context-Type:text/html\n\n");

        printInteger(number);

        return 0;

}

 4. 编译链接文件生成可执行文件main

方法1:

命令行中进行编译

4.1.1 编译main.c生成main.o

1

sudo cc -c main.c

 4.1.2 编译tool.c生成tool.o

1

sudo cc -c tool.c

 4.1.3 链接main.o和tool.o生成main可执行文件

1

sudo cc -o main main.o tool.o

 4.1.4 运行main

1

sudo ./main

 方法2:

makefile进行编译链接

4.2.1 编写makefile文件(没有后缀名)

1

2

3

4

5

6

7

8

9

10

11

12

#MakeFile

main:  main.o tool.o

 

main.o: main.c tool.h

        cc -c main.c

 

tool.o: tool.c tool.h

        cc -c tool.c

 

.PHONY:clean

clean:

        rm *.o main

 4.2.2 运行make进行编译链接

1

sudo make

 4.2.3 运行main

1

sudo ./main

 4.2.4 删除所有.o文件并再次编译

1

2

sudo make clean

sudo make

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值