Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

c - What is the sequence of wildcard function in Makefile

I created several test*.c files

$ ls
Makefile  test2.c  test3.c  test4.c  test5.c  test6.c  test7.c  test8.c  test9.c

All test*.c have same code except filename and fucntion name

$ cat test2.c
#include <stdio.h>

int test_2_()
{
        return 0;
}
$ cat test3.c
#include <stdio.h>

int test_3_()
{
        return 0;
}
$ cat test4.c
#include <stdio.h>

int test_4_()
{
        return 0;
}

Makefile is very simple, just echo $(src):

$ cat Makefile
src = $(wildcard *.c)
seq:
        @echo $(src)

Now, my question is what's the sequence of wildcard

$ make seq
test2.c test4.c test6.c test3.c test8.c test9.c test5.c test7.c

If it's arranged in alphabetical order, it should be "test2.c test3.c test4.c ...", but actually it's "test2.c test4.c test6.c test3.c test8.c test9.c test5.c test7.c" And it's not follow the file update time order

$ for i in `ls test*`;do echo $i;touch $i;sleep 1;done
test2.c
test3.c
test4.c
test5.c
test6.c
test7.c
test8.c
test9.c
$ make seq
test2.c test4.c test6.c test3.c test8.c test9.c test5.c test7.c
question from:https://stackoverflow.com/questions/65713777/what-is-the-sequence-of-wildcard-function-in-makefile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In older versions of GNU make, the results of wildcard are the order in which the operating system returns those values when the directory is read. That order is for all practical purposes random. It's not actually random: there is an order to it and two runs of make (with no other changes) will return the same order. But that order is not easily predictable: it depends on the type of filesystem, and other things. For most filesystems the order is more or less the order in which the files were created (not updated) but even that is not always sure as new files could re-use emptied slots from deleted files.

As of GNU make 4.3, the wildcard function is guaranteed to return values in sorted order (ASCII order, IIRC).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...