카테고리 없음

Makefile에서 사용하는 함수나 명령어들

술퍼 2024. 7. 25. 23:07

Makefile에서 제공하는 특수 함수와 명령어들은 문자열 처리, 조건 검증, 파일 처리 등 
다양한 작업을 간단하게 수행할 수 있게 도와줍니다. 
이러한 함수들은 Makefile을 더욱 강력하고 유연하게 만들어줍니다. 
아래에 몇 가지 중요한 함수와 명령어들을 정리했습니다:

$(findstring find, in)
     문자열 in에서 find가 포함되어 있는지 확인하고, 포함되어 있으면 find를 반환, 그렇지 않으면 빈 문자열을 반환.
      예: $(findstring a, abc) 결과: a

$(filter pattern..., text)
      text에서 pattern에 일치하는 단어들을 반환.
      예: $(filter %.c, foo.c bar.txt) 결과: foo.c

$(filter-out pattern..., text)
      text에서 pattern에 일치하지 않는 단어들을 반환.
      예: $(filter-out %.c, foo.c bar.txt) 결과: bar.txt

$(sort list)
      list를 정렬하고 중복을 제거.
      예: $(sort foo bar foo) 결과: bar foo

$(wildcard pattern)
      파일 시스템에서 pattern에 일치하는 파일 이름들을 반환.
      예: $(wildcard *.c) 결과: foo.c bar.c (디렉토리에 따라 다름)

$(subst from,to,text)
      text에서 from을 to로 대체.
      예: $(subst .c,.o,foo.c bar.c) 결과: foo.o bar.o

$(patsubst pattern,replacement,text)
      text에서 pattern에 일치하는 부분을 replacement로 대체.
      예: $(patsubst %.c,%.o,foo.c bar.c) 결과: foo.o bar.o

$(strip string)
      string에서 선행 및 후행 공백을 제거.
      예: $(strip foo bar ) 결과: foo bar

$(shell command)
      쉘 명령어를 실행하고 그 출력을 반환.
      예: $(shell echo hello) 결과: hello

$(error text)
      text를 출력하고 make를 종료.
      예: $(error Something went wrong!)

$(warning text)
      text를 경고로 출력.
      예: $(warning Something might be wrong!)

$(info text)
      text를 정보로 출력.
      예: $(info Building target)



예시 makefile
--------------------------------------------------
FILES := foo.c bar.txt baz.c
CFILES := $(filter %.c, $(FILES))       # FILES 변수에 설정된 문자열 중 .c 로 끝나는 문자열만 추출한다

OFILES := $(patsubst %.c, %.o, $(CFILES))     #CFILES 변수에 설정된 문자열 중 .c 로 끝나는 문자열을 .o 로 끝나는 문자열로 변환하여 반환한다
#####     OFILES = $(CFILES:.c=.o)  # 윗줄과 같은 결과가 나옴

 

all: $(OFILES)

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

info:
    @echo "C files: $(CFILES)"
    @echo "Object files: $(OFILES)"
    @echo "Current directory: $(shell pwd)"

error-demo:
    $(error This is an error message)

warning-demo:
    $(warning This is a warning message)

 

-----------------------------

DATE = $(shell date +'char tstCompileDate[20] = "%Y-%m-%d %H:%M:%S";')

    # 쉘 명령 {  date +'char tstCompileDate[20] = "%Y-%m-%d %H:%M:%S";'   } 을 수행하고 그 결과를 DATE 변수에 저장한다.