1

Use any editor to type your program and then compile it using thegcc compiler:

$ gcc   t1.c –o t1 –lpthread [-D_REENTRANT]
$ ./t1s
Hello world!...
4

2 Answers 2

1

From https://docs.oracle.com/cd/E19455-01/806-5257/compile-4/index.html

Defining _REENTRANT or _POSIX_C_SOURCE

For POSIX behavior, compile applications with the -D_POSIX_C_SOURCE flag set >= 199506L. For Solaris behavior, compile multithreaded programs with the -D_REENTRANT flag. This applies to every module of an application.

For mixed applications (for example, Solaris threads with POSIX semantics), compile with the -D_REENTRANT and -D_POSIX_PTHREAD_SEMANTICS flags.

To compile a single-threaded application, define neither the -D_REENTRANT nor the -D_POSIX_C_SOURCE flag. When these flags are not present, all the old definitions for errno, stdio, and so on, remain in effect.


Note -

Compile single-threaded applications, not linked with either of the thread libraries (libthread.so.1 or libpthread.so.1), without the -D_REENTRANT flag. This eliminates performance degradation incurred when macros, such as putc(3s), are converted into reentrant function calls.


To summarize, POSIX applications that define -D_POSIX_C_SOURCE get the POSIX 1003.1c semantics for the routines listed in Table 7-1. Applications that define only -D_REENTRANT get the Solaris semantics for these routines. Solaris applications that define -D_POSIX_PTHREAD_SEMANTICS get the POSIX semantics for these routines, but can still use the Solaris threads interface.

Applications that define both -D_POSIX_C_SOURCE and -D_REENTRANT get the POSIX semantics.

0

This is taken from the libc 8.2 manual:

Macro: _REENTRANT
Macro: _THREAD_SAFE

These macros are obsolete. They have the same effect as defining _POSIX_C_SOURCE with the value 199506L.

Some very old C libraries required one of these macros to be defined for basic functionality (e.g. getchar) to be thread-safe.

We recommend you use _GNU_SOURCE in new programs. If you don’t specify the ‘-ansi’ option to GCC, or other conformance options such as -std=c99, and don’t define any of these macros explicitly, the effect is the same as defining _DEFAULT_SOURCE to 1.

When you define a feature test macro to request a larger class of features, it is harmless to define in addition a feature test macro for a subset of those features. For example, if you define _POSIX_C_SOURCE, then defining _POSIX_SOURCE as well has no effect. Likewise, if you define _GNU_SOURCE, then defining either _POSIX_SOURCE or _POSIX_C_SOURCE as well has no effect.

Not the answer you're looking for? Browse other questions tagged .