简体   繁体   English

Makefile编译多个C程序?

[英]Makefile to compile multiple C programs?

This is an incredibly simple question, but I'm new to makefiles.这是一个非常简单的问题,但我是 makefile 的新手。 I am trying to make a makefile that will compile two independent programs:我正在尝试制作一个可以编译两个独立程序的 makefile:

program1:
    gcc -o prog1 program1.c

program2:
    gcc -o prog2 program2.c

All the examples online go into way more details than I need and are confusing!网上的所有例子都比我需要的更详细,而且令人困惑! All I really want it to do is to run the two gcc lines.我真正想要它做的就是运行两条gcc行。 What am I doing wrong?我究竟做错了什么?

Do it like so这样做

all: program1 program2

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

program2: program2.c
    gcc -o program2 program2.c

You said you don't want advanced stuff, but you could also shorten it like this based on some default rules.你说你不想要高级的东西,但你也可以根据一些默认规则像这样缩短它。

all: program1 program2

program1: program1.c
program2: program2.c

Pattern rules let you compile multiple c files which require the same compilation commands using make as follows: 模式规则允许您使用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

I rather the ansi and pedantic, a better control for your program.我更喜欢 ansi 和 pedantic,更好地控制你的程序。 It wont let you compile while you still have warnings !!当你仍然有警告时,它不会让你编译!!

A simple program's compilation workflow is simple, I can draw it as a small graph: source -> [compilation] -> object [linking] -> executable.一个简单的程序的编译流程很简单,我可以把它画成一个小图:source -> [compilation] -> object [linking] -> executable。 There are files (source, object, executable) in this graph, and rules ( make 's terminology).该图中有文件(源、对象、可执行文件)和规则make的术语)。 That graph is definied in the Makefile .该图在Makefile 中定义

When you launch make, it reads Makefile , and checks for changed files .当您启动 make 时,它​​会读取Makefile并检查更改的文件 If there's any, it triggers the rule , which depends on it.如果有,它会触发规则,这取决于它。 The rule may produce/update further files , which may trigger other rules and so on.规则可能会产生/更新更多文件,这可能会触发其他规则等。 If you create a good makefile, only the necessary rules (compiler/link commands) will run, which stands "to next" from the modified file in the dependency path.如果您创建了一个好的 makefile,则只会运行必要的规则(编译器/链接命令),即从依赖路径中修改后的文件“到下一个”。

Pick an example Makefile , read the manual for syntax (anyway, it's clear for first sight, w/o manual), and draw the graph .选择一个示例Makefile ,阅读语法手册(无论如何,第一眼就很清楚,没有手册),然后绘制图形 You have to understand compiler options in order to find out the names of the result files.您必须了解编译器选项才能找出结果文件的名称。

The make graph should be as complex just as you want.制作图应该像您想要的那样复杂。 You can even do infinite loops (don't do)!你甚至可以做无限循环(不要做)! You can tell make , which rule is your target , so only the left-standing files will be used as triggers.你可以告诉make ,哪个规则是你的目标,所以只有左边的文件将被用作触发器。

Again: draw the graph !.再次:绘制图形!。

This will compile all *.c files upon make to executables without the .c extension as in gcc program.c -o program .这将在make将所有*.c文件编译为没有.c扩展名的可执行文件,如gcc program.c -o program

make will automatically add any flags you add to CFLAGS like CFLAGS = -g Wall . make将自动添加您添加到CFLAGS任何标志,如CFLAGS = -g Wall

If you don't need any flags CFLAGS can be left blank (as below) or omitted completely.如果您不需要任何标志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 will do: make all会做:

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

If you set CXX and CXXFLAGS variables make will use them.如果您设置CXXCXXFLAGS变量, make将使用它们。

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

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