美文网首页
makefile学习笔记-1 (2020-04-08)

makefile学习笔记-1 (2020-04-08)

作者: 诗酒_年华 | 来源:发表于2020-04-08 18:37 被阅读0次

代码路径:

代码:


main.cpp


#include<iostream>

using namespace std;

#include "zte.h"

#include "h3c.h"

int main()

{

    zte();

    h3c();

    return 0;

}


zte.cpp


#include<iostream>

using namespace std;

void zte()

{

    cout << "hello zte .." << endl;

    return;

}


zte.h


#ifndef _ZTE_H_

#define _ZTE_H_

void zte();

#endif


h3c.cpp


#include<iostream>

using namespace std;

void h3c()

{

    cout << "hello h3c .." << endl;

    return;

}


h3c.h


#ifndef _H3C_H_

#define _H3C_H_

void h3c();

#endif

makefile


基础版:

{

main:main.o zte.o h3c.o

    g++ -o main main.o zte.o h3c.o

main.o:main.cpp zte.h h3c.h

    g++ -c main.cpp -o main.o

zte.o:zte.cpp

    g++ -c zte.cpp -o zte.o

h3c.o:h3c.cpp

    g++ -c h3c.cpp -o h3c.o

clean:

    rm -rf *.o *.out *.s main

hello:

    g++ -o main main.o zte.o h3c.o

    ./main

}

注:1,g++ -c *.cpp 默认生成对应的.o文件,所以-o *.o可以不写

        2,g++或者gcc前面一定要有tab

优化版:变量使用:

{

OBJS = main.o zte.o h3c.o

INC = zte.h h3c.h

TARGET = main

main:$(OBJS)

    g++ -o $(TARGET) $(OBJS)

main.o:main.cpp $(INC)

    g++ -c main.cpp -o main.o

zte.o:zte.cpp

    g++ -c zte.cpp -o zte.o

h3c.o:h3c.cpp

    g++ -c h3c.cpp -o h3c.o

clean:

rm -f *.o *.out *.s main

hello:

    g++ -o $(TARGET) $(OBJS)

./$(TARGET)

}

精简版:make自动推导依赖文件

{

OBJS = main.o zte.o h3c.o

INC = zte.h h3c.h

TARGET = main

main:$(OBJS)

g++ -o $(TARGET) $(OBJS)

main.o:$(INC)

zte.o:

h3c.o:

clean:    

    rm -f *.o *.out *.s main

hello:

    g++ -o $(TARGET) $(OBJS)

./$(TARGET)

}


执行结果:


[huchangkun@vm64-1 hello]$ make -j

g++    -c -o main.o main.cpp

g++    -c -o zte.o zte.cpp

g++    -c -o h3c.o h3c.cpp

g++ -o main main.o zte.o h3c.o

[huchangkun@vm64-1 hello]$ make hello

g++ -o main main.o zte.o h3c.o

./main

hello zte ..

hello h3c ..

[huchangkun@vm64-1 hello]$

[huchangkun@vm64-1 hello]$ make clean

rm -f *.o *.out *.s main

[huchangkun@vm64-1 hello]$ ls

h3c.cpp  h3c.h  main.cpp  makefile  zte.cpp  zte.h

[huchangkun@vm64-1 hello]$

相关文章

  • makefile学习笔记-1 (2020-04-08)

    代码路径: 代码: main.cpp #include using namespace std; #include...

  • Makefile学习笔记

    Makefile学习笔记 学习Makefile的资料 《跟我一起写makefile》 《GUN make manu...

  • Makefile自动化变量

    学习笔记,摘自陈皓的《跟我一起写 Makefile》 Makefile规则 Makefile文件由一系列规则构成。...

  • Makefile学习笔记

    Makefile学习笔记 概述 什么是makefile?或许很多Windows程序员都不知道这个东西,因为那些Wi...

  • Makefile学习笔记

    格式:ar rcs libxxx.a xx1.o xx2.o 链接 参数r:在库中插入模块(替换)。当插入的模块...

  • Makefile 学习笔记

    1.命令前的-号表示命令执行失败后继续执行接下来的操作。 2.环境变量中MAKEFIELmake指令会把环境变量中...

  • Makefile学习笔记

    1. 概述 1.1 前言 之前在Linux下写C/C++都是直接输命令行,虽然有使用make的经历,但没有自己动手...

  • makefile编写,GDB调试

    1.makefile编写的三要素 在学习编写makefile文件以前,我们先来看makefile编写的三要素。 1...

  • 2020-04-09

    2020-04-08 菜菜_d868 字数 247 · 阅读 1 2020-04-08 23:12 姓名:邢彩颜 ...

  • Makefile使用(学习笔记)

    makefile的规则 组成 target prerequisites commandtarget这一个或多个的目...

网友评论

      本文标题:makefile学习笔记-1 (2020-04-08)

      本文链接:https://www.haomeiwen.com/subject/krtxmhtx.html