[英]Makefile to compile multiple C programs?
這是一個非常簡單的問題,但我是 makefile 的新手。 我正在嘗試制作一個可以編譯兩個獨立程序的 makefile:
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
模式規則允許您使用make
編譯需要相同編譯命令的多個 c 文件,如下所示:
objects = program1 program2
all: $(objects)
$(objects): %: %.c
$(CC) $(CFLAGS) -o $@ $<
############################################################################
# '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
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,更好地控制你的程序。 當你仍然有警告時,它不會讓你編譯!!
一個簡單的程序的編譯流程很簡單,我可以把它畫成一個小圖:source -> [compilation] -> object [linking] -> executable。 該圖中有文件(源、對象、可執行文件)和規則( make的術語)。 該圖在Makefile 中定義。
當您啟動 make 時,它會讀取Makefile並檢查更改的文件。 如果有,它會觸發規則,這取決於它。 該規則可能會產生/更新更多文件,這可能會觸發其他規則等。 如果您創建了一個好的 makefile,則只會運行必要的規則(編譯器/鏈接命令),即從依賴路徑中修改后的文件“到下一個”。
選擇一個示例Makefile ,閱讀語法手冊(無論如何,第一眼就很清楚,沒有手冊),然后繪制圖形。 您必須了解編譯器選項才能找出結果文件的名稱。
制作圖應該像您想要的那樣復雜。 你甚至可以做無限循環(不要做)! 你可以告訴make ,哪個規則是你的目標,所以只有左邊的文件將被用作觸發器。
再次:繪制圖形!。
這將在make
將所有*.c
文件編譯為沒有.c
擴展名的可執行文件,如gcc program.c -o program
。
make
將自動添加您添加到CFLAGS
任何標志,如CFLAGS = -g Wall
。
如果您不需要任何標志CFLAGS
可以留空(如下)或完全省略。
SOURCES = $(wildcard *.c)
EXECS = $(SOURCES:%.c=%)
CFLAGS =
all: $(EXECS)
SRC = a.cpp b.cpp
BIN = $(patsubst %.cpp,%,$(SRC))
all: $(BIN)
clean:
rm -f $(BIN)
.PHONY: all clean
make all
會做:
c++ a.cpp -o a
c++ b.cpp -o b
如果您設置CXX
和CXXFLAGS
變量, make
將使用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.