>  기사  >  운영 및 유지보수  >  리눅스에서 make 명령을 사용하는 방법

리눅스에서 make 명령을 사용하는 방법

WBOY
WBOY앞으로
2023-05-15 12:37:124489검색

make의 작동 원리
make의 메커니즘을 모르는 사람들을 위해 make 명령은 명령줄 인수와 같은 대상을 허용합니다. 이러한 대상은 일반적으로 "makefiles"라는 특수 파일에 저장되며 파일에는 대상에 해당하는 작업도 포함됩니다. 자세한 내용은 makefile 작동 방식에 대한 이 기사 시리즈를 읽어보세요.

make 명령이 처음 실행되면 makefile을 스캔하여 대상과 해당 종속성을 찾습니다. 이러한 종속성 자체가 대상인 경우 이러한 종속성에 대한 makefile을 계속 검색하여 해당 종속성을 설정한 다음 컴파일합니다. 기본 종속성이 컴파일되면 기본 대상이 컴파일됩니다(make 명령을 통해 전달됨).

이제 특정 소스 파일을 수정하고 make 명령을 다시 실행한다고 가정하면 소스 파일과 관련된 대상 파일만 컴파일되므로 최종 실행 파일을 컴파일하면 많은 시간이 절약됩니다.

make 명령어 예시
다음은 이번 글에서 사용한 테스트 환경입니다.

os —— ubunut 13.04
shell —— bash 4.2.45
application —— gnu make 3.81

다음은 프로젝트 내용입니다.

$ ls 
anothertest.c makefile test.c test.h

메이크파일 내용은 다음과 같습니다.

all: test 

test: test.o anothertest.o 
  gcc -wall test.o anothertest.o -o test

test.o: test.c 
  gcc -c -wall test.c 

anothertest.o: anothertest.c 
  gcc -c -wall anothertest.c 

clean: 
  rm -rf *.o test

이제 살펴보겠습니다. Linux에서 make 명령 응용 프로그램의 몇 가지 예 :

1. 간단한 예

전체 프로젝트를 컴파일하려면 간단히 make를 사용하거나 make 명령 다음에 대상을 가져올 수 있습니다.

$ make 
gcc -c -wall test.c 
gcc -c -wall anothertest.c 
gcc -wall test.o anothertest.o -o test

make 명령어와 실제 타겟에 의해 최초로 생성된 의존성을 확인할 수 있습니다.

디렉토리 내용을 다시 살펴보면 그 안에 .o 파일과 실행 파일이 더 있습니다.

$ ls 
anothertest.c anothertest.o makefile test test.c test.h test.o

이제 test.c 파일을 일부 수정하고 make를 재사용하여 프로젝트를 컴파일한다고 가정합니다. :

$ make 
gcc -c -wall test.c 
gcc -wall test.o anothertest.o -o test

test.o만 재컴파일됐지만, 다른 test.o는 재컴파일되지 않은 것을 볼 수 있습니다.

이제 모든 대상 파일과 실행 파일 테스트를 정리하려면 target clean:

$ make clean
rm -rf *.o test

$ ls
anothertest.c makefile test.c test.h

을 사용하면 모든 .o 파일과 실행 파일 테스트가 삭제된 것을 확인할 수 있습니다.

2. -b 옵션을 사용하여 모든 대상을 항상 다시 빌드하세요.

지금까지 make 명령이 마지막 컴파일 이후 변경되지 않은 파일을 컴파일하지 않는다는 것을 알아차렸을 것입니다. make의 기본 동작에서는 -b 옵션을 사용할 수 있습니다.

예는 다음과 같습니다.

$ make
make: nothing to be done for `all'.

$ make -b
gcc -c -wall test.c
gcc -c -wall anothertest.c
gcc -wall test.o anothertest.o -o test

make 명령이 어떤 파일도 컴파일하지 않더라도 make -b는 모든 대상 파일과 최종 실행 파일을 강제로 컴파일한다는 것을 알 수 있습니다.

3. 디버깅 정보를 인쇄하려면 -d 옵션을 사용하세요.

make가 실행될 때 실제로 무엇을 하는지 알고 싶다면 -d 옵션을 사용하세요.

예는 다음과 같습니다.

$ make -d | more
gnu make 3.81
copyright (c) 2006 free software foundation, inc.
this is free software; see the source for copying conditions.
there is no warranty; not even for merchantability or fitness for a
particular purpose.

this program built for x86_64-pc-linux-gnu
reading makefiles…
reading makefile `makefile'…
updating makefiles….
considering target file `makefile'.
looking for an implicit rule for `makefile'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.o'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cc'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cpp'.
trying pattern rule with stem `makefile'.
--more--

출력이 매우 깁니다. 출력을 페이지별로 표시하기 위해 more 명령을 사용한 것도 보셨을 것입니다.

4 -c 옵션을 사용하여 디렉터리를 변경하세요.

make 명령에 다른 디렉터리 경로를 제공할 수 있으며, makefile을 찾기 전에 디렉터리가 전환됩니다.

현재 디렉토리에 있다고 가정하면 이것은 디렉토리입니다:

$ ls 
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt

그러나 실행하려는 make 명령의 makefile 파일은 ../make-dir/ 디렉토리에 저장되어 있습니다. 다음을 수행할 수 있습니다:

$ make -c ../make-dir/ 
make: entering directory `/home/himanshu/practice/make-dir' 
make: nothing to be done for `all'. 
make: leaving directory `/home/himanshu/practice/make-dir

make 명령이 먼저 특정 디렉터리로 전환하여 실행한 다음 다시 전환하는 것을 볼 수 있습니다.

5 다른 파일을 makefile로 처리하려면 -f 옵션을 사용하십시오. f 옵션.

make -f my_makefile

이 방법을 사용하면 make 명령이 makefile 대신 my_makefile을 검색하도록 선택합니다.

위 내용은 리눅스에서 make 명령을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제