繁体   English   中英

Makefile编译多个C程序?

[英]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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM