René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -

gcc options


Instead of outputting the result of preprocessing, output a rule suitable for make describing the dependencies of the main source file. The preprocessor outputs one make rule containing the object file name for that source file, a colon, and the names of all the included files, including those coming from -include or -imacros command line options.
Consider the following files:
class A {
#include "opt_m_A.h"

A::A() {

#include "opt_m_A.h"

class B : public A {
#include "opt_m_B.h"

B::B() : A() {
#include "opt_m_B.h"

int main() {
  B b;

  return 0;
Now, invoking gcc with
g++ -M opt_m_*.cpp
results in:
opt_m_A.o: opt_m_A.cpp opt_m_A.h
opt_m_B.o: opt_m_B.cpp opt_m_B.h opt_m_A.h
opt_m_main.o: opt_m_main.cpp opt_m_B.h opt_m_A.h


Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. That is, the commas are replaced with spaces.



This is actually an option to the linker, not the compiler.


Appends dir to the list of directories in which the preprocesser searches for include files.


When gcc is used to invoke the linker, gcc adds several command-line arguments, one of which is -lc. This causes the linker to search in the system-supplied standard C library for functions not supplied by your own source files. This option is so frequently needed that gcc passes it to the linker (along with all the command-line arguments we supplied) unless the -nostdlib option is used to force gcc not to.
All options passed are shown with the -v option.


Names the output file.


Adds dir to the directories in which the linker searches for libraries. Unless otherwise instructed, gcc uses shared libraries in preference to static libraries.


Links against (shared library) if found, otherwise against libname.a (static library). If both are not found, an error is emitted.
Note, a library must begin with lib and end in .a (.so?). However, neither lib nor .a must be specified.
Use -L to add search paths for libraries.


Compiles the source file(s), but does not link it/them.


Show compiler version.


Preprocess source file only.
Comments will be discared unless -C is also specified.
Creates #line directives unless -P is also specified.


Keep comments (used together with -E.


If specified, does not generate #line commands.


Doesn't create temporary files for the steps involved in compiling and linking a program. Instead, passes the output of one step through a pipe to the next step.


Non-ansi features (except some Turbo C idioms) will not be compiled. In order to force full ansi conformance, -pedantic must be specified.



Compiles and stops. Output is assembler code (suffix .S).


Suppres warning messages.


Print extra warning messages.


  • -Wimplicit-int and -Wimplicit-function


Does not detected variables that invoke a constructor ?
#include <string>

int main() {
  std::string i;
  int b;
  return b;


The combination of
  • -Wchar-subscripts
  • -Wcomment
  • -Wformat
  • -Wimplicit-int
  • -Wimplicit-function-declaration
  • -Wmain
  • -Wmultichar
  • -Wparentheses
  • -Wreturn-type
  • -Wswitch
  • -Wtrigraphs
  • -Wunused
  • -Wuninitialized
  • -Wreorder (C++ only)
  • -Wtemplate-debugging (C++ only)
  • -Wunknown-pragmas























C++ only



Turn all warnings into errors.



Position independant code, needed for shared libraries.


Position independant code, needed for shared libraries.
I am a bit in the dark what exactly the difference between -fpic and -fPIC is. It seems that -fPIC works always while -fpic produces smaller object files.


This option should not be used when building shared libraries.


Creates debugging information for the gnu debugger (gdb).


Adds additional debugging information suitable for the gnu debugger (gdb).


Optimizes generated code. n is a number, the higher the number, the better the optimization.


Override the file's extension.
For example, the following command treats a.cpp as a c file although its extension indicates a c++ file:
gcc -x c
The following languages are recognized:
  • c
  • c-header
  • cpp-output
  • c++
  • c++-header
  • c++-cpp-output
  • objective-c
  • objective-c-header
  • objc-cpp-output
  • objective-c++ (apply only)
  • objective-c++-header (apply only)
  • assembler
  • assembler-with-cpp
  • ada
  • f77
  • f77-cpp-input
  • ratfor
  • java
  • treelang


Checks for violations of a few of the guidelines in Scott Meyers' Effective C++ book.