Wednesday, August 13, 2014

Unix Prog: Unix Standardization(4)

1. Feature Test Macros -- _POSIX_C_SOURCE
We can define different macros to let system have different sets of features.

if we only want to compile programs with POSIX.1 features, instead of features defined by system itself, then we should define this macro at the very beginning of code.

explanation:
1) features.h defines how shall the user define the _POSIX_C_SOURCE
2) find all files containing the <feature.h>, it turns out that many header files include this feature file
 ubuntu@ip-172-31-23-227:~$ less /usr/include/features.h  
 ......  
   
 /* These are defined by the user (or the compiler)  
   to specify the desired environment:  
   
   __STRICT_ANSI__   ISO Standard C.  
   _ISOC99_SOURCE    Extensions to ISO C89 from ISO C99.  
   _ISOC11_SOURCE    Extensions to ISO C99 from ISO C11.  
   _POSIX_SOURCE    IEEE Std 1003.1.  
   _POSIX_C_SOURCE   If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2;  
             if >=199309L, add IEEE Std 1003.1b-1993;  
             if >=199506L, add IEEE Std 1003.1c-1995;  
             if >=200112L, all of IEEE 1003.1-2004  
             if >=200809L, all of IEEE 1003.1-2008  
   _XOPEN_SOURCE    Includes POSIX and XPG things. Set to 500 if  
             Single Unix conformance is wanted, to 600 for the  
             sixth revision, to 700 for the seventh revision.  
   
 ......  
   
 ubuntu@ip-172-31-23-227:~$ sudo find /usr/include -name *.h | xargs grep features.h  
 /usr/include/grp.h:#include <features.h>  
 /usr/include/netinet/ether.h:#include <features.h>  
 /usr/include/netinet/udp.h:#include <features.h>  
 /usr/include/netinet/if_ether.h:#include <features.h>  
 ......  

In order to use POSIX.1 feature, we should do:
#define _POSIX_C_SOURCE 200809L
or other values per above explanation.

another way to use POSIX.1 feature:
shell:
Define the -D_POSIX_C_SOURCE to 200809.
 ubuntu@ip-172-31-23-227:~$ gcc -g -D_POSIX_C_SOURCE=200809 options.c -o options.out  
 ubuntu@ip-172-31-23-227:~$   

2. Feature Test Macros -- _X_OPEN_SOURCE
To make the functionality of version 3 of the single UNIX specification available to applications, we need to define the constant _XOPEN_SOURCE to be 600.

Explanation:
_XOPEN_SOURCE's definition in features.h, which is included by many header files
 ubuntu@ip-172-31-23-227:~$ less /usr/include/features.h  
 ......  
 /* These are defined by the user (or the compiler)  
 ......  
   
   _XOPEN_SOURCE    Includes POSIX and XPG things. Set to 500 if  
             Single Unix conformance is wanted, to 600 for the  
             sixth revision, to 700 for the seventh revision.  
   _XOPEN_SOURCE_EXTENDED XPG things and X/Open Unix extensions.  
   
 ......  
   
   lack of any definitions) are defined by the file. */  
   
 ......  
   
 /* If _GNU_SOURCE was defined by the user, turn on all the other features. */  
 #ifdef _GNU_SOURCE  
 # undef _ISOC95_SOURCE  
 # define _ISOC95_SOURCE 1  
 # undef _ISOC99_SOURCE  
 # define _ISOC99_SOURCE 1  
 # undef _ISOC11_SOURCE  
 # define _ISOC11_SOURCE 1  
 # undef _POSIX_SOURCE  
 # define _POSIX_SOURCE 1  
 # undef _POSIX_C_SOURCE  
 # define _POSIX_C_SOURCE    200809L  
 # undef _XOPEN_SOURCE  
 # define _XOPEN_SOURCE 700  
 # undef _XOPEN_SOURCE_EXTENDED  
 # define _XOPEN_SOURCE_EXTENDED 1  
 # undef _LARGEFILE64_SOURCE  
 # define _LARGEFILE64_SOURCE  1  
 # undef _DEFAULT_SOURCE  
 # define _DEFAULT_SOURCE    1  
 # undef _BSD_SOURCE  
 # define _BSD_SOURCE  1  
 # undef _SVID_SOURCE  
 # define _SVID_SOURCE  1  
 # undef _ATFILE_SOURCE  
 # define _ATFILE_SOURCE 1  
 #endif  
   
 ......  

define _XOPEN_SOURCE at the compiler:
 ubuntu@ip-172-31-23-227:~$ gcc -g -D_XOPEN_SOURCE=600 options.c -o options.out  
 ubuntu@ip-172-31-23-227:~$  

3. Feature Test Macros -- c99
To enable the 1999 ISO C extensions in the gcc C compiler, we need to use the -std=c99 option in compiler

explanation:
1) extract the c99 from the header file, it indicates that __BEGIN_NAMESPACE_C99 is defined to be "namespace __c99 {"
2) BEGIN_NAMESPACE_C99 is used in many header files. So by defining std=c99 can affect many features of c programs.
 ubuntu@ip-172-31-23-227:~$ sudo find /usr/include -name *.h | xargs grep c99
 /usr/include/features.h:   options such as `-std=c99', define __STRICT_ANSI__.  If none of
 /usr/include/x86_64-linux-gnu/sys/cdefs.h:   namespace __c99.  The C++ wrapper header take case of adding the
 /usr/include/x86_64-linux-gnu/sys/cdefs.h:# define __BEGIN_NAMESPACE_C99        namespace __c99 {
 ......

 ubuntu@ip-172-31-23-227:~$ sudo find /usr/include -name *.h | xargs grep __BEGIN_NAMESPACE_C99

 ......  
 /usr/include/x86_64-linux-gnu/bits/mathcalls.h:__BEGIN_NAMESPACE_C99  
 ......  
 /usr/include/wctype.h:__BEGIN_NAMESPACE_C99  
 ......  
 /usr/include/stdlib.h:__BEGIN_NAMESPACE_C99  
 ......  
 /usr/include/stdio.h:__BEGIN_NAMESPACE_C99  
 ......  
 /usr/include/wchar.h:__BEGIN_NAMESPACE_C99  
 ......  
 /usr/include/ctype.h:__BEGIN_NAMESPACE_C99  
   

shell:
Use std option to define c99 with gcc compiler.
 ubuntu@ip-172-31-23-227:~$ gcc -g -std=c99 options.c -o options.out  
 ubuntu@ip-172-31-23-227:~$  

4. Primitive Data Types Definition
Normally "types.h" defines all implementation-dependent primitive types.
shell:
1) find how many header files in current system has the name "types.h"
2) List the file content of bits/types.h, it defines most of primitive types
 ubuntu@ip-172-31-23-227:~$ sudo find /usr/include -name types.h  
 /usr/include/asm-generic/types.h  
 /usr/include/linux/types.h  
 /usr/include/x86_64-linux-gnu/sys/types.h  
 /usr/include/x86_64-linux-gnu/bits/types.h  
 /usr/include/x86_64-linux-gnu/asm/types.h  
 /usr/include/rpc/types.h  
 ubuntu@ip-172-31-23-227:~$ less /usr/include/x86_64-linux-gnu/bits/types.h
 ...... 
 /* Fixed-size types, underlying types depend on word size and compiler.  */
 typedef signed char __int8_t;
 typedef unsigned char __uint8_t;
 typedef signed short int __int16_t;
 typedef unsigned short int __uint16_t;
 typedef signed int __int32_t;
 ......

No comments:

Post a Comment