Makefiles: 무엇이며 그들을 어떻게 사용할 수 있나요?

Makefiles가 무엇을 하는가?
Makefiles는 Make라는 프로그램을 사용하여 빌드 프로세스를 자동화하는 데 사용됩니다. 이 프로그램은 컴파일과 같은 작업을 포함합니다.make 명령을 실행하면 기본적으로 Makefile 또는 makefile 파일을 찾습니다. 이 파일에는 프로그램의 각 파일을 컴파일하거나 빌드하는 방법에 대한 지침이 포함되어야 합니다.
Makefiles는 "존속성" 또는 "전제 조건"이라는 개념을 기반으로 만들어집니다. Make는 파일 시간戳를 사용하여 이러한존속성을 확인합니다. 이는 Make가 컴파일을 크게 속도를 높일 수 있음을 의미합니다. 각 소스 파일을 재컴파일하는 대신, Make는 최소한의 재구축만 수행합니다. 일반적으로 소스 파일이 변경되면 Make는 해당 파일을 재컴파일하지만, 그렇지 않으면 그대로 둡니다.Makefile이 어떻게 생겼나요?
akefiles에 대해 알아야 할 첫 번째 것은 공백이 의미가 있다는 것입니다. 이는 일반적으로 나쁜 설계 결정으로 간주되지만, 우리는 그대로 사용해야 하므로 주의하세요!Makefile에는 각 구성 요소를 빌드하는 방법을 설명하는 규칙의 시리즈가 포함됩니다. 규칙의 일반적인 패턴은 다음과 같습니다:대상: 의존성 동작여기서는 거의 가장 간단한 makefile입니다. 이 makefile은 단일 소스 파일을 실행 파일로 컴파일합니다.program: program.c gcc -o program program.c 이 makefile의 두 번째 줄은 들여쓰여져 있으며, 들여쓰기는 반드시 단일 탭 문자여야 합니다. makefile을 복사 붙여넣기하거나 수동으로 작성할 때는 매우 신중하게 들여쓰기 줄의 시작 부분에 단일 탭 문자를 유지해야 합니다. 예를 들어, 탭 대신 4개의 공백을 사용하면 "Makefile:2: *** missing separator. Stop."와 같은 오류를 볼 수 있습니다.이 예제에서는 대상이 최종 실행 파일의 이름인 program입니다. 대상이 파일 이름과 일치할 필요는 없습니다만, 일반적으로如此입니다.이 목표에 대한 유일한 의존성은 "program.c"이며, Make가 무엇을 할지 결정하기 위해 이것을 사용합니다. "program" 파일이 "program.c"보다 더 최신이라면, Make는 아무것도 하지 않습니다. 첫 번째 Makefile 작성하기손으로 직접 경험하는 것이 가장 좋고, 운 좋게도 가장 기본적인 형태에서 Make를 사용하기가 쉽습니다.Make 설치
Linux 배포판(예: Ubuntu, Mint, 또는 Debian)에서 apt를 사용할 때 Make를 설치하는 방법은 다음과 같습니다:sudo apt install makeacOS에서 Make를 설치하려면 다음 명령어를 실행하세요:xcode-select --insta또는, homebrew가 설치되어 있다면 다음을 실행하세요:맥스에서 make를 설치하십시오. make 명령어가 실행될 때 "make: command not found" 오류가 표시되면 PATH에 make 프로그램이 설치되어 있지 않습니다. 올바르게 설치되었는지 확인하여 이 문제를 해결할 수 있습니다.단순한 테스트 프로그램 작성
위의 makefile을 간단한 C 프로그램으로 테스트하여 Make가 어떻게 작동하는지 직접 확인할 수 있습니다. gcc와 make가 설치되어 있어야 이 작업을 시도할 수 있습니다.
program.c 파일을 생성하고 다음 내용으로 채우세요.#includeAt this point, test your program is correct (and that you have gcc installed) by running the following:
gcc -o program program.cConfirm that gcc successfully builds the program, then remove it:
rm programWrite the Basic Makefile
Now create the example makefile (as either "Makefile" or "makefile") with the following contents:
program: program.c gcc -o program program.cRun Make
In your terminal, ensure your working directory contains the Makefile and program.c files, then run Make:
makeOnce you've confirmed that Make compiled and built your program, test its behavior by running it again:
makeMake tells you that everything is up-to-date, so it doesn't actually do anything now. This is one of Make's biggest benefits: it will only spend time building components that need to be rebuilt. Try updating your source file and you'll see the difference:
touch program.cmake The touch command here just updates the file's modified time to the current time. This is just a quick way of "tricking" Make into thinking the file has changed.What Else Can Make Do?
Make is only really limited by its file-based nature. Any file can be a part of your build process and act as a target or dependency.
This means that Make is appropriate for a wide range of projects. Anything with predictable, repeatable build steps is suitable.
For example, take a static site generator that generates web pages from other sources, often including Markdown text files. This makefile will rebuild a specific web page:
index.html: index.md pandoc index.md index.htmlWe've only touched the surface of what Make can do here. In reality, makefiles contain patterns to define rules for many different files, creating complex dependency chains.
Next time you build software, see if there's a makefile and try exploring its contents. Hopefully, you can understand some of the basics and recognize some of what's going on under the hood. With the GNU make manual in hand, you can start using Make for your own projects to avoid unnecessary downtime.
카테고리
최신 글
- Satechi 키보드 SM3 메커니컬 키보드 리뷰: 조용하고 생산성에 완벽합니다.
- 이 오래된 휴대 전화가 나의 앞에서 화재 위험으로 변했다.
- 애플에서 초대하는 방법 10가지와 애플 캘린더의 차이점"
- Apple @ Work: Will Apple launch a Google Workspace competitor in 2025?
- Apple, iPhone SE 4 발표를 위한 특별 이벤트 개최 가능성이 낮다.
- 오늘 뉴욕 타임즈 연결(Connections)의 힌트와 정답 (2월 8일, #608)
- Ubuntu 20.04 LTS 지원이 종료될 예정입니다.
- 읽는 사람이 있으신가요? AI가 당신을 위해 읽어드리겠습니다.
- This is my Blu-ray player.
- 새로운 애플 제품이 다음 주에 출시될 예정입니다. 다음이 출시될 예정입니다.
최신 태그
- Slim SM3
- fire risk
- disposal
- damaged
- ithium-ion battery
- Visual Appeal
- Enterprise
- Collaboration
- AI voice
- Speechify