views:

411

answers:

2

Hi,

which compiling a multithreaded program we use gcc like below:

gcc -lpthread -D_REENTRANT -o someprogram someprogram.c

what exactly is the flag -D_REENTRANT doing over here?

A: 

It simply defined _REENTRANT for the preprocessor. Somewhere in the associated code, you'll probably find #ifdef _REENTRANT or #if defined(_REENTRANT) in at least a few places.

Also note that the name "_REENTRANT: is in the implementer's name space (any name starting with an underscore followed by another underscore or a capital letter is), so defining it means you've stepped outside what the standard defines (at least the C or C++ standards).

Jerry Coffin
Could you please be more specific.may an example will help me understand better.
Vijay Sarathi
+1  A: 

Defining _REENTRANT causes the compiler to use thread safe (i.e. re-entrant) versions of several functions in the C library.

You can search your header files to see what happens when it's defined.

JayM