Makefile来编译多个C程序?

非常简单的问题,但我是新的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 
 ############################################################################ # '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 

模式规则让你编译多个需要使用make编译命令的c文件,如下所示:

 objects = program1 program2 all: $(objects) $(objects): %: %.c $(CC) $(CFLAGS) -o $@ $< 

一个简单的程序的编译工作stream程很简单,我可以把它画成一个小graphics:source – > [compilation] – > object [linking] – > executable。 这个图中有文件 (源文件 ,对象文件 ,可执行文件)和规则make的术语)。 该图在Makefile中定义

当你启动make时,它会读取Makefile ,并检查已更改的文件 。 如果有的话,它会触发规则 ,这取决于它。 该规则可能产生/更新更多的文件 ,这可能触发其他规则等等。 如果你创build了一个好的makefile文件,那么只有必要的规则 (编译器/链接命令)才能运行,它依赖于path中的修改文件。

select一个Makefile的例子,阅读手册的语法(无论如何,这是明确的第一眼,无手动),并绘制图表 。 您必须了解编译器选项才能find结果文件的名称。

make图应该尽可能的复杂。 你甚至可以做无限循环(不要这样做)! 你可以告诉make ,哪个规则是你的目标 ,所以只有左侧的文件将被用作触发器。

再次: 画图

 all: program1 program2 program1: gcc -Wall -ansi -pedantic -o prog1 program1.c program2: gcc -Wall -ansi -pedantic -o prog2 program2.c 

我喜欢ansi和迂腐,更好地控制你的程序。 它不会让你编译,而你仍然有警告!

 SRC = a.cpp b.cpp BIN = $(patsubst %.cpp,%,$(SRC)) all: $(BIN) clean: rm -f $(BIN) .PHONY: all clean 

make all努力:

 c++ a.cpp -oa c++ b.cpp -ob 

如果你设置了CXXCXXFLAGSvariables,就会使用它们。