Makefile例子

main.cpp

  • main.cpp add.h add.cpp放在同级目录下(针对前三个makefile)
    • 注:对于include(包含各种.h文件)只需要在生成target的时候加上即可
      eg:hello : hello.o add.o
      		   g++ hello.o add.o -I$(include_path) -o hello// (include_path)指的包含.h文件的路径,g++前面有一个“tab”
      
#include <iostream>
#include "add.h"

using namespace std;

int main()
{
	cout << "Hello World!!!" <<endl;
	cout << "add(3, 5) = " << add(3, 5) << endl;

	return 0;
}

add.h

int add(int a, int b);

add.cpp

#include "add.h"

int add(int a, int b)
{
	return (a + b);
}

makefile1:

Helloworld : main.o add.o
	g++ main.o add.o -o Helloworld

add.o : add.cpp add.h
	g++ -c add.cpp

main.o : main.cpp add.h
	g++ -c main.cpp

clean:
	rm -rf *.o helloworld

makefile2:

target = helloworld
object = main.o add.o

$(target) : $(object)
	g++ $(object) -o $(target)

add.o : add.cpp add.h
	g++ -c add.cpp

main.o : main.cpp add.h
	g++ -c main.cpp

clean:
	rm -rf *.o helloworld

makefile3

target = helloworld
object = main.o add.o

$(target) : $(object)
	g++ $(object) -o $(target)
	
#可以让makefile自动推导,由于都包含.h文件
$(object) : add.h
main.o : main.cpp
add.o : add.cpp

clean:
	rm -rf *.o helloworld

生成静/动态库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值