여러 C 프로그램을 컴파일하기위한 Makefile?
이것은 믿을 수 없을 정도로 간단한 질문이지만 저는 makefiles를 처음 사용합니다. 두 개의 독립적 인 프로그램을 컴파일 할 메이크 파일을 만들려고합니다.
program1:
gcc -o prog1 program1.c
program2:
gcc -o prog2 program2.c
온라인의 모든 예제는 내가 필요로하는 것보다 더 자세한 내용을 담고 있으며 혼란 스럽습니다! 내가 정말로 원하는 것은 두 gcc
줄 을 실행하는 것 입니다. 내가 도대체 뭘 잘못하고있는 겁니까?
그렇게 해
all: program1 program2
program1: program1.c
gcc -o program1 program1.c
program2: program2.c
gcc -o program2 program2.c
고급 항목을 원하지 않는다고 말했지만 일부 기본 규칙에 따라 이렇게 줄일 수도 있습니다.
all: program1 program2
program1: program1.c
program2: program2.c
############################################################################
# 'A Generic Makefile for Building Multiple main() Targets in $PWD'
# Author: Robert A. Nader (2012)
# Email: naderra at some g
# Web: xiberix
############################################################################
# The purpose of this makefile is to compile to executable all C source
# files in CWD, where each .c file has a main() function, and each object
# links with a common LDFLAG.
#
# This makefile should suffice for simple projects that require building
# similar executable targets. For example, if your CWD build requires
# exclusively this pattern:
#
# cc -c $(CFLAGS) main_01.c
# cc main_01.o $(LDFLAGS) -o main_01
#
# cc -c $(CFLAGS) main_2..c
# cc main_02.o $(LDFLAGS) -o main_02
#
# etc, ... a common case when compiling the programs of some chapter,
# then you may be interested in using this makefile.
#
# What YOU do:
#
# Set PRG_SUFFIX_FLAG below to either 0 or 1 to enable or disable
# the generation of a .exe suffix on executables
#
# Set CFLAGS and LDFLAGS according to your needs.
#
# What this makefile does automagically:
#
# Sets SRC to a list of *.c files in PWD using wildcard.
# Sets PRGS BINS and OBJS using pattern substitution.
# Compiles each individual .c to .o object file.
# Links each individual .o to its corresponding executable.
#
###########################################################################
#
PRG_SUFFIX_FLAG := 0
#
LDFLAGS :=
CFLAGS_INC :=
CFLAGS := -g -Wall $(CFLAGS_INC)
#
## ==================- NOTHING TO CHANGE BELOW THIS LINE ===================
##
SRCS := $(wildcard *.c)
PRGS := $(patsubst %.c,%,$(SRCS))
PRG_SUFFIX=.exe
BINS := $(patsubst %,%$(PRG_SUFFIX),$(PRGS))
## OBJS are automagically compiled by make.
OBJS := $(patsubst %,%.o,$(PRGS))
##
all : $(BINS)
##
## For clarity sake we make use of:
.SECONDEXPANSION:
OBJ = $(patsubst %$(PRG_SUFFIX),%.o,$@)
ifeq ($(PRG_SUFFIX_FLAG),0)
BIN = $(patsubst %$(PRG_SUFFIX),%,$@)
else
BIN = $@
endif
## Compile the executables
%$(PRG_SUFFIX) : $(OBJS)
$(CC) $(OBJ) $(LDFLAGS) -o $(BIN)
##
## $(OBJS) should be automagically removed right after linking.
##
veryclean:
ifeq ($(PRG_SUFFIX_FLAG),0)
$(RM) $(PRGS)
else
$(RM) $(BINS)
endif
##
rebuild: veryclean all
##
## eof Generic_Multi_Main_PWD.makefile
패턴 규칙을 사용 make
하면 다음과 같이 동일한 컴파일 명령이 필요한 여러 c 파일을 컴파일 할 수 있습니다 .
objects = program1 program2
all: $(objects)
$(objects): %: %.c
$(CC) $(CFLAGS) -o $@ $<
all: program1 program2
program1:
gcc -Wall -o prog1 program1.c
program2:
gcc -Wall -o prog2 program2.c
all: program1 program2
program1:
gcc -Wall -ansi -pedantic -o prog1 program1.c
program2:
gcc -Wall -ansi -pedantic -o prog2 program2.c
나는 오히려 당신의 프로그램을 더 잘 제어하는 ansi와 pedantic입니다. 경고가있는 동안에는 컴파일 할 수 없습니다!
A simple program's compilation workflow is simple, I can draw it as a small graph: source -> [compilation] -> object [linking] -> executable. There are files (source, object, executable) in this graph, and rules (make's terminology). That graph is definied in the Makefile.
When you launch make, it reads Makefile, and checks for changed files. If there's any, it triggers the rule, which depends on it. The rule may produce/update further files, which may trigger other rules and so on. If you create a good makefile, only the necessary rules (compiler/link commands) will run, which stands "to next" from the modified file in the dependency path.
Pick an example Makefile, read the manual for syntax (anyway, it's clear for first sight, w/o manual), and draw the graph. You have to understand compiler options in order to find out the names of the result files.
The make graph should be as complex just as you want. You can even do infinite loops (don't do)! You can tell make, which rule is your target, so only the left-standing files will be used as triggers.
Again: draw the graph!.
SRC = a.cpp b.cpp
BIN = $(patsubst %.cpp,%,$(SRC))
all: $(BIN)
clean:
rm -f $(BIN)
.PHONY: all clean
make all
will do:
c++ a.cpp -o a
c++ b.cpp -o b
If you set CXX
and CXXFLAGS
variables make
will use them.
ReferenceURL : https://stackoverflow.com/questions/5950395/makefile-to-compile-multiple-c-programs
'development' 카테고리의 다른 글
명령 행 인수로 프로그램 실행에 Ant 사용 (0) | 2021.01.06 |
---|---|
Div 테이블 셀 세로 정렬이 작동하지 않음 (0) | 2021.01.06 |
Rails 컨트롤러에서 HTTP 204를 반환하는 방법 (0) | 2021.01.06 |
Django에서 동일한 (이전) 페이지로 리디렉션 / 돌아 가기? (0) | 2021.01.06 |
Android 빈 선형 레이아웃 콘텐츠 (0) | 2021.01.05 |