Makefile编译多个C程序?

Sar*_*rah 55 c makefile

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

program1:
    gcc -o prog1 program1.c

program2:
    gcc -o prog2 program2.c
Run Code Online (Sandbox Code Playgroud)

所有在线示例都会比我需要的更多细节,让人感到困惑!我真正想做的就是运行两条gcc线.我究竟做错了什么?

cni*_*tar 63

这样做

all: program1 program2

program1: program1.c
    gcc -o program1 program1.c

program2: program2.c
    gcc -o program2 program2.c
Run Code Online (Sandbox Code Playgroud)

你说你不想要高级的东西,但你也可以根据一些默认规则缩短它.

all: program1 program2

program1: program1.c
program2: program2.c
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果您希望自动构建两个程序,则"all"目标必须出现在makefile中的各个程序之前,并且是makefile中的第一个目标. (4认同)
  • 你可以将`all`或任何其他目标放在任何你想要的位置,并使用`.DEFAULT_GOAL:= all`将它作为`make`的无目标(你称之为"自动")运行的默认值. (3认同)

Rob*_*ert 22

############################################################################
# '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
Run Code Online (Sandbox Code Playgroud)

  • "$(OBJS)应该在链接后立即自动删除." 这是否会导致不必要的重新编译,从而大多打败了"make"的观点? (4认同)

hmo*_*rad 21

模式规则允许您使用make以下方法编译需要相同编译命令的多个c文件:

objects = program1 program2
all: $(objects)

$(objects): %: %.c
        $(CC) $(CFLAGS) -o $@ $<
Run Code Online (Sandbox Code Playgroud)

  • @SH' 使用 Make `shell` 命令使用 `find` 的输出来填充 `objects`。例如, `objects := $(shell find . -type f -iname "*.c" -print | tr -d ".c" | sed 's/\///') ` (2认同)
  • @hmofrad 用 'tr' 回答将删除所有匹配的字符 ['.', 'c'],而不是子字符串。修复:`find tests/ -type f -iname '*.cpp' | sed 's/.cpp//g'` (2认同)

Eri*_*rik 10

all: program1 program2

program1:
    gcc -Wall -o prog1 program1.c

program2:
    gcc -Wall -o prog2 program2.c
Run Code Online (Sandbox Code Playgroud)