简体   繁体   English

如何编写“选择性” Makefile?

[英]How do I write a “selective” Makefile?

noob question here. 菜鸟问题在这里。

I have a directory with a lot of .c files, they're basicely libc functions that I code myself as an exercice. 我的目录中有很多.c文件,它们基本上是libc函数,我将自己编码为执行程序。

I write a little main() in these files to test the functions, and I want to write a Makefile that allow me to compile only the file I want to test, for exemple: 我在这些文件中写了一点main()来测试功能,并且我想编写一个Makefile,例如,我可以只编译我要测试的文件:

make memset.c

And get only the executable of the code wrote in memset.c. 并且仅获取在memset.c中编写的代码的可执行文件。

I tried to do something like this: 我试图做这样的事情:

CC = gcc
CFLAGS = -Wall -Wextra -pedantic

all : %.o
    $(CC) $(CFLAGS) $<
%.o : %.c
    $(CC) $(CFLAGS) $< -c -o $@

But obviously it doens't work. 但是显然它不起作用。 I don't what to put in place of the "all". 我没有什么可以代替“全部”。 I know it's very basic, but I didn't manage to do it, and I did research but didn't find an answer to this specific question. 我知道这是非常基本的,但是我没有做到,我做了研究,但没有找到这个特定问题的答案。

Thanks in advance for your help. 在此先感谢您的帮助。

If you do make -n -p you get a dump of all of the built-in rules in make. 如果执行make -n -p ,则将转储make -n -p中所有内置规则。 In GNU Make 4.1, this includes: 在GNU Make 4.1中,这包括:

%: %.o
#  recipe to execute (built-in):
        $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@

So you might just needs a % in your makefile where you currently have all . 因此,您可能只需要在makefile中当前拥有all文件的位置添加一个%

You also might find that you don't need those rules which are already built in. Suppose you have three C files, each with a main() as you specify: abs.c , div.c and fmax.c . 您还可能会发现不需要那些内置的规则。假设您有三个C文件,每个文件都有您指定的main()abs.cdiv.cfmax.c Your Makefile needs to be no more than two lines: Makefile不得超过两行:

CFLAGS = -Wall -Wextra -pedantic
all: abs div fmax

which would then allow you to do make abs to make the abs executable, and make all to make them all. 然后,您可以make abs使abs可执行, make all变为abs

You can define static pattern rules to build the object files and the executables and then invoke make with the name of the executable you want as the goal: 您可以定义静态模式规则来构建目标文件和可执行文件,然后以您想要作为目标的可执行文件的名称调用make:

CC     = gcc
CFLAGS = -Wall -Wextra -pedantic

SRC := $(wildcard *.c)
OBJ := $(patsubst %.c,%.o,$(SRC))
EXE := $(patsubst %.c,%,$(SRC))

.PHONY: all obj

all: $(EXE)

obj: $(OBJ)

$(EXE): %: %.o
    $(CC) $(LDFLAGS) $< -o $@

$(OBJ): %.o: %.c
    $(CC) $(CFLAGS) $< -c -o $@

.PHONY: clean

clean:
    rm -f $(OBJ) $(EXE)

Then: 然后:

$ make memset.o

builds only memset.o , 仅构建memset.o

$ make memset

builds only memset (and memset.o if needed), 仅构建memset (如果需要,则构建memset.o ),

$ make obj

builds all object files, 构建所有目标文件,

$ make # or make all

builds all executables (and object files if needed), and 构建所有可执行文件(如果需要,还构建目标文件),以及

$ make clean

deletes all executables and object files. 删除所有可执行文件和目标文件。

With wildcard , you can achieve what you want. 使用wildcard ,您可以实现所需的功能。

Note that if each program depends on only one .c file, you don't need %.o rules: 请注意,如果每个程序仅依赖一个.c文件,则不需要%.o规则:

CC = gcc
CFLAGS = -Wall -Wextra -pedantic

SRC  := $(wildcard *.c)
EXEC = $(SRC:%.c=%)

all: $(EXEC)

%: %.c
       $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

And just invoke this way for instance: 并以这种方式调用例如:

make memset

You already have most you to compile the executable selectively: 您已经拥有最多的选择编译可执行文件的能力:

CC = gcc
CFLAGS = -Wall -Wextra -pedantic

%.o : %.c
    $(CC) $(CFLAGS) $< -c -o $@
% : %.o
    $(CC) $(LDLAGS) $< -o $@

Then you just need to call make with the target you want, the executable: 然后,您只需要使用所需的目标(可执行文件)调用make:

make select

If you have several sets of executable with different flags, you can use: 如果您有几套带有不同标志的可执行文件,则可以使用:

EX0 = drink clean
${EXE0}: % : %.o
        $(CC) $(LDLAGS) -lwater $< -o $@
EX1 = burn melt
{EX1}: % : %.o
        $(CC) $(LDLAGS) -lfire  $< -o $@

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

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