"Makefile 만들기"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 4개는 보이지 않습니다)
16번째 줄: 16번째 줄:
[[File:files.png|600px]]
[[File:files.png|600px]]


===파일 코드 내용===
fe
a.c, a.h, b.c, b.h, main.c 파일 내용은 아래와 같다.
{{소스헤더|a.c}}
<source lang='C'>
#include <stdio.h>
 
void foo()
{
    printf("foo\n");
}
</source>
{{소스헤더|a.h}}
<source lang='C'>
void foo();
</source>
 
{{소스헤더|b.c}}
<source lang='C'>
#include <stdio.h>
 
void bar()
{
    printf("bar\n");
}
</source>
{{소스헤더|b.h}}
<source lang='C'>
void bar();
</source>
 
{{소스헤더|main.c}}
<source lang='C'>
#include "a.h"
#include "b.h"
 
int main()
{
    foo();
    bar();
 
    return 0;
}
</source>
 
===컴파일 하기===
;object 파일 생성
<source lang='console'>
$ gcc -c -o a.o a.c
$ gcc -c -o b.o b.c
$ gcc -c -o main.o main.c
</source>
:→ -c 는 컴파일, 어셈블 수행, -o 는 파일명을 정하는 옵션임
 
;실행 파일 생성
<source lang='C'>
gcc -o exefile main.o a.o b.o
</source>
:→ 링킹하여 실행파일 생성
 
;실행파일 실행해보기
<source lang='C'>
$ ./exefile
foo
bar
</source>
:→ foo, bar 가 각각 실행이 잘 되는 것을 볼 수 있음
 
==Makefile 만들기==
;Makefile 구조
Makefile 은 목표(Target), 의존 관계(Dependency), 명령(command)의 세가지로 이뤄짐
<source lang='makefile'>
target ... : dependency ...
(tab)command1
(tab)command2
</source>
 
;Makefile 작성
<source lang='makefile'>
exefile : a.o b.o main.o
    gcc -o exefile a.o b.o main.o
 
a.o : a.c
    gcc -c -o a.o a.c
 
b.o : b.c
    gcc -c -o b.o b.c
 
main.o : main.c
    gcc -c -o main.o main.c
 
clean:
    rm *.o exefile
</source>
:→ vi Makefile 후 위 내용을 입력
 
;Makefile 실행
<source lang='console'>
$ ls
a.c  a.h  b.c  b.h  main.c  Makefile
$ make
gcc -c -o a.o a.c
gcc -c -o b.o b.c
gcc -c -o main.o main.c
gcc -o exefile a.o b.o main.o
$ ls
a.c  a.h  a.o  b.c  b.h  b.o  exefile  main.c  main.o  Makefile
</source>
:→ 실행 파일인 exefile 이 make 명령을 통해 새로 생성된 것을 알 수 있다.
 
;실행
<source lang='console'>
$ ./exefile
foo
bar
</source>
 
;make clean
<source lang='console'>
$ make clean
rm *.o exefile
$ ls
a.c  a.h  b.c  b.h  main.c  Makefile
</source>
:→ make clean 명령으로 확장자가 o 인 파일과 exefile 이 삭제된 것을 확인 할 수 있다.
 
==Makefile 간단히 만들기==
<source lang='console'>
CC=gcc
TARGET=exefile
OBJECT=a.o b.o main.o
 
$(TARGET) : $(OBJECT)
    $(CC) -o $(TARGET) a.o b.o main.o
 
a.o : a.c
    $(CC) -c -o a.o a.c
 
b.o : b.c
    $(CC) -c -o b.o b.c
 
main.o : main.c
    $(CC) -c -o main.o main.c
 
clean:
    rm $(OBJECT) $(TARGET)
</source>


==같이 보기==
==같이 보기==
167번째 줄: 22번째 줄:
* [[makefile]]
* [[makefile]]


[[분류: make]]
[[분류: 컴파일]]
[[분류: 컴파일]]
[[분류:빌드]]
[[분류:빌드]]

2020년 6월 6일 (토) 03:02 기준 최신판

1 개요[ | ]

makefile 만들기
How to make the makefile
  • make 빌드를 위한 makefile 만들기

2 make 란?[ | ]

3 Makefile 이란?[ | ]

4 컴파일 예제[ | ]

4.1 파일 개요[ | ]

a.c (a.h), b.c (b.h), main.c 파일 생성

Files.png

fe

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}