2016-12-23 1 views
0

동일한 소스 코드에 대해 두 종류의 makefile이 있습니다. 하나는 작동하지만 다른 하나는 작동하지 않습니다. gnu 가이드에 따라 makefile을 적어 둡니다. 그러나 두 번째 메이크 파일이 작동하지 않는 이유를 찾을 수 없습니다. 이유를 알려 주실 수 있습니까? 최종 makefile은 변경된 파일 만 다시 컴파일하는 것입니다.왜이 메이크 파일이 작동하지 않습니까?

첫 번째 메이크 파일 : 작동 중. 파일을 변경하지 않고 모든 파일을 컴파일합니다.

test: 
     gcc -c test.c -o test.out 
     gcc -c test2.c -o test2.out 

clean: 
     rm -rf test.out test2.out 

2 메이크 :

all: program 
program: test.o test2.o 
     gcc test.o test2.o -o program 

test.o: test.c 
     gcc -c test.c 
test2.o: test2.c 
     gcc -c test2.c 

clean: 
     rm -rf test.o test2.out 

편집 작업을 작동하지는 컴파일이 성공적으로 수행을 의미합니다.

gcc test.o test2.o -o program 
test2.o: In function `paths': 
test2.c:(.text+0x1ca): multiple definition of `paths' 
test.o:test.c:(.text+0x1d2): first defined here 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21 
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2 
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
test.o: In function `dead_code': 
test.c:(.text+0xce): undefined reference to `some_func' 
test.o: In function `reverse_negative': 
test.c:(.text+0x150): undefined reference to `some_func' 
test.o: In function `paths': 
test.c:(.text+0x1fc): undefined reference to `some_other_function' 
test.c:(.text+0x21d): undefined reference to `yet_another_function' 
test.c:(.text+0x239): undefined reference to `do_some_things' 
test2.o: In function `dead_code2': 
test2.c:(.text+0xc6): undefined reference to `some_func2' 
test2.o: In function `negative_returns2': 
test2.c:(.text+0x109): undefined reference to `read2' 
test2.o: In function `reverse_negative2': 
test2.c:(.text+0x148): undefined reference to `some_func2' 
test2.o: In function `paths': 
test2.c:(.text+0x1f4): undefined reference to `some_other_function' 
test2.c:(.text+0x215): undefined reference to `yet_another_function' 
test2.c:(.text+0x231): undefined reference to `do_some_things' 
collect2: error: ld returned 1 exit status 
make: *** [program] Error 1 
+0

연결에 해결 될 수 있습니다 원인, 당신은에 대한 unresolved references 상관 없어 있기 때문에, 잘 작동 오브젝트 파일의 undefined reference

의 많은 두 번째 Makefile을 사용하려고하면 어떻게됩니까? – duskwuff

+0

첫 번째 파일에 실행 프로그램이 없습니다. 왜 그것이 효과가 있다고 생각합니까? – DyZ

답변

0

의 차이는 다음과 같습니다 : 나는 2 메이크 파일과 소스 코드를 컴파일 할 때,이 많은 컴파일 오류는 다음과 같다 첫째 makefile 당신이 object 파일을 분리 컴파일에서
, 그것은이다 모든.

command-line option -c is used to compile a source file to an object file도는 오브젝트 파일에 대한 .o 확장 대신 .out을 사용하는 것이 좋습니다. 두 번째 makefile에서

은 두 객체가 같은 방식으로 파일을 컴파일하려고하지만, 당신은 실행하도록 link에 노력하고 있습니다.
그리고 그것이 가장 큰 차이점입니다. 이 경우 Linker에서
발견 multiple definition of 및 오브젝트 파일을 컴파일 할 때이 단계를

관련 문제