Skip to main content

1.7) Using The GNU C++ Compiler


The GNU C++ compiler can be run using the command g++. Here are two examples of invoking g++ on the file hello.cpp:

g++ hello.cpp -o hello
g++ -Wall -g hello.cpp -o hello
  • -o is used to specify an output filename (.exe is added automatically on Windows)
  • The program still compiles if you omit the -o option, but an unhelpful default name will be used for the output file
  • Various other options can be added to the command line, including
    • -Wall to turn on (almost) all compiler warnings (using this is a good idea!)
    • -g to add debugging information to the compiled code
    • -c to stop after generating object code, before the linking stage (useful when compiling code distributed across multiple files - see later)

Note that the command to invoke the compiler and the options that can be specified to control compilation will depend on the particular compiler that you are using. They are likely to be different if you are not using GNU C++.