본문 바로가기
IT일반/Linux

Command Line - 프로그램 컴파일

by 버섯도리 2022. 6. 3.
[cmd_test@btjeon-naver src]$ ftp ftp.gnu.org
Trying 209.51.188.20...
Connected to ftp.gnu.org (209.51.188.20).
220 GNU FTP server ready.
Name (ftp.gnu.org:cmd_test): anonymous
230-NOTICE (Updated October 15 2021):
230-
...
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd gnu/diction
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (209,51,188,20,165,137).
150 Here comes the directory listing.
-rw-r--r--    1 3003     65534       68940 Aug 28  1998 diction-0.7.tar.gz
-rw-r--r--    1 3003     65534       90957 Mar 04  2002 diction-1.02.tar.gz
-rw-r--r--    1 3003     65534      141062 Sep 17  2007 diction-1.11.tar.gz
-rw-r--r--    1 3003     65534         189 Sep 17  2007 diction-1.11.tar.gz.sig
226 Directory send OK.
ftp> get diction-1.11.tar.gz
local: diction-1.11.tar.gz remote: diction-1.11.tar.gz
227 Entering Passive Mode (209,51,188,20,165,140).
150 Opening BINARY mode data connection for diction-1.11.tar.gz (141062 bytes).
226 Transfer complete.
141062 bytes received in 0.626 secs (225.41 Kbytes/sec)
ftp> bye
221 Goodbye.
[cmd_test@btjeon-naver src]$ ll
total 280
-rw-rw-r-- 1 cmd_test cmd_test 141062 Jun  3 11:35 diction-1.11.tar.gz
[cmd_test@btjeon-naver src]$ tar xzf diction-1.11.tar.gz 
[cmd_test@btjeon-naver src]$ ls
diction-1.11  diction-1.11.tar.gz

컴파일용 테스트 파일(diction-1.11.tar.gz) 다운로드 및 압축 해제

 

[cmd_test@btjeon-naver src]$ cd diction-1.11/
[cmd_test@btjeon-naver diction-1.11]$ ls
config.guess  config.sub  configure.in  de     diction.1.in  diction.pot   diction.spec.in  en     en_GB.po   getopt.c  getopt_int.h  install-sh   misc.c  NEWS  nl.po   sentence.c  style.1.in  test
config.h.in   configure   COPYING       de.po  diction.c     diction.spec  diction.texi.in  en_GB  getopt1.c  getopt.h  INSTALL       Makefile.in  misc.h  nl    README  sentence.h  style.c
[cmd_test@btjeon-naver diction-1.11]$ ls *.c
diction.c  getopt1.c  getopt.c  misc.c  sentence.c  style.c
[cmd_test@btjeon-naver diction-1.11]$ ls *.h
getopt.h  getopt_int.h  misc.h  sentence.h
[cmd_test@btjeon-naver diction-1.11]$ less diction.c

소스 구성 확인

 

[cmd_test@btjeon-naver diction-1.11]$ ./configure 
...
checking libintl.h presence... yes
checking for libintl.h... yes
checking for library containing gettext... none required
configure: creating ./config.status
config.status: creating Makefile
config.status: creating diction.1
config.status: creating diction.texi
config.status: creating diction.spec
config.status: creating style.1
config.status: creating test/rundiction
config.status: creating config.h

./configure : 빌드 환경을 분석한다. 필수적인 외부 툴과 컴포넌트가 설치되어 있는지를 검사한다.

 

[cmd_test@btjeon-naver diction-1.11]$ less Makefile
----------------------------------------------------------------------------------
CC=             gcc
...
diction:        diction.o sentence.o misc.o getopt.o getopt1.o
                $(CC) -o $@ $(LDFLAGS) diction.o sentence.o misc.o \
                getopt.o getopt1.o $(LIBS)
...
diction.o:      diction.c config.h getopt.h misc.h sentence.h
getopt.o:       getopt.c getopt.h getopt_int.h
getopt1.o:      getopt1.c getopt.h getopt_int.h
misc.o: misc.c config.h misc.h
sentence.o:     sentence.c config.h misc.h sentence.h
style.o:        style.c config.h getopt.h misc.h sentence.h
...
----------------------------------------------------------------------------------
[cmd_test@btjeon-naver diction-1.11]$ make
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common diction.c
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common sentence.c
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common misc.c
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common getopt.c
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common getopt1.c
gcc -o diction -g diction.o sentence.o misc.o \
	getopt.o getopt1.o 
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common style.c
gcc -o style -g style.o sentence.o misc.o \
	getopt.o getopt1.o -lm 
msgfmt -o de.mo de.po
msgfmt -o en_GB.mo en_GB.po
msgfmt -o nl.mo nl.po
[cmd_test@btjeon-naver diction-1.11]$ ls
config.guess  config.log     configure     de     diction       diction.c    diction.spec     diction.texi.in  en_GB.mo   getopt1.o  getopt_int.h  install-sh   misc.c  NEWS   nl.po       sentence.h  style.1     style.o
config.h      config.status  configure.in  de.mo  diction.1     diction.o    diction.spec.in  en               en_GB.po   getopt.c   getopt.o      Makefile     misc.h  nl     README      sentence.o  style.1.in  test
config.h.in   config.sub     COPYING       de.po  diction.1.in  diction.pot  diction.texi     en_GB            getopt1.c  getopt.h   INSTALL       Makefile.in  misc.o  nl.mo  sentence.c  style       style.c

