一个小例子学习makefile

前言

makefile推荐资料为陈皓的跟我一起写makefile,需要pdf资源的可以私我

正文

目录结构

---include

------student.h

---src

------student.cc

------main.cc

---makefile

makefile文件

vpath %.h ./include
vpath %.cc ./src
CC = g++
object = main.o student.o 
all: exe
exe: ${object}
    ${CC} -o $@ ${object}        
#main.o: main.cc student.h
#    g++ -c $< -I./include     
#student.o: student.cc student.h
#    g++ -c $< -I./include
${object}: %.o: %.cc student.h
    ${CC} -c $< -I./include    
.PHONE:clean
clean:
    rm -rf ${object} exe 

student.h代码

#ifndef _STUDENT
#define _STUDENT
#include <string>
class Student
{
public:
    Student(int id,std::string name);
    std::string getName();
private:
    int id;
    std::string name;
};
#endif

student.cc代码

#include "student.h"
Student::Student(int id,std::string name):id(id),name(name){

}
std::string Student::getName()
{
    return name;
}

main.cc代码

#include<iostream>
#include"student.h"
using namespace std;
int main()
{
    int id = 121;
    std::string name = "pcxie";
    Student pcxie(id,name);
    cout<<"hello world"<<endl;
    cout<<pcxie.getName()<<endl;
    return 0;
}

执行make all后的目录结构

---include

------student.h

---src

------student.cc

------main.cc

---makefile

---main.o

---student.o

---exe

原文地址:https://www.cnblogs.com/pcxie/p/7985432.html