C: What is the use of 'extern' in header files? - c

Pardon me if this sounds a question that has been asked many times but I assure you this is a little different.
I use Codeblocks for C programming and lately I have started to wonder why would someone use a header file in C. I understand it is used for declaring and/or defining variables structures. But here is something i tried and now I am confused.
I have a header file named
test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
static int testvar = 233;
extern int one;
extern void show();
#endif // TEST1_H_INCLUDED
and two other files
headertest.c
#include"test1.h"
#include<stdio.h>
int one = 232;
int main()
{
testvar = 12;
printf("The address of one is %p and value is %d",&testvar,testvar);
printf("value of one is %d",one);
show();
return 0;
}
headertest2.c
#include "test1.h"
#include<stdio.h>
extern int one;
extern void show()
{
//extern int one;
testvar = 34;
printf("\nThe address of one is %p and value is %d",&testvar, testvar);
printf("The value of one is %d", one);
}
Now my point is that even if i comment out the declaration
extern int one;
extern void show();
from the header file test1.h I get no warning or error during compilation and execution. When I can directly access int one and void show() from headertest.c because of them having external linkage implicitly then whats the use of Header file here? Why would someone declare some variable or function as extern in a header file that can otherwise be accessed directly!!
Thank you

A header file is used so that you won't repeat yourself. In your example, you didn't need to write
extern int one;
in headertest2.c, because it would already get included in that file via the header file.
Not repeating yourself is not a small thing. Imagine you have a hundred files that use this global variable (one). Do you want to write extern int one in all of them? What if there were 20 of these variables and 50 more functions? What if you wanted to change int to uint32_t?
Surely, duplicating definitions is tedious and error-prone.
Let's take a look at stdio.h for example. Without headers, you would have to copy-paste the following code in every file that wanted to do I/O:
/* Define ISO C stdio on top of C++ iostreams.
Copyright (C) 1991, 1994-2008, 2009, 2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/*
* ISO C99 Standard: 7.19 Input/output <stdio.h>
*/
#ifndef _STDIO_H
#if !defined __need_FILE && !defined __need___FILE
# define _STDIO_H 1
# include <features.h>
__BEGIN_DECLS
# define __need_size_t
# define __need_NULL
# include <stddef.h>
# include <bits/types.h>
# define __need_FILE
# define __need___FILE
#endif /* Don't need FILE. */
#if !defined __FILE_defined && defined __need_FILE
/* Define outside of namespace so the C++ is happy. */
struct _IO_FILE;
__BEGIN_NAMESPACE_STD
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE FILE;
__END_NAMESPACE_STD
#if defined __USE_LARGEFILE64 || defined __USE_SVID || defined __USE_POSIX \
|| defined __USE_BSD || defined __USE_ISOC99 || defined __USE_XOPEN \
|| defined __USE_POSIX2
__USING_NAMESPACE_STD(FILE)
#endif
# define __FILE_defined 1
#endif /* FILE not defined. */
#undef __need_FILE
#if !defined ____FILE_defined && defined __need___FILE
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE __FILE;
# define ____FILE_defined 1
#endif /* __FILE not defined. */
#undef __need___FILE
#ifdef _STDIO_H
#define _STDIO_USES_IOSTREAM
#include <libio.h>
#if defined __USE_XOPEN || defined __USE_XOPEN2K8
# ifdef __GNUC__
# ifndef _VA_LIST_DEFINED
typedef _G_va_list va_list;
# define _VA_LIST_DEFINED
# endif
# else
# include <stdarg.h>
# endif
#endif
#ifdef __USE_XOPEN2K8
# ifndef __off_t_defined
# ifndef __USE_FILE_OFFSET64
typedef __off_t off_t;
# else
typedef __off64_t off_t;
# endif
# define __off_t_defined
# endif
# if defined __USE_LARGEFILE64 && !defined __off64_t_defined
typedef __off64_t off64_t;
# define __off64_t_defined
# endif
# ifndef __ssize_t_defined
typedef __ssize_t ssize_t;
# define __ssize_t_defined
# endif
#endif
/* The type of the second argument to `fgetpos' and `fsetpos'. */
__BEGIN_NAMESPACE_STD
#ifndef __USE_FILE_OFFSET64
typedef _G_fpos_t fpos_t;
#else
typedef _G_fpos64_t fpos_t;
#endif
__END_NAMESPACE_STD
#ifdef __USE_LARGEFILE64
typedef _G_fpos64_t fpos64_t;
#endif
/* The possibilities for the third argument to `setvbuf'. */
#define _IOFBF 0 /* Fully buffered. */
#define _IOLBF 1 /* Line buffered. */
#define _IONBF 2 /* No buffering. */
/* Default buffer size. */
#ifndef BUFSIZ
# define BUFSIZ _IO_BUFSIZ
#endif
/* End of file character.
Some things throughout the library rely on this being -1. */
#ifndef EOF
# define EOF (-1)
#endif
/* The possibilities for the third argument to `fseek'.
These values should not be changed. */
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#if defined __USE_SVID || defined __USE_XOPEN
/* Default path prefix for `tempnam' and `tmpnam'. */
# define P_tmpdir "/tmp"
#endif
/* Get the values:
L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
TMP_MAX The minimum number of unique filenames generated by tmpnam
(and tempnam when it uses tmpnam's name space),
or tempnam (the two are separate).
L_ctermid How long an array to pass to `ctermid'.
L_cuserid How long an array to pass to `cuserid'.
FOPEN_MAX Minimum number of files that can be open at once.
FILENAME_MAX Maximum length of a filename. */
#include <bits/stdio_lim.h>
/* Standard streams. */
extern struct _IO_FILE *stdin; /* Standard input stream. */
extern struct _IO_FILE *stdout; /* Standard output stream. */
extern struct _IO_FILE *stderr; /* Standard error output stream. */
/* C89/C99 say they're macros. Make them happy. */
#define stdin stdin
#define stdout stdout
#define stderr stderr
__BEGIN_NAMESPACE_STD
/* Remove file FILENAME. */
extern int remove (__const char *__filename) __THROW;
/* Rename file OLD to NEW. */
extern int rename (__const char *__old, __const char *__new) __THROW;
__END_NAMESPACE_STD
#ifdef __USE_ATFILE
/* Rename file OLD relative to OLDFD to NEW relative to NEWFD. */
extern int renameat (int __oldfd, __const char *__old, int __newfd,
__const char *__new) __THROW;
#endif
__BEGIN_NAMESPACE_STD
/* Create a temporary file and open it read/write.
This function is a possible cancellation points and therefore not
marked with __THROW. */
#ifndef __USE_FILE_OFFSET64
extern FILE *tmpfile (void) __wur;
#else
# ifdef __REDIRECT
extern FILE *__REDIRECT (tmpfile, (void), tmpfile64) __wur;
# else
# define tmpfile tmpfile64
# endif
#endif
#ifdef __USE_LARGEFILE64
extern FILE *tmpfile64 (void) __wur;
#endif
/* Generate a temporary filename. */
extern char *tmpnam (char *__s) __THROW __wur;
__END_NAMESPACE_STD
#ifdef __USE_MISC
/* This is the reentrant variant of `tmpnam'. The only difference is
that it does not allow S to be NULL. */
extern char *tmpnam_r (char *__s) __THROW __wur;
#endif
#error Omitted due to post length limit
__BEGIN_NAMESPACE_STD
#ifndef __USE_FILE_OFFSET64
/* Get STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
/* Set STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fsetpos (FILE *__stream, __const fpos_t *__pos);
#else
# ifdef __REDIRECT
extern int __REDIRECT (fgetpos, (FILE *__restrict __stream,
fpos_t *__restrict __pos), fgetpos64);
extern int __REDIRECT (fsetpos,
(FILE *__stream, __const fpos_t *__pos), fsetpos64);
# else
# define fgetpos fgetpos64
# define fsetpos fsetpos64
# endif
#endif
__END_NAMESPACE_STD
#ifdef __USE_LARGEFILE64
extern int fseeko64 (FILE *__stream, __off64_t __off, int __whence);
extern __off64_t ftello64 (FILE *__stream) __wur;
extern int fgetpos64 (FILE *__restrict __stream, fpos64_t *__restrict __pos);
extern int fsetpos64 (FILE *__stream, __const fpos64_t *__pos);
#endif
__BEGIN_NAMESPACE_STD
/* Clear the error and EOF indicators for STREAM. */
extern void clearerr (FILE *__stream) __THROW;
/* Return the EOF indicator for STREAM. */
extern int feof (FILE *__stream) __THROW __wur;
/* Return the error indicator for STREAM. */
extern int ferror (FILE *__stream) __THROW __wur;
__END_NAMESPACE_STD
#ifdef __USE_MISC
/* Faster versions when locking is not required. */
extern void clearerr_unlocked (FILE *__stream) __THROW;
extern int feof_unlocked (FILE *__stream) __THROW __wur;
extern int ferror_unlocked (FILE *__stream) __THROW __wur;
#endif
__BEGIN_NAMESPACE_STD
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void perror (__const char *__s);
__END_NAMESPACE_STD
/* Provide the declarations for `sys_errlist' and `sys_nerr' if they
are available on this system. Even if available, these variables
should not be used directly. The `strerror' function provides
all the necessary functionality. */
#include <bits/sys_errlist.h>
#ifdef __USE_POSIX
/* Return the system file descriptor for STREAM. */
extern int fileno (FILE *__stream) __THROW __wur;
#endif /* Use POSIX. */
#ifdef __USE_MISC
/* Faster version when locking is not required. */
extern int fileno_unlocked (FILE *__stream) __THROW __wur;
#endif
#if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
defined __USE_MISC)
/* Create a new stream connected to a pipe running the given command.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *popen (__const char *__command, __const char *__modes) __wur;
/* Close a stream opened by popen and return the status of its child.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int pclose (FILE *__stream);
#endif
#ifdef __USE_POSIX
/* Return the name of the controlling terminal. */
extern char *ctermid (char *__s) __THROW;
#endif /* Use POSIX. */
#ifdef __USE_XOPEN
/* Return the name of the current user. */
extern char *cuserid (char *__s);
#endif /* Use X/Open, but not issue 6. */
#ifdef __USE_GNU
struct obstack; /* See <obstack.h>. */
/* Write formatted output to an obstack. */
extern int obstack_printf (struct obstack *__restrict __obstack,
__const char *__restrict __format, ...)
__THROW __attribute__ ((__format__ (__printf__, 2, 3)));
extern int obstack_vprintf (struct obstack *__restrict __obstack,
__const char *__restrict __format,
_G_va_list __args)
__THROW __attribute__ ((__format__ (__printf__, 2, 0)));
#endif /* Use GNU. */
#if defined __USE_POSIX || defined __USE_MISC
/* These are defined in POSIX.1:1996. */
/* Acquire ownership of STREAM. */
extern void flockfile (FILE *__stream) __THROW;
/* Try to acquire ownership of STREAM but do not block if it is not
possible. */
extern int ftrylockfile (FILE *__stream) __THROW __wur;
/* Relinquish the ownership granted for STREAM. */
extern void funlockfile (FILE *__stream) __THROW;
#endif /* POSIX || misc */
#if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU
/* The X/Open standard requires some functions and variables to be
declared here which do not belong into this header. But we have to
follow. In GNU mode we don't do this nonsense. */
# define __need_getopt
# include <getopt.h>
#endif /* X/Open, but not issue 6 and not for GNU. */
/* If we are compiling with optimizing read this file. It contains
several optimizing inline functions and macros. */
#ifdef __USE_EXTERN_INLINES
# include <bits/stdio.h>
#endif
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/stdio2.h>
#endif
#ifdef __LDBL_COMPAT
# include <bits/stdio-ldbl.h>
#endif
__END_DECLS
#endif /* <stdio.h> included. */
#endif /* !_STDIO_H */
That, is why we have header files.

