The GNU C Library: Feature Selection

written by: Humberto Mitchson; article published: year 2008, month 01;



In: Categories » Computers and technology » Linux » The GNU C Library: Feature Selection

In glibc, there are a set of feature selection macros that are used to select which standards you wish glibc to comply with. Standards sometimes conflict, and so glibc allows you to select exactly which set of standards (formal, de jure, and informal, de facto) with which to comply, fully or partially. These macros are technically called feature test macros.

You need to be aware of these macros because the default set of macros defined does not provide all the functionality of glibc. A few mechanisms are not available with the default feature set selected; we document the required feature macros to use each of these mechanisms.

The feature test macros are designed to specify with what standards (de jure or de facto), and in some cases precisely which versions of those standards, glibc should comply. This compliance often includes not defining functions and macros beyond what is specified by a standard for header files that are themselves defined by that standard. That means that an application written to conform with a standard can define its own functions and macros without conflicting with extensions not defined by that standard.

The feature test macros do not guarantee that your application is fully compatible with the set of standards specified by the set of macros you define. Setting the feature test macros may find some use of nonportable extensions, but it will not show, for example, use of header files that are entirely unspecified by the standard.

The macros are defined in the system header file feature.h, which you should not include directly. Instead, all other header files that might be affected by the contents of feature.h include it.

The default set of feature macros if none are defined is _SVID_SOURCE=1,_BSD_SOURCE=1,_POSIX_SOURCE=1, and _POSIX_C_SOURCE=199506L. Each option is described in more detail below, but this essentially translates into "support the capabilities of the 1995 POSIX standard, all standard System V features, and all BSD features that do not conflict with System V features." This default set of feature macros suffices for most programs.

When you give gcc the -ansi option, it automatically defines the internal __STRICT_ANSI__ macro, which turns off all the default feature macros.

With the exception of the __STRICT_ANSI__ macro, which is special (and which should be set only by the compiler in the context of the -ansi command line option), these feature macros are cumulative; you can define any combination of them. The exact definition of _BSD_SOURCE changes depending on which other feature macros are set (as documented below); the rest are purely cumulative.

Some of the feature test macros are defined by various versions of POSIX or other standards, some are common in the industry, and others are strictly limited to glibc.

_POSIX_SOURCE If this macro is defined, all the interfaces defined as part of the original POSIX.1 specification are made available.
  This macro was defined by the original POSIX.1-1990 standard.
_POSIX_C_SOURCE This macro supersedes _POSIX_SOURCE. If it is set to 1, it is equivalent to _POSIX_SOURCE. If it is >=2, then it also includes C interfaces defined by POSIX.2, including regular expressions. If it is >=199309L, then it also includes additional C interfaces defined in the 1993 revision of POSIX, particularly including the soft real-time functionality; if it is >=199506L (the default), it also includes additional C interfaces defined in the 1995 revision of POSIX, particularly including POSIX threads. This macro was defined by versions of POSIX released after 1990 in order to differentiate support for various versions of the POSIX (and now also Single Unix) standards. It is largely superseded by _XOPEN_SOURCE.
_XOPEN_SOURCE The _XOPEN_SOURCE macro is defined by the XSI portion of the Single Unix Standard, and defines a logical superset of the interfaces included by _POSIX_C_SOURCE. It was also defined by XPG. If it is defined at all, base-level conformance with XPG4 (Unix95) is included. If it is defined as 500, then base-level conformance with XPG5 (Unix98, SuS version 2) is included. If it is defined as 600, base-level conformance with IEEE Std 1003.1-2003 (the combined POSIX and SuS document) is included.
_ISOC99_SOURCE This feature test macro exports the interfaces defined by the new ISO/IEC C99 standard.
_SVID_SOURCE This feature test macro makes functionality specified by the System V Interface Definition (SVID) available. This does not imply that glibc provides a complete implementation of the SVID standard; it merely exposes the SVID-specified functionality that exists in glibc.
_BSD_SOURCE BSD features can conflict with other features, and the conflicts are always resolved in favor of System Vor standard-compliant behavior if any POSIX, X/Open, or System V feature macro is defined or implied—so the only feature macro that allows BSD behavior to be asserted is _ISOC99_SOURCE. (The exact definition of this feature test macro has changed from time to time, and may change again, since it is not specified by any standard.)
_GNU_SOURCE _GNU_SOURCE turns on everything possible, favoring System V interfaces to BSD interfaces in cases of conflict. It also adds some GNU- and Linux-specific interfaces, such as file leases.


