Using The g++ Compiler Under Linux

MT262: Putting Computer Systems to Work (C++)


Using the g++ compiler under Linux



  • To compile source code (g++

    • No errors:

      • Source code will be without error and the executable will be written, returning you back to the command prompt.

      • An executable file will be created in the current directory called a.out

      • To run the program, type ./a.out

    • Errors:

      • The code will have a few errors in it and a display of the errors will be shown (no executable file will be written and you will be returned to your command prompt).

  • g++ Flag Options:

    • -g

      • g++ -g

      • allow the executable file to be "debugged" using the GDB debugger.

    • -Wall

      • g++ -Wall

      • show ALL the warning messages

      • Output:

        • Only warnings: executable will still be created

        • Any Errors: messages will an executable not be created

    • -o

      • g++ -o proj2.exe

      • allow to rename the executable file from something other than a.out

  • Many Flag Options:

    • g++ -g -Wall -o proj2

      • This command will compile the source code file such that the executable will be able to be used with the GDB debugger (-g), will show all warning messages when compiled (-Wall), and will create the executable into a file called proj2 (-o proj2).

    • g++ source_file_1 source_file_2 source_file_3...

      • to compile a program that is contained in multiple files and create a single executable

    • g++

      • 3 files: contains the main() function and is the backbone of your program, functions.h contains the prototypes for functions that your program uses, and contains the implementations for those functions. To compile these 3 files into one executable.

      • The above command will then compile the two source files and create one executable a.out.


    • g++ -g -Wall -o proj2

      • This command will compile the two source files and so that the executable can be used with the GDB debugger (-g), all compilation warnings will be displayed (-Wall), and the executable file will be called proj2 (-o proj2).

  • man g++   (for more info).

  • notes:

    • g++ = cc = gcc = gcc++ = c++

    • MĚŞĦǼŁ says: ba3den a3lmek shlon etswey optimze for specific cpu 7ag ur application with gcc

This page comes from
Kuwait Linux User Group

The URL for this page is: