开发者

How does the echo | g++ work in the following script

开发者 https://www.devze.com 2023-03-17 18:23 出处:网络
Based on gcc: Do I need -D_REENTRANT with pthreads? > echo | g++-E -dM -c - > singlethreaded > echo | g++ -pthread -E -dM -c - > multithreaded

Based on gcc: Do I need -D_REENTRANT with pthreads?

> echo | g++          -E -dM -c - > singlethreaded
> echo | g++ -pthread -E -dM -c - > multithreaded
> diff singlethreaded multithreaded
39a40
> #define _REENTRANT 1

The author uses the above command to export the options used by the compiler. I want to know why such a command wor开发者_StackOverflow中文版ks in detail:

> echo | g++ -E -dM -c - > singlethreaded

I understand the following parts:

> singlethread # means to redirect the results to the file singlethread


The -dM option:

Instead of the normal output, generate a list of #define directives for all the macros defined during the execution of the preprocessor, including predefined macros. This gives you a way of finding out what is predefined in your version of the preprocessor.

The -E option:

Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

The -c option causes the compiler to compile but not link. This is superfluous with the -E option.

The solitary - tells the compiler to read its input from stdin.

Thus, the command echoes an empty stream to stdout, pipes that empty stream to g++, which then reads the empty stream but runs only the preprocessor, which has been told to ignore the input (empty) and output the complete list of defined preprocessor macros.


echo | g++ - tells g++ to compile stdin and passes in an empty program (a blank line from echo). The -E option invokes only the preprocessor (it's similar to running cpp directly but with any macros automatically defined by g++ present). The -dM option is a debug flag which dumps out state inside the compiler at a particular step:

       -dM
       -fdump-rtl-mach
           Dump after performing the machine dependent reorganization
           pass, to file.155r.mach.

The purpose of all this is to discover what macros the compiler sets differently when you vary the command line parameter -pthread by comparing that dump output.

0

精彩评论

暂无评论...
验证码 换一张
取 消