![](/img/trans.png)
[英]C project structure with MakeFile for command-line project with unit testing
[英]Testing command in Makefile
我已经用C语言编写了一些函数,并且希望使用Makefile对其进行编译。
在编译之前,我想发出一个命令以使用另一个命令对其进行测试,然后仅在它们通过测试时才对其进行编译。
我在想什么:
tests :
gcc tests.c
all : tests
gcc *.c
我要编译tests.c,然后如果可以的话,可以编译所有功能。
我怎样才能做到这一点 ? 非常感谢你
仅在测试通过后才可以构建realproduct
概述:
all: realproduct
realproduct: | run-tests
run-tests: tests
./$<
它是如何工作的?
有一个隐式规则可以将tests.c
编译为tests
可执行文件,它将运行cc tests.c -o tests
,您可以使用CFLAGS
, LDFLAGS
, LDLIBS
变量对其进行修改。
$<
表示第一个依赖项,而./
是它的路径,因此它意味着可以从本地目录运行可执行的tests
|
放置在“仅订购依赖项”之前,这意味着run-tests
必须在realproduct
之前“进行”,但是run-tests
不是“ buildin” realproduct
。 因此,将调用cc realproduct.c -o realproduct
进行构建。
所以,总的来说,只要tests.c
改变,首先tests
将建成,然后./tests
会跑了,然后realproduct
将建:
$ make
cc tests.c -o tests
./tests
cc realproduct.c -o realproduct
最后,您可以使用https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html来确保始终运行某些命令,不仅是在依赖项已更改时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.