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

140번째 줄: 140번째 줄:
</source>
</source>
:→ make clean 명령으로 확장자가 o 인 파일과 exefile 이 삭제된 것을 확인 할 수 있다.
:→ 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>


==같이 보기==
==같이 보기==

2018년 6월 13일 (수) 20:49 판

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

4.2 파일 코드 내용

a.c, a.h, b.c, b.h, main.c 파일 내용은 아래와 같다.

a.c
#include <stdio.h>

void foo()
{
    printf("foo\n");
}
a.h
void foo();
b.c
#include <stdio.h>

void bar()
{
    printf("bar\n");
}
b.h
void bar();
main.c
#include "a.h"
#include "b.h"

int main()
{
    foo();
    bar();

    return 0;
}

4.3 컴파일 하기

object 파일 생성
$ gcc -c -o a.o a.c
$ gcc -c -o b.o b.c
$ gcc -c -o main.o main.c
→ -c 는 컴파일, 어셈블 수행, -o 는 파일명을 정하는 옵션임
실행 파일 생성
gcc -o exefile main.o a.o b.o
→ 링킹하여 실행파일 생성
실행파일 실행해보기
$ ./exefile
foo
bar
→ foo, bar 가 각각 실행이 잘 되는 것을 볼 수 있음

5 Makefile 만들기

Makefile 구조

Makefile 은 목표(Target), 의존 관계(Dependency), 명령(command)의 세가지로 이뤄짐

target ... : dependency ...
(tab)command1
(tab)command2
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
→ vi Makefile 후 위 내용을 입력
Makefile 실행
$ 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
→ 실행 파일인 exefile 이 make 명령을 통해 새로 생성된 것을 알 수 있다.
실행
$ ./exefile
foo
bar
make clean
$ make clean
rm *.o exefile
$ ls
a.c  a.h  b.c  b.h  main.c  Makefile
→ make clean 명령으로 확장자가 o 인 파일과 exefile 이 삭제된 것을 확인 할 수 있다.

6 Makefile 간단히 만들기

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)

7 같이 보기

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