Next: C++ Compiler, Previous: Generic Compiler Characteristics, Up: Compilers and Preprocessors
The following macros provide ways to find and exercise a C Compiler. There are a few constructs that ought to be avoided, but do not deserve being checked for, since they can easily be worked around.
#ifdef __STDC__
/\
* A comment with backslash-newlines in it. %{ %} *\
\
/
char str[] = "\\
" A string with backslash-newlines in it %{ %} \\
"";
char apostrophe = '\\
\
'\
';
#endif
yields
error-->cpp: "foo.c", line 13: error 4048: Non-terminating comment at end of file.
error-->cpp: "foo.c", line 13: error 4033: Missing #endif at end of file.
Removing the lines with solitary backslashes solves the problem.
$ cc a.c b.c
a.c:
b.c:
This can cause problems if you observe the output of the compiler to
detect failures. Invoking `cc -c a.c && cc -c b.c && cc -o c a.o
b.o' solves the issue.
#line support#line directives whose line
numbers are greater than 32767. In addition, nothing in posix
makes this invalid. That is the reason why Autoconf stopped issuing
#line directives.
Determine a C compiler to use. If
CCis not already set in the environment, check forgccandcc, then for other C compilers. Set output variableCCto the name of the compiler found.This macro may, however, be invoked with an optional first argument which, if specified, must be a space separated list of C compilers to search for. This just gives the user an opportunity to specify an alternative search list for the C compiler. For example, if you didn't like the default order, then you could invoke
AC_PROG_CClike this:AC_PROG_CC(cl egcs gcc cc)If the C compiler is not in ANSI C mode by default, try to add an option to output variable
CCto make it so. This macro tries various options that select ANSI C on some system or another. It considers the compiler to be in ANSI C mode if it handles function prototypes correctly.After calling this macro you can check whether the C compiler has been set to accept ANSI C; if not, the shell variable
ac_cv_prog_cc_stdcis set to `no'. If you wrote your source code in ANSI C, you can make an un-ANSIfied copy of it by using the programansi2knr, which comes with Automake. See also underAC_C_PROTOTYPESbelow.If using the GNU C compiler, set shell variable
GCCto `yes'. If output variableCFLAGSwas not already set, set it to -g -O2 for the GNU C compiler (-O2 on systems where GCC does not accept -g), or -g for other compilers.
If the C compiler does not accept the -c and -o options simultaneously, define
NO_MINUS_C_MINUS_O. This macro actually tests both the compiler found byAC_PROG_CC, and, if different, the firstccin the path. The test fails if one fails. This macro was created for GNU Make to choose the default C compilation rule.
Set output variable
CPPto a command that runs the C preprocessor. If `$CC -E' doesn't work, /lib/cpp is used. It is only portable to runCPPon files with a .c extension.Some preprocessors don't indicate missing include files by the error status. For such preprocessors an internal variable is set that causes other macros to check the standard error from the preprocessor and consider the test failed if any warnings have been reported. For most preprocessors, though, warnings do not cause include-file tests to fail unless
AC_PROG_CPP_WERRORis also specified.
This acts like
AC_PROG_CPP, except it treats warnings from the preprocessor as errors even if the preprocessor exit status indicates success. This is useful for avoiding headers that generate mandatory warnings, such as deprecation notices.
The following macros check for C compiler or machine architecture
features. To check for characteristics not listed here, use
AC_COMPILE_IFELSE (see Running the Compiler) or
AC_RUN_IFELSE (see Run Time).
If words are stored with the most significant byte first (like Motorola and SPARC CPUs), execute action-if-true. If words are stored with the least significant byte first (like Intel and VAX CPUs), execute action-if-false.
This macro runs a test-case if endianness cannot be determined from the system header files. When cross-compiling, the test-case is not run but grep'ed for some magic values. action-if-unknown is executed if the latter case fails to determine the byte sex of the host system.
The default for action-if-true is to define `WORDS_BIGENDIAN'. The default for action-if-false is to do nothing. And finally, the default for action-if-unknown is to abort configure and tell the installer which variable he should preset to bypass this test.
If the C compiler does not fully support the ANSI C qualifier
const, defineconstto be empty. Some C compilers that do not define__STDC__do supportconst; some compilers that define__STDC__do not completely supportconst. Programs can simply useconstas if every C compiler supported it; for those that don't, the Makefile or configuration header file will define it as empty.Occasionally installers use a C++ compiler to compile C code, typically because they lack a C compiler. This causes problems with
const, because C and C++ treatconstdifferently. For example:const int foo;is valid in C but not in C++. These differences unfortunately cannot be papered over by defining
constto be empty.If autoconf detects this situation, it leaves
constalone, as this generally yields better results in practice. However, using a C++ compiler to compile C code is not recommended or supported, and installers who run into trouble in this area should get a C compiler like GCC to compile their C code.
If the C compiler recognizes the
restrictkeyword, don't do anything. If it recognizes only a variant spelling (__restrict,__restrict__, or_Restrict), then definerestrictto that. Otherwise, definerestrictto be empty. Thus, programs may simply userestrictas if every C compiler supported it; for those that do not, the Makefile or configuration header defines it away.Although support in C++ for the
restrictkeyword is not required, several C++ compilers do accept the keyword. This macro works for them, too.
If the C compiler does not understand the keyword
volatile, definevolatileto be empty. Programs can simply usevolatileas if every C compiler supported it; for those that do not, the Makefile or configuration header will define it as empty.If the correctness of your program depends on the semantics of
volatile, simply defining it to be empty does, in a sense, break your code. However, given that the compiler does not supportvolatile, you are at its mercy anyway. At least your program will compile, when it wouldn't before.In general, the
volatilekeyword is a feature of ANSI C, so you might expect thatvolatileis available only when__STDC__is defined. However, Ultrix 4.3's native compiler does support volatile, but does not define__STDC__.
If the C compiler supports the keyword
inline, do nothing. Otherwise defineinlineto__inline__or__inlineif it accepts one of those, otherwise defineinlineto be empty.
If the C type
charis unsigned, define__CHAR_UNSIGNED__, unless the C compiler predefines it.
If the C compiler supports a working
long doubletype with more range or precision than thedoubletype, defineHAVE_LONG_DOUBLE.
If the C preprocessor supports the stringizing operator, define
HAVE_STRINGIZE. The stringizing operator is `#' and is found in macros such as this:#define x(y) #y
If function prototypes are understood by the compiler (as determined by
AC_PROG_CC), definePROTOTYPESand__PROTOTYPES. In the case the compiler does not handle prototypes, you should useansi2knr, which comes with the Automake distribution, to unprotoize function definitions. For function prototypes, you should first definePARAMS:#ifndef PARAMS # if PROTOTYPES # define PARAMS(protos) protos # else /* no PROTOTYPES */ # define PARAMS(protos) () # endif /* no PROTOTYPES */ #endifthen use it this way:
size_t my_strlen PARAMS ((const char *));
This macro also defines __PROTOTYPES; this is for the benefit of
header files that cannot use macros that infringe on user name space.
Add -traditional to output variable
CCif using the GNU C compiler andioctldoes not work properly without -traditional. That usually happens when the fixed header files have not been installed on an old system. Since recent versions of the GNU C compiler fix the header files automatically when installed, this is becoming a less prevalent problem.