g++flags

What stage of compilation should what flags be applied to?


I'd like to make sure i am understanding the g++ documentation correctly.

When working with C/C++ programs via G++, we normally do preprocessing, compilation, assembly and linking.

Depending whether you're building a debug or release version of the code, you may chose to apply different flags to the final product.

Take flag -O3 for example. What stage should this flag be applied to? I assume this should be applied when working to produce the .o file, during compilation. (Is this in documentation somewhere?)

If so, if one wants to produce 2 versions of the program one for release and one for debugging, .o should be produced separately as part of the make file (is this correct?)

In general, G++ help is extensive, how can one find out what part of the process a given flag should be applied to?

When we're talking about optimizations, are we talking about object code?


Solution

  • Take flag -O3 for example. What stage should this flag be applied to?

    Compilation.

    if one wants to produce 2 versions of the program one for release and one for debugging, .o should be produced separately as part of the make file

    Yes.

    Preprocessing might also be affected, e.g. by the definition of macros such as NDEBUG or _GLIBCXX_DEBUG but it's not common to do preprocessing separately anyway: most makefiles invoke the compiler to turn a source file into an object file directly, so the compiler performs preprocessing, compilation and assembly in one step.

    In general, G++ help is extensive, how can one find out what part of the process a given flag should be applied to?

    RTFM, of course!

    The Option Summary lists all options, grouping them into sections.

    Fairly obviously these options control specific stages:

    Options Controlling the Preprocessor
    Passing Options to the Assembler
    Options for Linking

    There are also Options for Directory Search, where header file search paths are used by the preprocessor, library search paths are used by the linker.

    And generally all other options control compilation, or apply to all stages (e.g. -o to set the output file name,) or specify

    When we're talking about optimizations, are we talking about object code?

    Yes.

    There is also a -O flag for the GNU linker, see the ld manual, but that currently only has any effect when linking an ELF shared library and GCC doesn't use it unless you request it explicitly, e.g. via -Wl,-O