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
613 views
in Technique[技术] by (71.8m points)

makefile - How to generate a source file when building using autotools

With Make, I do something like

generated.c: input.txt generator
    ./generator input.txt > generated.c

How would I get equivalent functionality out of autotools? (removing generated.c on cleaning would also be a bonus).

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I'm assuming that input.txt is a source file that is distributed, that you do not want to distribute generated.c (i.e., it must be built by each user), and that generator is a program built by your package too.

EXTRA_DIST = input.txt
bin_PROGRAMS = prog
noinst_PROGRAMS = generator

# Source files for the generator
generator_SOURCES = generator.c ...

# Source files for prog
prog_SOURCES = prog.c ...
nodist_prog_SOURCES = generated.c 

# generated.c is a built source that must be cleaned
CLEANFILES = generated.c

# Build rule for generated.c
generated.c: $(srcdir)/input.txt generator$(EXEEXT)
        ./generator$(EXEEXT) $(srcdir)/input.txt

In some situations, like when generating header files, it is important to generate the file before the rest of the sources are compiled, so it can be included. In that case you should also list the generated file in the variable BUILT_SOURCES (The Automake manual has many example in its Built sources section. In the above case it is not necessary.

EDIT: I have fixed the above Makefile.am example to declare generator as a non-installed program.


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

...