美文网首页
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)

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