루트 폴더에 개체 파일 (.o
)이 어떻게 표시되는지는 알지만 빌드 폴더에서 파일을 어떻게 가져 오는지는 알 수 없습니다.
단계별로 살펴 보겠습니다. 먼저 우리는 객체 파일에 자신의 규칙을 줄 것입니다 :
# Note the use of "-o ..."
build/file1.o:
gcc -g -fPIC -c src/file1.c -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o build/file1.o
build/file2.o:
gcc -g -fPIC -c src/file2.c -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o build/file2.o
debug_build: $(OBJS)
gcc -shared -o bin/$(SHLIB).so $(OBJS) -lm -lpq -lmylib_core
이것은 효과적이지만 미숙 한 것입니다. 객체 파일은 이제 build/
으로 들어갑니다. 그러나 중복성이 많으며 의존성 처리가 없습니다. 그래서 우리는 GNUMake을 (이 당신이해야한다), 우리가 Automatic Variables을 사용할 수 있습니다 (난 그냥 가독성을 위해 -I 문자열을 생략합니다)를 사용하는 전제 조건에 넣어, 그리고 가정 :
build/file1.o: src/file1.c
gcc -g -fPIC -c $< -I... -o [email protected]
build/file2.o: src/file2.c
gcc -g -fPIC -c $< -I... -o [email protected]
debug_build: $(OBJS)
gcc -shared -o bin/$(SHLIB).so $^ -lm -lpq -lmylib_core
주의 그 명령에 객체 규칙이 이제 정확히 동일합니다. 그래서 우리는이 두 규칙을 몇 가지 다른 방법으로 조합 할 수 있습니다. 가장 간단한은 다음과 같습니다
build/file1.o: src/file1.c
build/file2.o: src/file2.c
build/file1.o build/file2.o:
gcc -g -fPIC -c $< -I... -o [email protected]
이제 하나 또는 두 개의 더 작은 개조하면 되겠 어 우리가 갈 수 있어요 :
build/file1.o: src/file1.c
build/file2.o: src/file2.c
build/file1.o build/file2.o:
gcc -g -fPIC -c $< -I`pg_config --includedir` -I`pg_config --includedir-server` -I/some/required/path/include -Iinclude -o [email protected]
debug_build: $(OBJS)
gcc -shared -o bin/$(SHLIB).so $^ -lm -lpq -lmylib_core
이보다 정교한 트릭이 있습니다,하지만 지금은 충분히해야한다.
내가 어디에서 왔는지 모르겠지만 makefile을 직접 만들기 전에'automake'를 시도해보십시오. 이것은 트리 구조가 아닌 이식성 있고 사용자가 * 설정할 수있는 방식으로 빌드를 지원합니다. – thiton