Providing function declarations in a header will at least protect you from stupid errors like passing arguments of incompatible types.
Moreover, headers are used not only for function declarations. What's about struct/enum definitions, global typedefs, macros, inline functions, etc?
Finally, header can be considered as an external API for your module. One can briefly take a look at a header to understand how to use it without dealing with an implementation (which sometimes is not available at all).

C header files are a way to share global pointers, macros (#define ...), common structure types declared as uninstatated structures or typedefs. One of the biggest uses of a header file is to share function declarations across C modules, as well.

Related

Function declared twice in C header file

I'm aware of the declaration of C header files with #ifdef and the meaning of extern before variables and functions. But recently I've got a third party library for an embedded device with the following scheme:
/* "lib.h" */
#ifndef LIB_H_
#define LIB_H_
#ifdef LIB_C
void function1();
/* ... */
#else
extern void function1();
/* ... */
#endif
#endif /* LIB_H_ */
And additionally I've got a corresponding C source file:
/* lib.c */
#define LIB_C
#include "lib.h"
void function1()
{
/* ... */
}
/* ... */
So here I am and a bit confused. What is the reason to declare all functions twice in the header in this way?
It's either an affectation, or a compatibility hack for some non-conforming or ancient compiler. You don't need the extern version, but using it is also fine, because function declarations are extern by default.
In other words, it's cruft, but maybe someone needs that cruft. We can't know for sure.

Implicit declaration of scandir; alphasort is undeclared

I am trying to use scandir to print a list of files in the current directory. When I try to compile, I am receiving the following errors and warnings:
warning: implicit declaration of function ‘scandir’ [-Wimplicit-function-declaration]
error: ‘alphasort’ undeclared (first use in this function)
note: each undeclared identifier is reported only once for each function it appears in
I am including <dirent.h>, which to my knowledge should define scandir() and all related functions. And I don't see any errors in my code:
#include <dirent.h>
...
int printFiles(){
struct dirent **nameList;
int numOfFiles = scandir(".", &nameList, 0, alphasort);
//TODO print file names
return numOfFiles;
}
....
I am running Ubuntu 12.04, and I'm compiling using gcc with the -c99 flag.
Am I simply overlooking something? I can't figure out why it's failing to compile.
If you use -std=c99, only functions that are strictly a part of the C99 standard are included by the header files. scandir() is not in the C99 standard. Therefore, you have to set a preprocessor variable to ensure that the function prototype is included. For example, the man page for scandir() indicates that setting the _BSD_SOURCE or _SVID_SOURCE preprocessor variables before you do the #include will fix the problem. Or, you can use #define _GNU_SOURCE which will in turn set quite a few different variables for you (including _BSD_SOURCE and _SVID_SOURCE).
Your code will still compile with the warning and work because C allows you to compile with implicitly defined functions, and the linker will correctly link the call to scandir() to the proper function.
what macro you use is decided by the macro in dirent.h on your own computer.usually it located in /usr/include/dirent.h.
find scandir in dirent.h ,you will find the macro which make scandir Invisible.for example in my conmputer,it is "__USE_BSD ,__USE_MISC".
#if defined __USE_BSD || defined __USE_MISC
/* Return the file descriptor used by DIRP. */
extern int dirfd (DIR *__dirp) __THROW __nonnull ((1));
# if defined __OPTIMIZE__ && defined _DIR_dirfd
# define dirfd(dirp) _DIR_dirfd (dirp)
# endif
# ifndef MAXNAMLEN
/* Get the definitions of the POSIX.1 limits. */
# include <bits/posix1_lim.h>
/* `MAXNAMLEN' is the BSD name for what POSIX calls `NAME_MAX'. */
# ifdef NAME_MAX
# define MAXNAMLEN NAME_MAX
# else
# define MAXNAMLEN 255
# endif
# endif
# define __need_size_t
# include <stddef.h>
# ifndef __USE_FILE_OFFSET64
extern int scandir (__const char *__restrict __dir,
struct dirent ***__restrict __namelist,
int (*__selector) (__const struct dirent *),
int (*__cmp) (__const void *, __const void *))
__nonnull ((1, 2));
# else
# ifdef __REDIRECT
extern int __REDIRECT (scandir,
(__const char *__restrict __dir,
struct dirent ***__restrict __namelist,
int (*__selector) (__const struct dirent *),
int (*__cmp) (__const void *, __const void *)),
scandir64) __nonnull ((1, 2));
# else
# define scandir scandir64
# endif
# endif
you know what to do now? don't be worry!it not a nice way to #define this macro directly.
open file features.h(located /usr/include/features.h),search "__USE_MISC" inside,you can see codes below:
#if defined _BSD_SOURCE || defined _SVID_SOURCE
# define __USE_MISC 1
#endif
it tells that if"_BSD_SOURCE "or"_SVID_SOURCE"is defined,then __USE_MISC would be defined auto.
consider Compatibility of you own codes,add statement below(any one or both) at start(before any statement like #include <.h>||".h") of you file which will use scandir().
#define _BSD_SOURCE 1
#define _SVID_SOURCE 1
save your file and make.
try to #include <sys/dir.h> file to use scandir and define extern int alphasort(const void*,const void*); or extern int alphasort(); above your printFiles
also - you should link you program with standard library (hope it is already done)

Understanding fd_set in unix sys/select.h

I am not really fluent in C but I have experience in Java, C#, Python, Rust etc. I am currently trying to wrap fd_set in Rust but I have absolutely no idea how to read this code.
I am only interested in this part:
typedef struct
{
/* XPG4.2 requires this member name. Otherwise avoid the name
from the global namespace. */
#ifdef __USE_XOPEN
__fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else
__fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif
} fd_set;
So far I understood that __fd_mask is just a long int. __NFDBITS just gives me the size of a long int and multiplies it by 8.
But I have no idea what this is doing #define FD_SETSIZE __FD_SETSIZE and there for I don't know what this is doing _fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS]; For me FD_SETSIZE has no value at all. And I have absolutely no idea what # define __FDS_BITS(set) ((set)->__fds_bits) is doing.
I really would like to see how this code would look in java or c# etc
Full header file:
/* `fd_set' type and related macros, and `select'/`pselect' declarations.
Copyright (C) 1996-2003, 2009, 2011 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* POSIX 1003.1g: 6.2 Select from File Descriptor Sets <sys/select.h> */
#ifndef _SYS_SELECT_H
#define _SYS_SELECT_H 1
#include <features.h>
/* Get definition of needed basic types. */
#include <bits/types.h>
/* Get __FD_* definitions. */
#include <bits/select.h>
/* Get __sigset_t. */
#include <bits/sigset.h>
#ifndef __sigset_t_defined
# define __sigset_t_defined
typedef __sigset_t sigset_t;
#endif
/* Get definition of timer specification structures. */
#define __need_time_t
#define __need_timespec
#include <time.h>
#define __need_timeval
#include <bits/time.h>
#ifndef __suseconds_t_defined
typedef __suseconds_t suseconds_t;
# define __suseconds_t_defined
#endif
/* The fd_set member is required to be an array of longs. */
typedef long int __fd_mask;
/* Some versions of <linux/posix_types.h> define this macros. */
#undef __NFDBITS
/* It's easier to assume 8-bit bytes than to get CHAR_BIT. */
#define __NFDBITS (8 * (int) sizeof (__fd_mask))
#define __FD_ELT(d) ((d) / __NFDBITS)
#define __FD_MASK(d) ((__fd_mask) 1 << ((d) % __NFDBITS))
/* fd_set for select and pselect. */
typedef struct
{
/* XPG4.2 requires this member name. Otherwise avoid the name
from the global namespace. */
#ifdef __USE_XOPEN
__fd_mask fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->fds_bits)
#else
__fd_mask __fds_bits[__FD_SETSIZE / __NFDBITS];
# define __FDS_BITS(set) ((set)->__fds_bits)
#endif
} fd_set;
/* Maximum number of file descriptors in `fd_set'. */
#define FD_SETSIZE __FD_SETSIZE
#ifdef __USE_MISC
/* Sometimes the fd_set member is assumed to have this type. */
typedef __fd_mask fd_mask;
/* Number of bits per word of `fd_set' (some code assumes this is 32). */
# define NFDBITS __NFDBITS
#endif
/* Access macros for `fd_set'. */
#define FD_SET(fd, fdsetp) __FD_SET (fd, fdsetp)
#define FD_CLR(fd, fdsetp) __FD_CLR (fd, fdsetp)
#define FD_ISSET(fd, fdsetp) __FD_ISSET (fd, fdsetp)
#define FD_ZERO(fdsetp) __FD_ZERO (fdsetp)
__BEGIN_DECLS
/* Check the first NFDS descriptors each in READFDS (if not NULL) for read
readiness, in WRITEFDS (if not NULL) for write readiness, and in EXCEPTFDS
(if not NULL) for exceptional conditions. If TIMEOUT is not NULL, time out
after waiting the interval specified therein. Returns the number of ready
descriptors, or -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int select (int __nfds, fd_set *__restrict __readfds,
fd_set *__restrict __writefds,
fd_set *__restrict __exceptfds,
struct timeval *__restrict __timeout);
#ifdef __USE_XOPEN2K
/* Same as above only that the TIMEOUT value is given with higher
resolution and a sigmask which is been set temporarily. This version
should be used.
This function is a cancellation point and therefore not marked with
__THROW. */
extern int pselect (int __nfds, fd_set *__restrict __readfds,
fd_set *__restrict __writefds,
fd_set *__restrict __exceptfds,
const struct timespec *__restrict __timeout,
const __sigset_t *__restrict __sigmask);
#endif
/* Define some inlines helping to catch common problems. */
#if __USE_FORTIFY_LEVEL > 0 && defined __GNUC__
# include <bits/select2.h>
#endif
__END_DECLS
#endif /* sys/select.h */
fd_set is used to represent file descriptor set. For example, I need select() to work on 1024 file descriptors, but a long has only 8 bytes, so that's 64 (8 * 8) bits, so naturally fd_set should be represented as an array of long.
typedef struct
{
long fds_bits[1024 / 64];
} fd_set;
Note that this is just an example to demonstrate, but you get the idea, compare this with the real fd_set, and you'll see.

How to apply indent correctly on a line containing BEGIN_C_DECLS macro?

I'm using indent utility to apply code styling on my code. I have problem with the following:
#ifdef __cplusplus
#define BEGIN_C_DECLS extern "C" {
#define END_C_DECLS }
#else /* !__cplusplus */
#define BEGIN_C_DECLS
#define END_C_DECLS
#endif /* __cplusplus */
BEGIN_C_DECLS
int x;
...
END_C_DECLS
and after applying indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs I get:
BEGIN_C_DECLS int x on the same line.
Any ideas how to keep them on separate lines?
Since indent isn't a C parser (it can't be because it can't process #include directives), it uses heuristics to determine which tokens are identifiers or typedef names and which kind of syntactic unit these form. This obviously guesses wrong for what you are doing--mangling C syntax.
What you can do is to wrap the macros like so
/* *INDENT-OFF* */
BEGIN_C_DECLS
/* *INDENT-ON* */
int x;
/* *INDENT-OFF* */
END_C_DECLS
/* *INDENT-ON* */
Another way is through judicious use of the empty preprocessor directive, # (finally it becomes useful for something!):
#
BEGIN_C_DECLS
#
int x;
#
END_C_DECLS
#

GCC option to get statvfs64

I am using statvfs function call on AIX. And using GCC compiler.
I would like statvfs call to resolve to statvfs64 by preprocessor.
Ex: In Solaris, using "-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64" flags with gcc i am resolved to statvfs64.
Could you please help in getting the similar flags on AIX which resolves me to statvfs64 from statvfs.
Thanks & Regards,
Sivaram T
Thanks a lot for immediate response.
Unfortunately there is no "_LARGEFILE64_SOURCE" define on AIX include file.
I come to know the following options
"-maix64 -mpowerpc64" can resolve to the statvfs64. Not sure whether these are right to use or not.
Please find the following sys/statvfs.h file
=================================================
#ifndef _H_STATVFS
#define _H_STATVFS
#ifndef _H_STANDARDS
#include <standards.h>
#endif
#if _XOPEN_SOURCE_EXTENDED==1
#include <strict_stdtypes.h>
#ifndef _H_TYPES
#include <sys/types.h>
#endif
#include <end_strict_stdtypes.h>
#define _FSTYPSIZ 16
#ifdef _ALL_SOURCE
#include <sys/vmount.h>
#define FSTYPSIZ _FSTYPSIZ
#endif
/*
* statvfs system call return structure
*/
struct statvfs {
ulong_t f_bsize; /* preferred file system block size */
ulong_t f_frsize; /* fundamental file system block size */
fsblkcnt_t f_blocks; /* total # of blocks of f_frsize in fs */
fsblkcnt_t f_bfree; /* total # of free blocks */
fsblkcnt_t f_bavail; /* # of blocks available to non super user */
fsfilcnt_t f_files; /* total # of file nodes (inode in JFS) */
fsfilcnt_t f_ffree; /* total # of free file nodes */
fsfilcnt_t f_favail; /* # of nodes available to non super user */
#ifdef _ALL_SOURCE
fsid_t f_fsid; /* file system id */
#else
ulong_t f_fsid; /* file system id */
#ifndef __64BIT__
ulong_t f_fstype; /* file system type */
#endif
#endif /* _ALL_SOURCE */
char f_basetype[_FSTYPSIZ]; /* Filesystem type name (eg. jfs) */
ulong_t f_flag; /* bit mask of flags */
ulong_t f_namemax; /* maximum filename length */
char f_fstr[32]; /* filesystem-specific string */
ulong_t f_filler[16];/* reserved for future use */
};
#define ST_NOSUID 0x0040 /* don't maintain SUID capability */
#define ST_RDONLY 0x0001 /* file system mounted read only */
#define ST_NODEV 0x0080 /* don't allow device access across */
/* this mount */
/*
* Prototypes
*/
#ifdef _NO_PROTO
extern int statvfs();
extern int fstatvfs();
#else
extern int statvfs(const char *__restrict__, struct statvfs *__restrict__);
extern int fstatvfs(int, struct statvfs *);
#endif
/*
* statvfs64 system call return structure
*/
#ifdef _ALL_SOURCE
struct statvfs64 {
blksize64_t f_bsize; /* preferred file system block size */
blksize64_t f_frsize; /* fundamental file system block size */
blkcnt64_t f_blocks; /* total # of blocks of f_frsize in fs */
blkcnt64_t f_bfree; /* total # of free blocks */
blkcnt64_t f_bavail; /* # of blocks available to non super user */
blkcnt64_t f_files; /* total # of file nodes (inode in JFS) */
blkcnt64_t f_ffree; /* total # of free file nodes */
blkcnt64_t f_favail; /* # of nodes available to non super user */
fsid64_t f_fsid; /* file system id */
char f_basetype[FSTYPSIZ]; /* Filesystem type name (eg. jfs) */
ulong_t f_flag; /* bit mask of flags */
ulong_t f_namemax; /* maximum filename length */
char f_fstr[32]; /* filesystem-specific string */
ulong_t f_filler[16];/* reserved for future use */
};
/*
* Prototypes
*/
#ifdef _NO_PROTO
extern int statvfs64();
extern int fstatvfs64();
#else
extern int statvfs64(const char *__restrict__, struct statvfs64 *__restrict__);
extern int fstatvfs64(int, struct statvfs64 *);
#endif
#endif /* _ALL_SOURCE */
#endif /* _XOPEN_SOURCE_EXTENDED */
#endif /* _H_STATVFS */
=================================================
I don't have an AIX system, so I can't tell you the flags to set. However, on Solaris you can view sys/statvfs.h and see how this works, e.g search for statvfs64 and look for the #ifdef blocks surrounding it. You'll see the lines
#if defined(_LARGEFILE64_SOURCE)
typedef struct statvfs64 {
.....
} statvfs64_t;
#endif
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
...
#define statvfs_t statvfs64_t
#define statvfs statvfs64
#define fstatvfs fstatvfs64
#endif
You will be able to do exactly the same thing on AIX. However, AIX may behave differently and not use the pre-processor to switch between the 32 and 64 bit versions.
If it's not obvious to you, then you could post the contents of sys/statvfs.h up here and we can take a look for you.
iirc on AIX you need the _LARGE_FILES token set, which will enable implicit large file support.
-D_LARGE_FILES
If you want to call statvfs64 explicittly, you have to do
-D_LARGE_FILE_API

Resources