리눅스에서 gcc를위한 간단한 makefile을 어떻게 만드나요?
나는 세 개의 파일이 있습니다 program.c
, program.h
하고 headers.h
.
program.c
포함 program.h
하고 headers.h
.
gcc 컴파일러를 사용하여 Linux에서 이것을 컴파일해야합니다 . 이 작업을 수행하는 방법을 잘 모르겠습니다. Netbeans는 나를 위해 하나를 만들었지 만 비어 있습니다.
흥미롭게도, 소스 파일에 관한 규칙이 주어진 C 컴파일러를 사용하는 것이 기본값인지는 몰랐습니다.
어쨌든 간단한 Makefile 개념을 보여주는 간단한 솔루션은 다음과 같습니다.
HEADERS = program.h headers.h
default: program
program.o: program.c $(HEADERS)
gcc -c program.c -o program.o
program: program.o
gcc program.o -o program
clean:
-rm -f program.o
-rm -f program
(공간 들여 쓰기 대신 탭이 필요하므로 복사 할 때 수정해야 함을 명심하십시오)
그러나 더 많은 C 파일을 지원하려면 각 파일에 대해 새로운 규칙을 만들어야합니다. 따라서 다음을 개선하십시오.
HEADERS = program.h headers.h
OBJECTS = program.o
default: program
%.o: %.c $(HEADERS)
gcc -c $< -o $@
program: $(OBJECTS)
gcc $(OBJECTS) -o $@
clean:
-rm -f $(OBJECTS)
-rm -f program
나는 보통 makefile에서 보이는 $ (CC)와 $ (CFLAGS)와 같은 변수를 생략함으로써 이것을 가능한 한 단순하게 만들려고 노력했다. 당신이 그것을 알아내는 데 관심이 있다면, 나는 당신에게 좋은 시작을 해주기를 바랍니다.
다음은 C 소스에 사용하려는 Makefile입니다. 자유롭게 사용하십시오 :
TARGET = prog
LIBS = -lm
CC = gcc
CFLAGS = -g -Wall
.PHONY: default all clean
default: $(TARGET)
all: default
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)
%.o: %.c $(HEADERS)
$(CC) $(CFLAGS) -c $< -o $@
.PRECIOUS: $(TARGET) $(OBJECTS)
$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) -Wall $(LIBS) -o $@
clean:
-rm -f *.o
-rm -f $(TARGET)
make 유틸리티의 와일드 카드 및 patsubst 기능을 사용하여 현재 디렉토리에 .c 및 .h 파일을 자동으로 포함합니다. 즉, 디렉토리에 새 코드 파일을 추가 할 때 Makefile을 업데이트 할 필요가 없습니다. 그러나 생성 된 실행 파일, 라이브러리 또는 컴파일러 플래그의 이름을 변경하려면 변수를 수정하면됩니다.
두 경우 모두 autoconf를 사용하지 마십시오. 부탁합니다! :)
예를 들어이 간단한 Makefile이면 충분합니다.
CC = gcc CFLAGS =-벽 모두 : 프로그램 프로그램 : program.o program.o: program.c program.h headers.h clean: rm -f program program.o run: program ./program
Note there must be <tab>
on the next line after clean and run, not spaces.
UPDATE Comments below applied
all: program
program.o: program.h headers.h
is enough. the rest is implicit
The simplest make file can be
all : test
test : test.o
gcc -o test test.o
test.o : test.c
gcc -c test.c
clean :
rm test *.o
Depending on the number of headers and your development habits, you may want to investigate gccmakedep. This program examines your current directory and adds to the end of the makefile the header dependencies for each .c/cpp file. This is overkill when you have 2 headers and one program file. However, if you have 5+ little test programs and you are editing one of 10 headers, you can then trust make to rebuild exactly those programs which were changed by your modifications.
gcc program.c -o program
no makefile necessary.
참고URL : https://stackoverflow.com/questions/1484817/how-do-i-make-a-simple-makefile-for-gcc-on-linux
'programing tip' 카테고리의 다른 글
안드로이드 아카이브 라이브러리 (ar) vs 표준 jar (0) | 2020.07.10 |
---|---|
정확히 파이썬의 file.flush ()가 무엇을하고 있습니까? (0) | 2020.07.10 |
iPython 출력에 HTML을 포함시키는 방법은 무엇입니까? (0) | 2020.07.10 |
제출하기 전에 어떻게해야합니까? (0) | 2020.07.10 |
node.js와 Python 결합 (0) | 2020.07.10 |