main.cpp
- main.cpp add.h add.cpp放在同级目录下(针对前三个makefile)
#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
生成静/动态库