views:

192

answers:

1

On Solaris, in order to get the msg_control field in struct msghdr and have IPV6_TCLASS I seem to need to define _XPG4_2 and __EXTENSIONS__.

It seems to work if I just define these to 1 before including anything:

#if defined (__SVR4) && defined (__sun)
# define _XPG4_2 1
# define __EXTENSIONS__ 1
#endif
  1. Should I do it this way?
  2. Do I need to define them in all source files, or bad things may happen?
  3. Is there a list of these things somewhere?

This is related to this question.

+1  A: 

man -k XPG4 reveals that there is a standards(5) man page, which lists the feature test macros and library linking info for various standards, including the following:

X/Open CAE To build or compile an application that conforms to one of the X/Open CAE specifications, use the following guidelines. Applications need not set the POSIX feature test macros if they require both CAE and POSIX functionality.

 SUS (XPG4v2)
       The application must define _XOPEN_SOURCE with a value
       other    than    500    (preferably    1)    and   set
       _XOPEN_SOURCE_EXTENDED=1.

Grepping through /usr/include for _XOPEN_SOURCE turns more information in /usr/include/sys/feature_tests.h:

application writers wishing to use any functions specified as X/Open UNIX Extension must define _XOPEN_SOURCE and _XOPEN_SOURCE_EXTENDED=1. The Sun internal macro _XPG4_2 should not be used in its place as unexpected results may occur.

So defining _XPG4_2 yourself is not the way to do it.

If any structure definitions depend on these macros, you would definitely be better off defining them in all translation units. The easiest way to do that is to specify them on the compiler command line:

cc -D_XOPEN_SOURCE=1 -D_XOPEN_SOURCE_EXTENDED=1

If you're using make, you should be able to do this by adding the -D parameters to the CFLAGS variable:

CFLAGS += -D_XOPEN_SOURCE=1 -D_XOPEN_SOURCE_EXTENDED=1
bk1e