How do I make Makefile to recompile only changed files?

前端 未结 3 1269
粉色の甜心
粉色の甜心 2020-12-12 12:41

I have been struggling a bit to get make to compile only the files that have been edited. However I didn\'t have much success and all the files get recompiled. Can someone e

3条回答
  •  粉色の甜心
    2020-12-12 12:52

    Not sure if this is causing your specific problem but the two lines:

    a_functions.c: a.h
    main.c: main.h
    

    are definitely wrong, because there's generally no command to re-create a C file based on a header it includes.

    C files don't depend on their header files, the objects created by those C files do.

    For example, a main.c of:

    #include 
    #include 
    int main (void) { return 0; }
    

    would be in the makefile as something like:

    main.o: main.c hdr1.h hdr2.h
        gcc -c -o main.o main.c
    

    Change:

    a_functions.o: a_functions.c
    a_functions.c: a.h
    main.o: main.c
    main.c: main.h
    

    to:

    a_functions.o: a_functions.c a.h
    main.o: main.c main.h
    

    (assuming that a_functions.c includes a.h and main.c includes main.h) and try again.

    If that assumption above is wrong, you'll have to tell us what C files include what headers so we can tell you the correct rules.


    If your contention is that the makefile is still building everything even after those changes, you need look at two things.

    The first is the output from ls -l on all relevant files so that you can see what dates and times they have.

    The second is the actual output from make. The output of make -d will be especially helpful since it shows what files and dates make is using to figure out what to do.


    In terms of investigation, make seems to work fine as per the following transcript:

    =====
    pax$ cat qq.h
    #define QQ 1
    =====
    pax$ cat qq.c
    #include "qq.h"
    int main(void) { return 0; }
    =====
    pax$ cat qq.mk
    qq: qq.o
            gcc -o qq qq.o
    
    qq.o: qq.c qq.h
            gcc -c -o qq.o qq.c
    =====
    pax$ touch qq.c qq.h
    =====
    pax$ make -f qq.mk
    gcc -c -o qq.o qq.c
    gcc -o qq qq.o
    =====
    pax$ make -f qq.mk
    make: `qq' is up to date.
    

提交回复
热议问题