Makefile可以编译多个C程序?

这是一个非常简单的问题,但是我是makefile的新手。 我正在尝试制作一个可以编译两个独立程序的makefile:

program1:
    gcc -o prog1 program1.c

program2:
    gcc -o prog2 program2.c

在线上的所有示例都提供了比我所需更多的详细信息,令人困惑! 我真正想要做的就是运行两条gcc行。 我究竟做错了什么?

Sarah asked 2019-11-17T19:54:07Z
7个解决方案
62 votes

像这样做

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
cnicutar answered 2019-11-17T19:54:37Z
21 votes
############################################################################
# '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
Robert answered 2019-11-17T19:54:54Z
19 votes

模式规则使您可以使用make来编译需要相同编译命令的多个c文件,如下所示:

objects = program1 program2
all: $(objects)

$(objects): %: %.c
        $(CC) $(CFLAGS) -o $@ $<
hmofrad answered 2019-11-17T19:55:18Z
10 votes
all: program1 program2

program1:
    gcc -Wall -o prog1 program1.c

program2:
    gcc -Wall -o prog2 program2.c
Erik answered 2019-11-17T19:55:35Z
1 votes
all: program1 program2

program1:
    gcc -Wall -ansi -pedantic -o prog1 program1.c

program2:
    gcc -Wall -ansi -pedantic -o prog2 program2.c

我更喜欢ansi和pedantic,它可以更好地控制您的程序。 当您仍然有警告时,它不会让您编译!

Master C answered 2019-11-17T19:55:59Z
1 votes

一个简单程序的编译工作流程很简单,我可以将其绘制为一个小图:源-> [编译]->对象[链接]->可执行文件。 此图中有文件(源,对象,可执行文件)和规则(make的术语)。 该图在Makefile中定义。

启动make时,它将读取Makefile,并检查更改的文件。 如果有的话,它会触发取决于它的规则。 该规则可能会生成/更新其他文件,这可能会触发其他规则,依此类推。 如果创建了良好的makefile,则仅运行必要的规则(编译器/链接命令),这些规则从依赖路径中的已修改文件“到下一个”。

选择一个示例Makefile,阅读语法手册(无论如何,一目了然,没有手册),然后绘制图形。 您必须了解编译器选项才能找到结果文件的名称。

生成图应尽可能复杂。 您甚至可以执行无限循环! 您可以告诉make,哪个规则是您的目标,因此只有左侧的文件将用作触发器。

再次:绘制图形!

ern0 answered 2019-11-17T19:56:49Z
0 votes
SRC = a.cpp b.cpp
BIN = $(patsubst %.cpp,%,$(SRC))

all: $(BIN)

clean:
    rm -f $(BIN)

.PHONY: all clean

CXX将执行以下操作:

c++     a.cpp   -o a
c++     b.cpp   -o b

如果设置CXXCXXFLAGS变量make将使用它们。

Misha Tavkhelidze answered 2019-11-17T19:57:20Z
translate from https://stackoverflow.com:/questions/5950395/makefile-to-compile-multiple-c-programs