라벨이 make인 게시물 표시

[Linux] make - 3

이미지
 이젠 Makefile 을 src 에 두지 않고 아래와 같이 따로 상위 폴더로 분리 해 보자 /* include/my_math.h */ int add ( int x, int y) ; int subtract ( int x, int y) ; int multiply ( int x, int y) ; int divide ( int x, int y) ; ​ /* src/main.c */ # include <stdio.h> # include <pthread.h> # include <my_math.h> void * thread_func ( void *data) { printf ( "[%s]\n" , __func__); printf ( "%d\n" , add( 10 , 5 )); printf ( "%d\n" , subtract( 10 , 5 )); printf ( "%d\n" , multiply( 10 , 5 )); printf ( "%d\n" , divide( 10 , 5 )); return 0 ; } int main ( void ) { pthread_t thread; pthread_create(&thread, 0 , thread_func, 0 ); pthread_join(thread, 0 ); return 0 ; } /* src/add.c */ # include <my_math.h> int add ( int x, int y) { return x + y; } //----------------------------------------------------------------------------------

[Linux] make - 2

이미지
 이전 예제에서는 소스파일과 헤더파일이 동일한 경로에 있었는데, 이번에는 c파일은 src 폴더에, h파일은 include 폴더에 나누어 위치 시키고 컴파일을 해 보자. 전체적인 구조는 다음과 같다. /* include/add.h */ int add ( int x, int y) ; /* src/add.c */ # include <add.h> int add ( int x, int y) { return x + y; } /* main.c */ # include <stdio.h> # include <add.h> int main ( void ) { int value = 0 ; value = add( 3 , 5 ); printf ( "%d\n" , value); return 0 ; } Make 파일은 src 디렉토리에 있고, 헤더파일은 include 디렉토리에 있으므로, -I../include 를 추가 해 주어야 한다. # src/Makefile CC = gcc CFLAGS = -I../ include -c TARGET = result OBJS = main.o add.o $(TARGET) : $(OBJS) $(CC) $(OBJS) -o $(TARGET) %.o: %.c $(CC) $(CFLAGS) $< clean: rm -rf $(OBJS) $(TARGET) 이번엔 스레드를 사용하는 예제를 살펴 보자. /* src/main.c */ # include <stdio.h> # include <pthread.h> void * thread_func ( void *data) { printf ( "%s\n" , __func__); } int main ( void ) {

[Linux] make - 1

  프로그램 빌드 자동화 소프트웨어 여러 파일들 간의 의존성 과  각 파일을 위한 명령어 를 정의 한 Makefile을 해석하여 실행파일 또는 라이브러리를 빌드 함 따로 옵션을 주지 않을 경우 default로 Makefile 또는 makefile 파일을 찾고, 다른 이름의 파일을 넘겨 줄 경우에는 -f 옵션으로 파일 이름을 지정 함. 기본구조 Target: Dependencies [TAB키] Commands TAB키 부분에 SPACE키를 사용하면 안됨. Target - Command 실행 결과로 만들어 지게 되는 목적(object) 파일 Dependencis - Target을 만들 때 의존성(연관관계)를 규정. 이 부분에 나열된 파일이 수정되면 command를 수행해서 target을 다시 만듦. 즉 target 파일의 최종 수정 시간과 dependencies에 있는 파일들의 최종 수정 시간을 비교해서 command 수행여부를 결정 Command - Target을 만들기 위해 실행해야 하는 명령. command는 여러줄이 될 수도 있음. Comment - '#' 뒤에 오는 글자는 주석 처리 함. Command 앞에 '@'을 붙여주면 그 명령은 화면에 출력 되지 않음. # 커맨드 앞에 @를 붙이지 않은 경우 # Makefile all : echo hello #---------------------------------------- $ make echo hello hello # 커맨드 앞에 @를 붙인 경우 # Makefile all: @echo hello #---------------------------------------- $ make hello 예를 들어 다음과 같은 2개의 소스 파일과 1개의 헤더 파일이 있다고 할 때, /* add.h */ int add ( int x, int y) ; // ---------------------------------------------------------