Linux学习笔记-Makefile的基本使用

135 篇文章 11 订阅

程序自动编译:

在vc中,点击“生成解决方案”就能生成解决方案;
在linux中使用Makefile,是一个脚本文件,和vc中生成解决方案差不多。

 

写如下代码:

other.h


void printOther();

other.cpp

#include <stdio.h>
#include "other.h"

void printOther() {
	printf("printOther called\n");
}

main.cpp

#include "other.h"
#include <stdio.h>

int main() {
	printf("main called\n");
	printOther();
	return 0;
}

运行截图如下:

方法:
1.创建一个文件叫Makefile
2.输入命令,根据Makefile中的指示,自动执行所有的步骤
如:make -f Makefile

make file文件如下:


helloworld:
	g++ main.cpp other.cpp -o helloworld

如下图展示:
创建一个makefile文件:(使用touch Makefile或右键点击新建文件)


make命令会自动解析Makefile里面的内容
或 make -f Makefile

Makefile写法:

target:dependencies
<TAB>system command1
<TAB>system command2
<TAB>system command...

target:目标,
dependencies:依赖
<TAB>每行命令前必须插入一个TAB
system command:系统命令

当存在很多规则时,默认从第一条规则开始执行(只执行一条规则)

输入make命令时,同时显式指定要执行的那一条rule:
make clean
make -f Makefile clean

 

如下图:

因为是vs创建的,用Makefile把vc有关的东西删掉:

如下图:

Makefile如下:


helloworld:
	g++ main.cpp other.cpp -o helloworld
	
clean:
	rm -rf *.vcxproj *.sln *.filters

 

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值