非常簡單的問題,但我是新來makefiles,並試圖使makefile,將編譯兩個獨立的程序。網上的所有例子都比我需要更多的細節,令人困惑!Makefile可以編譯多個C程序?
我有以下幾點:
program1:
gcc -o prog1 program1.c
program2
gcc -o prog2 program2.c
我真正想要做的是運行兩個GCC線。我究竟做錯了什麼?
非常簡單的問題,但我是新來makefiles,並試圖使makefile,將編譯兩個獨立的程序。網上的所有例子都比我需要更多的細節,令人困惑!Makefile可以編譯多個C程序?
我有以下幾點:
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
請注意,如果您希望自動構建這兩個程序,all'目標必須出現在makefile中的各個程序之前,並且是makefile中的第一個目標。 – 2014-01-08 02:15:29
你可以把'all'或任何其他目標放在任何你想要的地方,並且使用'.DEFAULT_GOAL:= all'來使它成爲默認的無目標(你稱之爲「自動」)運行'make'。 – 2015-09-19 21:08:58
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和迂腐,你的程序更好的控制。它不會讓你編譯,而你仍然有警告!
一個簡單的程序編譯工作流程很簡單,我可以把它繪製成一個小圖:source-> [compilation] - > object [linking] - > executable。在此圖中有文件(來源,對象,可執行文件)和規則(使得的術語)。該圖在Makefile中定義。
當您啓動make時,它會讀取Makefile,並檢查更改的文件。如果有的話,它會觸發規則,這取決於它。 規則可以產生/更新文件,其可以觸發其他規則等等。如果你創建了一個好的生成文件,只有必要的規則(編譯器/鏈接命令)纔會運行,它依賴於路徑中的修改文件。
選擇一個示例生成文件,讀取語法手冊(無論如何,很明顯對於乍一看,W/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,[email protected])
ifeq ($(PRG_SUFFIX_FLAG),0)
BIN = $(patsubst %$(PRG_SUFFIX),%,[email protected])
else
BIN = [email protected]
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
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
會使用它們。
你的意思是'a.cpp -o a'? – 2017-03-09 22:14:51
是的,謝謝。固定。 – 2017-03-10 03:45:58
Pattern rules讓您編譯需要使用make
相同的編譯命令多個C文件如下:
objects = program1 program2
all: $(objects)
$(objects): %: %.c
$(CC) $(CFLAGS) -o [email protected] $<
答案波紋管是很清楚的,但你也可以只運行命令行:'使程序1 program2'這將在你給自己的例子中引用這兩個目標。如果你只是想'program1',你可以運行'make'(它會運行第一個目標)。如果你只想'program2',運行'make program2'。你有更多的控制權。當然,一個目標'all:program1 program2'就能做到這一點(作爲第一個,運行其他2個目標)。完成。 – 2016-06-16 05:31:44