What does the g++ -D flag do?

22,439

Solution 1

It is equivalent to adding the statement #define LINUX 1 in the source code of the file that is being compiled. It does not have any effect on other compilation flags. The reason for this is it's an easy way to enable #ifdef statements in the code. So you can have code that says:

#ifdef LINUX
   foo;
#endif

It will only be enabled if that macro is enabled which you can control with the -D flag. So it is an easy way to enable/disable conditional compilation statements at compile time without editing the source file.

Solution 2

It doesn't have anything to do with -O3. Basically, it means the same as

#define LINUX 1

at the beginning of the compiled file.

Share:
22,439

Related videos on Youtube

Sterling
Author by

Sterling

Updated on July 18, 2022

Comments

  • Sterling
    Sterling almost 2 years

    I am looking at a CFLAGS of -

    CFLAGS=-g -w -D LINUX -O3 -fpermissive
    

    in a Makefile. What does the -D flag do? I see on the man page that

    -D name
        Predefine name as a macro, with definition 1. 
    

    but I don't know how to interpret that. My interpretation is...its making LINUX a macro and only doing -03 and -fpermissive when in a linux environment. Is that right? If not, then what? Thanks for any help

    • Basile Starynkevitch
      Basile Starynkevitch about 12 years
      Often, the -D is glued to the following (defined) name, e.g. -DLINUX or -DFOO=BAR
  • jpm
    jpm about 12 years
    I would +1, but the interpretation is only correct up to a point. The -D flag doesn't affect the following flags (e.g. making them conditional or some such crazy talk), and that's a fairly important distinction.
  • Gabriel Southern
    Gabriel Southern about 12 years
    @jpm that's a good point I had not interpreted the question that way when I first read it, but now I see you are correct. I edited my answer to clarify that point.
  • jpm
    jpm about 12 years
    -O3 sets the compiler's optimization level to 3.