관리 메뉴

개발 노트

9/22 p.81 gcc, makefile 파일 본문

학부 공부 : 20.08.31~12.10/Linux

9/22 p.81 gcc, makefile 파일

hayoung.dev 2020. 9. 22. 16:53

<p.81 : gcc>

$vi sum.c로 내용 작성

int sum(int a, int b) {
	return a + b;
}

$vi helloworld.c로 내용 작성

#include <stdio.h>
int sum(int, int);
int main(int argc, char **argv)
{
	printf("Hello World\n");
	printf("Sum : %d\n", sum(3,5));

	return 0;
}

 

$gcc -o helloworld hellworld.c sum.c 작성 후

$./helloworld 하면

결과 출력됨

 Hello World

 Sum : 8

 

 

 

<p.84 : makefile 파일>

$vi makefile

all :    //all : 쓰고 enter쳐서 밑의 줄의 tab 처리 된 다음부터 작성하도록 해야 함
        gcc -o helloworld helloworld.c sum.c

 

//위와같이 makefile을 만들어두면 make만 실행해도 하단처럼 안의 명령문 실행됨

$ make

gcc -o helloworld helloworld.c sum.c

$ ./helloworld

Hello World

Sum : 8

 

$ vi makefile2

all : helloworld.o sum.o
        gcc -o helloworld helloworld.o sum.o

helloworld.o : helloworld.c
        gcc -c helloworld.c

sum.o : sum.c
        gcc -c sum.c

 

$ make -f makefile2   //실행하면 하단 내용 출력됨

gcc -c helloworld.c

gcc -c sum.c

gcc -o helloworld helloworld.o sum.o

$ ./helloworld

Hello World

Sum : 8

 

참고로 -c 는 컴파일만 하라는 뜻

반응형