When the standard set of feature test macros will not suffice, the most commonly useful feature macros to define are _GNU_SOURCE (turn everything on—the easiest solution), _XOPEN_SOURCE=600 (most things you are likely to care about, a subset of _GNU_SOURCE), or _ISOC99_SOURCE (use features from the most recent C standard, a subset of _XOPEN_SOURCE=600).

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. Port Forwarding with SSH
Problem: Since many programs use services that send clear-text data over the network, it is desirable to find something that can be used to encrypt the network traffic for these services while minimizing any change to end users. SSH provides this functionality with port forwarding. Port forwarding allows a user to create an encrypted session from a client to a remote server for any TCP-based service by tunneling the service through SSH. Of course, this requires that the user have an account on the remote server and tha...

2. How to use PuTTY Passphrase Agents
STEP1: Use Pageant to store your private keys in memory To make public key authentication more convenient, the developers of PuTTY created Pageant. Pageant is a program included with PuTTY that will keep your decrypted private keys in memory so you only have to enter your passphrase once rather than every time you authenticate to a server using public key authentication. While this will make your day-to-day use more convenient, please keep in mind that it also poses a slight risk, since other applications (inc...

3. Interactively transfer files from the command line with PSFTP
One method to transfer files from the Windows command line is to use PSFTP. PSFTP creates an interactive SFTP file transfer session where you can use many of the commands available within a normal FTP session. Since PSFTP uses the SFTP protocol, which is only available with servers running protocol SSHv2, you may not be able to run it on every server. PSFTP is run from the command line and provides numerous options. To see the options available run PSFTP with the –h option: ...

4. Using Plink to initiate an SSH session from the command line or a script
Using PuTTY from the command line will create an SSH interactive session. This may not be what we want if for example we need to remain at the Windows command line or we want to issue an SSH command from within a script. In order to satisfy these types of needs, PuTTY provides a tool called Plink. Plink is a command line tool that will allow you to log in to a remote machine using SSH and either create an SSH session or execute a command, all from the command line and without opening another window. Plink comes with many comma...

5. How to Generate a Key Pair Using OpenSSH
Problem: How can a key-pair be created in OpenSSH?STEP1: Generating your public/private key-pairThe ssh-keygen command is utilized to generate your public and private keys. OpenSSH provides authentication methods via a choice of three public key "cryptosystems": RSA1, RSA, and DSA. RSA1 works with SSHv1 while RSA and DSA are for SSHv2. RSA and DSA use different techniques for authenticating and have different capabilities, but for purposes of this guide, either will suffice.To create a key-pair, r...

6. Transfer files from the command line with PSCP
A second method to transfer files from a Windows command line prompt is to use PSCP. Unlike PSFTP, PSCP is not interactive and is designed to transfer files "in one shot" and then exit, much like OpenSSH's scp command. PSCP also allows you to specify wildcards within filenames (PSFTP does not). Additionally, PSCP will work with any SSH server as it is not dependent on SSHv2 being present. Note  PSCP will blindly copy files to the remote server, overwriting any files with the same name, without prompting for veri...

7. Create an SSH session from the command line using PuTTY
There are multiple ways to create an SSH session from the command line using PuTTY. The first way involves using the PuTTY program itself. PuTTY comes with a number of options that can be used to invoke the graphical PuTTY terminal from the command line. A description of these options is available within the PuTTY help file. To run PuTTY from the command line: Note  ...

8. Install SSH Windows Clients to Access Remote Machines Securely
Problem: Many times administrators will find themselves on a Windows machine with no way to access a remote server securely since Microsoft does not yet package an SSH client. There are a number of excellent tools available that provide SSH client connectivity from a Windows platform. A list of these tools is available at http://www.openSSH.com/windows.html. ...