简体   繁体   English

Makefile中的测试命令

[英]Testing command in Makefile

I have written some functions in C and I want to compile them with a Makefile. 我已经用C语言编写了一些函数,并且希望使用Makefile对其进行编译。

Before compiling I want to make a command to test them with another command and then compile them only if they passed the tests. 在编译之前,我想发出一个命令以使用另一个命令对其进行测试,然后仅在它们通过测试时才对其进行编译。

What I am thinking about : 我在想什么:

tests :
   gcc tests.c
all : tests
   gcc *.c

I want to compile the tests.c and then if they are OK to compile all the functions. 我要编译tests.c,然后如果可以的话,可以编译所有功能。

How can I do this ? 我怎样才能做到这一点 ? Thank you very much 非常感谢你

General outline for building realproduct only after tests pass: 仅在测试通过后才可以构建realproduct概述:

all: realproduct

realproduct: | run-tests

run-tests: tests
        ./$<

How does it work? 它是如何工作的?

there's an implicit rules to compile tests.c into tests executable, it will run cc tests.c -o tests and you can modify it with CFLAGS , LDFLAGS , LDLIBS variables. 有一个隐式规则可以将tests.c编译为tests可执行文件,它将运行cc tests.c -o tests ,您可以使用CFLAGSLDFLAGSLDLIBS变量对其进行修改。

$< means first dependency, and ./ is a path to it, so it means run tests executable from local directory $<表示第一个依赖项,而./是它的路径,因此它意味着可以从本地目录运行可执行的tests

| is placed before "order-only dependency", which means that run-tests must be "made" before realproduct , but run-tests is not part of "buildin" realproduct . 放置在“仅订购依赖项”之前,这意味着run-tests必须在realproduct之前“进行”,但是run-tests不是“ buildin” realproduct Thus, cc realproduct.c -o realproduct will be invoked to build. 因此,将调用cc realproduct.c -o realproduct进行构建。

So, in summary, whenever tests.c is changed, first tests will be built, then ./tests will be ran, then realproduct will be built: 所以,总的来说,只要tests.c改变,首先tests将建成,然后./tests会跑了,然后realproduct将建:

$ make
cc     tests.c   -o tests
./tests
cc     realproduct.c   -o realproduct

Finally you can use https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html to make sure some commands are always ran, not only when dependencies have changed. 最后,您可以使用https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html来确保始终运行某些命令,不仅是在依赖项已更改时。

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

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