configure.texi
来自「基于4个mips核的noc设计」· TEXI 代码 · 共 1,744 行 · 第 1/5 页
TEXI
1,744 行
@samp{make}. Doing this is more reliable than trying to rebuild thefiles manually, because there are complex order dependencies and it iseasy to forget something.@node Getting Started Example@section ExampleLet's consider a trivial example.Suppose we want to write a simple version of @samp{touch}. Our program,which we will call @samp{poke}, will take a single file name argument,and use the @samp{utime} system call to set the modification and accesstimes of the file to the current time. We want this program to behighly portable.We'll first see what this looks like without using autoconf andautomake, and then see what it looks like with them.@menu* Getting Started Example 1:: First Try.* Getting Started Example 2:: Second Try.* Getting Started Example 3:: Third Try.* Generate Files in Example:: Generate Files.@end menu@node Getting Started Example 1@subsection First TryHere is our first try at @samp{poke.c}. Note that we've written itwithout ANSI/ISO C prototypes, since we want it to be highly portable.@example#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <utime.h>intmain (argc, argv) int argc; char **argv;@{ if (argc != 2) @{ fprintf (stderr, "Usage: poke file\n"); exit (1); @} if (utime (argv[1], NULL) < 0) @{ perror ("utime"); exit (1); @} exit (0);@}@end exampleWe also write a simple @file{Makefile}.@exampleCC = gccCFLAGS = -g -O2all: pokepoke: poke.o $(CC) -o poke $(CFLAGS) $(LDFLAGS) poke.o@end exampleSo far, so good.Unfortunately, there are a few problems.On older Unix systems derived from BSD 4.3, the @samp{utime} system calldoes not accept a second argument of @samp{NULL}. On those systems, weneed to pass a pointer to @samp{struct utimbuf} structure.Unfortunately, even older systems don't define that structure; on thosesystems, we need to pass an array of two @samp{long} values.The header file @file{stdlib.h} was invented by ANSI C, and oldersystems don't have a copy. We included it above to get a declaration of@samp{exit}.We can find some of these portability problems by running@samp{autoscan}, which will create a @file{configure.scan} file which wecan use as a prototype for our @file{configure.in} file. I won't showthe output, but it will notice the potential problems with @samp{utime}and @file{stdlib.h}.In our @file{Makefile}, we don't provide any way to install the program.This doesn't matter much for such a simple example, but a real programwill need an @samp{install} target. For that matter, we will also wanta @samp{clean} target.@node Getting Started Example 2@subsection Second TryHere is our second try at this program.We modify @file{poke.c} to use preprocessor macros to control whatfeatures are available. (I've cheated a bit by using the same macronames which autoconf will use).@example#include <stdio.h>#ifdef STDC_HEADERS#include <stdlib.h>#endif#include <sys/types.h>#ifdef HAVE_UTIME_H#include <utime.h>#endif#ifndef HAVE_UTIME_NULL#include <time.h>#ifndef HAVE_STRUCT_UTIMBUFstruct utimbuf@{ long actime; long modtime;@};#endifstatic intutime_now (file) char *file;@{ struct utimbuf now; now.actime = now.modtime = time (NULL); return utime (file, &now);@}#define utime(f, p) utime_now (f)#endif /* HAVE_UTIME_NULL */intmain (argc, argv) int argc; char **argv;@{ if (argc != 2) @{ fprintf (stderr, "Usage: poke file\n"); exit (1); @} if (utime (argv[1], NULL) < 0) @{ perror ("utime"); exit (1); @} exit (0);@}@end exampleHere is the associated @file{Makefile}. We've added support for thepreprocessor flags we use. We've also added @samp{install} and@samp{clean} targets.@example# Set this to your installation directory.bindir = /usr/local/bin# Uncomment this if you have the standard ANSI/ISO C header files.# STDC_HDRS = -DSTDC_HEADERS# Uncomment this if you have utime.h.# UTIME_H = -DHAVE_UTIME_H# Uncomment this if utime (FILE, NULL) works on your system.# UTIME_NULL = -DHAVE_UTIME_NULL# Uncomment this if struct utimbuf is defined in utime.h.# UTIMBUF = -DHAVE_STRUCT_UTIMBUFCC = gccCFLAGS = -g -O2ALL_CFLAGS = $(STDC_HDRS) $(UTIME_H) $(UTIME_NULL) $(UTIMBUF) $(CFLAGS)all: pokepoke: poke.o $(CC) -o poke $(ALL_CFLAGS) $(LDFLAGS) poke.o.c.o: $(CC) -c $(ALL_CFLAGS) poke.cinstall: poke cp poke $(bindir)/pokeclean: rm poke poke.o@end exampleSome problems with this approach should be clear.Users who want to compile poke will have to know how @samp{utime} workson their systems, so that they can uncomment the @file{Makefile}correctly.The installation is done using @samp{cp}, but many systems have an@samp{install} program which may be used, and which supports optionalfeatures such as stripping debugging information out of the installedbinary.The use of @file{Makefile} variables like @samp{CC}, @samp{CFLAGS} and@samp{LDFLAGS} follows the requirements of the GNU standards. This isconvenient for all packages, since it reduces surprises for users.However, it is easy to get the details wrong, and wind up with aslightly nonstandard distribution.@node Getting Started Example 3@subsection Third TryFor our third try at this program, we will write a @file{configure.in}script to discover the configuration features on the host system, ratherthan requiring the user to edit the @file{Makefile}. We will also writea @file{Makefile.am} rather than a @file{Makefile}.The only change to @file{poke.c} is to add a line at the start of thefile:@smallexample#include "config.h"@end smallexampleThe new @file{configure.in} file is as follows.@exampleAC_INIT(poke.c)AM_INIT_AUTOMAKE(poke, 1.0)AM_CONFIG_HEADER(config.h:config.in)AC_PROG_CCAC_HEADER_STDCAC_CHECK_HEADERS(utime.h)AC_EGREP_HEADER(utimbuf, utime.h, AC_DEFINE(HAVE_STRUCT_UTIMBUF))AC_FUNC_UTIME_NULLAC_OUTPUT(Makefile)@end exampleThe first four macros in this file, and the last one, were describedabove; see @ref{Write configure.in}. If we omit these macros, then whenwe run @samp{automake} we will get a reminder that we need them.The other macros are standard autoconf macros.@table @samp@item AC_HEADER_STDCCheck for standard C headers.@item AC_CHECK_HEADERSCheck whether a particular header file exists.@item AC_EGREP_HEADERCheck for a particular string in a particular header file, in this casechecking for @samp{utimbuf} in @file{utime.h}.@item AC_FUNC_UTIME_NULLCheck whether @samp{utime} accepts a NULL second argument to set thefile change time to the current time.@end tableSee the autoconf manual for a more complete description.The new @file{Makefile.am} file is as follows. Note how simple this iscompared to our earlier @file{Makefile}.@examplebin_PROGRAMS = pokepoke_SOURCES = poke.c@end exampleThis means that we should build a single program name @samp{poke}. Itshould be installed in the binary directory, which we called@samp{bindir} earlier. The program @samp{poke} is built from the sourcefile @file{poke.c}.We must also write a @file{acconfig.h} file. Besides @samp{PACKAGE} and@samp{VERSION}, which must be mentioned for all packages which useautomake, we must include @samp{HAVE_STRUCT_UTIMBUF}, since we mentionedit in an @samp{AC_DEFINE}.@example/* Name of package. */#undef PACKAGE/* Version of package. */#undef VERSION/* Whether utime.h defines struct utimbuf. */#undef HAVE_STRUCT_UTIMBUF@end example@node Generate Files in Example@subsection Generate FilesWe must now generate the other files, using the following commands.@smallexampleaclocalautoconfautoheaderautomake@end smallexampleWhen we run @samp{autoheader}, it will remind us of any macros we forgotto add to @file{acconfig.h}.When we run @samp{automake}, it will want to add some files to ourdistribution. It will add them automatically if we use the@samp{--add-missing} option.By default, @samp{automake} will run in GNU mode, which means that itwill want us to create certain additional files; as of this writing, itwill want @file{NEWS}, @file{README}, @file{AUTHORS}, and@file{ChangeLog}, all of which are files which should appear in astandard GNU distribution. We can either add those files, or run@samp{automake} with the @samp{--foreign} option.Running these tools will generate the following files, all of which aredescribed in the next chapter.@itemize @bullet@item@file{aclocal.m4}@item@file{configure}@item@file{config.in}@item@file{Makefile.in}@item@file{stamp-h.in}@end itemize@node Files@chapter FilesAs was seen in the previous chapter, the GNU configure and build systemuses a number of different files. The developer must write a few files.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?