Hello. I am writing an application ( C99 ) and I desire to make it POSIX 2008 compliant for portability reasons.
According to [1]
http://www.opengroup.org/onlinepubs/...l#tag_02_02_01 :
Quote:
8. For the C programming language, shall define _POSIX_C_SOURCE to be 200809L before any header is included
|
According to [2]
http://www.gnu.org/s/libc/manual/htm...st-Macros.html :
Quote:
You should define these macros by using ‘#define’ preprocessor directives at the top of your source code files. These directives must come before any #include of a system header file. It is best to make them the very first thing in the file, preceded only by comments. You could also use the ‘-D’ option to GCC, but it's better if you make the source files indicate their own meaning in a self-contained way.
|
From the two paragraphs quoted above, I gather that each of my C source files ( *.c ) should begin with the following definition:
Code:
#define _POSIX_C_SOURCE 200809L
However, I am unsure what to do in the case of C header files. Assume a "library" written in C99 which we desire to make POSIX 2008 conformant. Assume the library contains (among others) the following files:
1. alpha_interface.h:
Code:
#ifndef ALPHA_INTERFACE_H
#define ALPHA_INTERFACE_H
#include <sys/socket.h>
#include <sys/types.h>
ssize_t alpha(const struct sockaddr *);
#endif
2. alpha_implementation.c:
Code:
#include <sys/socket.h>
#include <sys/types.h>
#include "alpha_interface.h"
ssize_t alpha(const struct sockaddr *s)
{
// implementation
}
3. beta_interface.h:
Code:
#ifndef BETA_INTERFACE_H
#define BETA_INTERFACE_H
#include <sys/socket.h>
#include <sys/types.h>
ssize_t beta(const struct sockaddr *, const struct sockaddr *);
#endif
4. beta_implementation.c:
Code:
#include <sys/socket.h>
#include <sys/types.h>
#include "beta_implementation.h"
ssize_t beta(const struct sockaddr *s1, const struct sockaddr *s2)
{
// implementation
}
Note: the previous are only meant for clarification, they are not simplified versions of files within my program.
According to [1] and [2] all four files should define _POSIX_C_SOURCE before including <sys/socket.h> and <sys/types.h>. Now also assume we have a program which is to use the above described library, as such:
Code:
#include <sys/socket.h>
#include <unistd.h>
#include "alpha_interface.h"
#include "beta_interface.h"
int main(int argc, char *argv)
{
// parse command line options using getopt()
// use alpha()
// use beta()
}
Since the program includes <sys/socket.h> and <unistd.h> it
also has to define _POSIX_C_SOURCE.
The above scenario introduces the following programs:
- Redefinition of _POSIX_C_SOURCE. This can be avoided if, for example, "somewhere" in alpha_interface.h and beta_interface.h the following sequence occures: "#undef _POSIX_C_SOURCE". This could occur either after the last #include directive, or, for symmetry purposes, at the very end of the file. The alpha_interface.h file could end up looking as such:
Code:
#define _POSIX_C_SOURCE 200809L
#ifndef ALPHA_INTERFACE_H
#define ALPHA_INTERFACE_H
#include <sys/socket.h>
#include <sys/types.h>
ssize_t alpha(const struct sockaddr *);
#endif
#undef _POSIX_C_SOURCE
This looks fairly inelegant to me, I'm wondering if there is a better way.
- Changing to a new version of the POSIX standard would require modification of every source file.
I'm curious as to the recommended/usual solutions for these problems, or, if I'm doing something wrong, I would very much appreciate to be told what that
something is.