code

변경된 파일 만 다시 컴파일하도록 Makefile을 만들려면 어떻게해야합니까?

codestyles 2020. 12. 25. 09:54
반응형

변경된 파일 만 다시 컴파일하도록 Makefile을 만들려면 어떻게해야합니까?


편집 된 파일 만 컴파일하기 위해 약간 고심하고 있습니다. 그러나 나는 많은 성공을 거두지 못했고 모든 파일이 다시 컴파일됩니다. 누군가 이유를 설명 할 수 있습니까?

내 파일은 다음과 같습니다.

main.c
a_functions.c

여기서 main.cmain.h를 포함 하고 a_functions.cah를 포함합니다.

내 makefile은 다음과 같습니다.

CC=gcc
CFLAGS=-Wall -I. -c
EXEC_FILE=program1


all: program

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

objects: a_functions.c main.c
    $(CC) a_functions.c main.c $(CFLAGS)

program: a_functions.o main.o
    $(CC) a_functions.o main.o -o $(EXEC_FILE)

제안에 따라 makefile을 변경하면 동일한 문제가있는 것 같습니다.

all: program

a_functions.o: a_functions.c a.h
    gcc a_functions.c -c

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

program: a_functions.o main.o
    gcc a_functions.o main.o -o program1

당신이 말하고있는 특정 문제는- program1아무것도 변경되지 않았는데도 (객체를 다시 연결하여) 재 구축하기 -이 규칙에 있습니다 :

program: a_functions.o main.o
    gcc a_functions.o main.o -o program1

이 규칙의 대상은 program이고 Make는 파일이라고 가정합니다. 그러나 그러한 파일이 없기 때문에 Make를 실행할 때마다 Make는이 파일을 다시 빌드해야한다고 생각하고 규칙을 실행합니다. 나는 이것을 제안한다 :

program1: a_functions.o main.o
    gcc a_functions.o main.o -o program1

또는 더 나은 방법 :

program1: a_functions.o main.o
    gcc $^ -o $@

또는 더 나은 방법 :

$(EXEC_FILE): a_functions.o main.o
    $(CC) $^ -o $@

(그리고 all일치 하도록 규칙을 변경하는 것을 잊지 마십시오 .)

다른 몇 가지 사항 :

  1. @paxdiablo가 지적했듯이

    a_functions.o: a_functions.c a.h
    main.o: main.c main.h
    
  2. 하나의 (아마도 main.o) 무언가가 다른 (아마도 a_functions.o) 무언가를 호출 하지 않는 한 이러한 개체를 함께 연결하는 것은 의미가 없으므로 다음 과 같은 종속성을 볼 것으로 예상합니다.

    main.o: a.h
    

    그래서 나는 당신이 잘못된 선언을 가지고 있다고 생각합니다.

  3. objects규칙을 선언 하지만 결코 참조하지 마십시오. 따라서 실제로 사용하지 않습니다. Make는에 대한 기본 규칙을 사용합니다 %.o: %.c. 나는 이것을 제안한다 :

    OBJECTS = a_functions.o main.o
    $(OBJECTS): %: %.c
        $(CC) $< $(CFLAGS) -o $@
    

    (In which case you can change $(EXEC_FILE): a_functions.o main.o to $(EXEC_FILE): $(OBJECTS).) Or just this:

    %.o: %.c
        $(CC) $< $(CFLAGS) -o $@
    

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 <hdr1.h>
#include <hdr2.h>
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.

ReferenceURL : https://stackoverflow.com/questions/7815400/how-do-i-make-makefile-to-recompile-only-changed-files

반응형