Make 파일

make 파일은 대상의 빌드를 나타내는 make 명령에서 참조하는 텍스트 파일로 소스 레벨 종속성 및 빌드 순서 종속성과 같은 정보를 포함합니다.

CDT는 make 파일을 생성할 수 있으며 이러한 프로젝트를 관리 Make 프로젝트라 부릅니다. 표준 Make 프로젝트로 알려진 일부 프로젝트는 사용자 자신의 make 파일을 정의하도록 합니다.

샘플 Make 파일

# A sample Makefile
# This Makefile demonstrates and explains
# Make Macros, Macro Expansions,
# Rules, Targets, Dependencies, Commands, Goals
# Artificial Targets, Pattern Rule, Dependency Rule.

# Comments start with a # and go to the end of the line.

# Here is a simple Make Macro.
LINK_TARGET = test_me.exe

# Here is a Make Macro that uses the backslash to extend to multiple lines.
# This allows quick modification of more object files.
OBJS =  \
 Test1.o \
 Test2.o \
 Main.o

# Here is a Make Macro defined by two Macro Expansions.
# A Macro Expansion may be treated as a textual replacement of the Make Macro.
# Macro Expansions are introduced with $ and enclosed in (parentheses).
REBUILDABLES = $(OBJS) $(LINK_TARGET)

# Make Macros do not need to be defined before their Macro Expansions,
# but they normally should be defined before they appear in any Rules.
# Consequently Make Macros often appear first in a Makefile.

# Here is a simple Rule (used for "cleaning" your build environment).
# It has a Target named "clean" (left of the colon ":" on the first line),
# no Dependencies (right of the colon),
# and two Commands (indented by tabs on the lines that follow).
# The space before the colon is not required but added here for clarity.
clean :
 rm -f $(REBUILDABLES)
 echo Clean done

# There are two standard Targets your Makefile should probably have:
# "all" and "clean", because they are often command-line Goals.
# Also, these are both typically Artificial Targets, because they don't typically
# correspond to real files named "all" or "clean".

# The rule for "all" is used to incrementally build your system.
# It does this by expressing a dependency on the results of that system,
# which in turn have their own rules and dependencies.
all : $(LINK_TARGET)
 echo All done

# There is no required order to the list of rules as they appear in the Makefile.
# Make will build its own dependency tree and only execute each rule only once
# its dependencies' rules have been executed successfully.

# Here is a Rule that uses some built-in Make Macros in its command:
# $@ expands to the rule's target, in this case "test_me.exe".
# $^ expands to the rule's dependencies, in this case the three files
# main.o, test1.o, and  test2.o.
$(LINK_TARGET) : $(OBJS)
 g++ -g -o $@ $^

# Here is a Pattern Rule, often used for compile-line.
# It says how to create a file with a .o suffix, given a file with a .cpp suffix.
# The rule's command uses some built-in Make Macros:
# $@ for the pattern-matched target
# $lt; for the pattern-matched dependency
%.o : %.cpp
 g++ -g -o $@ -c $<

# These are Dependency Rules, which are rules without any command.
# Dependency Rules indicate that if any file to the right of the colon changes,
# the target to the left of the colon should be considered out-of-date.
# The commands for making an out-of-date target up-to-date may be found elsewhere
# (in this case, by the Pattern Rule above).
# Dependency Rules are often used to capture header file dependencies.
Main.o : Main.h Test1.h Test2.h
Test1.o : Test1.h Test2.h
Test2.o : Test2.h

# Alternatively to manually capturing dependencies, several automated
# dependency generators exist.  Here is one possibility (commented out)...
# %.dep : %.cpp
#        g++ -M $(FLAGS) $< > $@
# include $(OBJS:.o=.dep)

자주 질문되는 내용:

콘솔 보기는 빌드를 디버깅하는 데 매우 유용할 수 있습니다.

Q1. 콘솔 보기에 "Error launching builder"라고 표시됩니다. 무슨 의미일까요?

Error launching builder(make k clean all )
(Exec error:Launching failed)

대부분의 경우 빌드 명령(기본적으로 "make")이 경로에 없기 때문일 수 있습니다. 경로에 빌드 명령을 넣고 Eclipse를 다시 시작합니다.
경로에 있는 다른 명령으로 빌드 명령을 변경할 수도 있습니다. MinGW 도구를 사용하여 컴파일하는 경우에는 빌드 명령을 "mingw32-make"로 바꿔야 합니다.

Q2. 콘솔 보기에 "No rule to make target 'X'"라고 표시됩니다.