Makefile 확인 및 make 빌드하기

 

[cmd_test@btjeon-naver diction-1.11]$ make
make: Nothing to be done for `all'.
[cmd_test@btjeon-naver diction-1.11]$ rm getopt.o
[cmd_test@btjeon-naver diction-1.11]$ make
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common getopt.c
gcc -o diction -g diction.o sentence.o misc.o \
	getopt.o getopt1.o 
gcc -o style -g style.o sentence.o misc.o \
	getopt.o getopt1.o -lm 
[cmd_test@btjeon-naver diction-1.11]$ ls getopt.o
getopt.o

make는 삭제된 모듈에 의존적이기 때문에 getopt.o를 다시 빌드하고 diction과 style을 재링크시킨다.

 

[cmd_test@btjeon-naver diction-1.11]$ ls -l diction getopt.c
-rwxrwxr-x 1 cmd_test cmd_test 53480 Jun  3 11:58 diction
-rw-r--r-- 1 cmd_test cmd_test 33125 Mar 31  2007 getopt.c
[cmd_test@btjeon-naver diction-1.11]$ touch getopt.c
[cmd_test@btjeon-naver diction-1.11]$ ls -l diction getopt.c
-rwxrwxr-x 1 cmd_test cmd_test 53480 Jun  3 11:58 diction
-rw-r--r-- 1 cmd_test cmd_test 33125 Jun  3 13:38 getopt.c
[cmd_test@btjeon-naver diction-1.11]$ make
gcc -c -I. -DSHAREDIR=\"/usr/local/share\" -DLOCALEDIR=\"/usr/local/share/locale\" -g -O2 -pipe -Wno-unused -Wshadow -Wbad-function-cast -Wmissing-prototypes -Wstrict-prototypes -Wcast-align -Wcast-qual -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-declarations -Wnested-externs -Wundef -pedantic -fno-common getopt.c
gcc -o diction -g diction.o sentence.o misc.o \
	getopt.o getopt1.o 
gcc -o style -g style.o sentence.o misc.o \
	getopt.o getopt1.o -lm 
(objdir=`pwd`; cd .; \
	  xgettext --add-comments --keyword=_ \
	           --output=$objdir/messages.po *.c )
...
msgfmt -o nl.mo nl.po
[cmd_test@btjeon-naver diction-1.11]$ ls -l diction getopt.c
-rwxrwxr-x 1 cmd_test cmd_test 53480 Jun  3 13:38 diction
-rw-r--r-- 1 cmd_test cmd_test 33125 Jun  3 13:38 getopt.c

소스 파일 중 하나가 갱신된 후 make를 실행하면 타겟이 의존 파일보다 새 파일인 것을 보게 된다.

 

[cmd_test@btjeon-naver diction-1.11]$ sudo make install
[sudo] password for cmd_test: 
[ -d /usr/local/bin ] || /bin/install -c -m 755 -d /usr/local/bin
/bin/install -c diction /usr/local/bin/diction
/bin/install -c style /usr/local/bin/style
/bin/install -c -m 755 -d /usr/local/share/diction
/bin/install -c -m 644 ./de /usr/local/share/diction/de
/bin/install -c -m 644 ./en /usr/local/share/diction/en
(cd /usr/local/share/diction; rm -f C; ln en C)
/bin/install -c -m 644 ./en_GB /usr/local/share/diction/en_GB
/bin/install -c -m 644 ./nl /usr/local/share/diction/nl
[ -d /usr/local/share/man/man1 ] || /bin/install -c -m 755 -d /usr/local/share/man/man1
/bin/install -c -m 644 diction.1 /usr/local/share/man/man1/diction.1
/bin/install -c -m 644 style.1 /usr/local/share/man/man1/style.1
make install-po-yes
make[1]: Entering directory `/home/cmd_test/src/diction-1.11'
for cat in de.mo en_GB.mo nl.mo; do \
	  dir=/usr/local/share/locale/`basename $cat .mo`/LC_MESSAGES; \
	  [ -d $dir ] || /bin/install -c -m 755 -d $dir; \
	  /bin/install -c -m 644 $cat $dir/diction.mo; \
	done
make[1]: Leaving directory `/home/cmd_test/src/diction-1.11'
[cmd_test@btjeon-naver diction-1.11]$ which diction
/usr/local/bin/diction
[cmd_test@btjeon-naver diction-1.11]$ man diction

마지막으로 프로그램을 설치한다.(make install) 보통 /usr/local/bin 디렉토리에 설치되는데, 일반 사용자에게는 쓰기 권한이 없기 때문에 반드시 슈퍼유저로 설치를 진행해야 한다.

 

 

 

 

 

 

출처 : 리눅스 커맨드라인 완벽 입문서

 

'IT일반 > Linux' 카테고리의 다른 글

Command Line - 흐름 제어 : if 분기  (0) 2022.06.03
Command Line - 쉘 스크립트 기초  (0) 2022.06.03
Command Line - 출력 포맷 지정  (0) 2022.06.02
Command Line - 텍스트 편집  (0) 2022.06.01
Command Line - 정규 표현식  (0) 2022.06.01