make -k clean all
make: *** No rule to make target 'clean'.
make: *** No rule to make target 'all'.

보통 make 프로그램은 기본적으로 Makefile" 또는 "makefile"이라는 파일을 검색합니다. 작업 디렉토리에서 이러한 파일을 찾을 수 없는 경우 또는 이 파일이 비어 있거나 파일에 명령행 목표(이 경우 "clean" 및 "all")에 대한 규칙이 들어 있지 않은 경우에는 일반적으로 위에 표시된 것과 유사한 오류 메시지를 표시하며 실패합니다.

유효한 Make 파일이 이미 있으면 빌드의 작업 디렉토리를 변경해야 합니다. 빌드 명령의 기본 작업 디렉토리는 프로젝트 루트 디렉토리입니다. Make 프로젝트 특성에서 대체 빌드 디렉토리를 지정하여 이를 변경할 수 있습니다. 또는 Make 파일의 이름이 이와 다른 것인 경우(예: buildFile.mk) 기본 빌드 명령을 make f buildFile.mk로 설정하여 이름을 지정할 수 있습니다.

유효한 Make 파일이 없으면 루트 디렉토리에서 Makefile이라는 새 파일을 작성하십시오. 그런 다음, 샘플 Makefile(위에서 언급한 파일)의 컨텐츠를 추가하고 이를 적절하게 수정할 수 있습니다.

Q3. 콘솔 보기에 "missing separator"라고 표시됩니다.

make -k clean all
makefile:12: *** missing separator.  Stop.

표준 Make 파일의 구문은 빌드 규칙의 모든 행이 탭 문자보다 앞에 와야합니다. 이 탭 문자는 우연히도 종종 공백으로 교체되고 이로 인해 공백 들여쓰기가 유발되기 때문에 이 문제점을 쉽게 간과하기도 합니다. 제공한 샘플에서는 오류 메시지가 "makefile" 파일의 12행으로 식별될 수 있습니다. 문제를 해결하려면 이 행의 맨 처음에 탭을 삽입하십시오.

Q4. 콘솔 보기에 "Target `all' not remade because of errors"라고 표시됩니다.

make -k clean all
make: *** [clean] Error 255
rm -f Test1.o Test2.o Main.o test_me.exe
g++ -g -o Test1.o -c Test1.cpp
make: *** [Test1.o] Error 255
make: *** [Test2.o] Error 255
make: *** [Main.o] Error 255
g++ -g -o Test2.o -c Test2.cpp
g++ -g -o Main.o -c Main.cpp
make: Target 'all' not remade because of errors.

이 문제에서 가능한 주요 원인은 g++가 경로에 없다는 것입니다.

특정 규칙의 명령을 찾을 수 없는 명령 쉘로 인해 make는 오류 255를 생성합니다.
표준 오류 스트림(Error 255라 표시된 행) 및 표준 출력 스트림(다른 모든 행)의 메시지는 여기의 콘솔 보기에서 병합됩니다.

Q5. -k 플래그의 역할은 무엇입니까?

-k 플래그는 한 규칙이 실패하는 경우에도 make가 계속해서 다른 독립 규칙을 작성하도록 합니다. 이는 대형 프로젝트를 빌드하는 데 유용합니다.

첫 번째 빌드 오류 시 프로젝트 특성 > C/C++ Make 프로젝트 > Make 빌더 > 중지를 켜서 -k 플래그를 제거할 수 있습니다.

Q6. 콘솔 보기에 다음 사항이 표시됩니다.

mingw32-make clean all
process_begin: CreateProcess((null), rm -f Test1.o Test2.o Main.o test_me.exe, ...) failed.
make (e=2): The system cannot find the file specified.

mingw32-make: *** [clean] Error 2
rm -f Test1.o Test2.o Main.o test_me.exe

이는 mingw32-make가 "rm" 유틸리티를 찾을 수 없다는 것을 의미합니다. 불행히도 MinGW는 "rm"과 함께 제공되지 않습니다. 이를 정정하려면 Makefile의 정리 규칙을 다음과 같이 바꾸십시오.

clean : 
	-del $(REBUILDABLES)
	echo Clean done

위에서 선행하는 빼기 부호는 del 명령이 실패를 리턴하는 경우에도 make가 정리 규칙이 성공한 것으로 간주하도록 합니다. 삭제할 지정 파일이 아직(또는 더 이상) 존재하지 않으면 del 명령이 실패하기 때문에 이는 허용될 수 있습니다.

IBM Copyright Statement