Using The g++ Compiler Under Linux


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

 

Using the g++ compiler under Linux

Links:

Summary:

  • To compile source code (g++ proj2.cc)

    • 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 source_file.cc

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

    • -Wall

      • g++ -Wall source_file.cc

      • show ALL the warning messages

      • Output:

        • Only warnings: executable will still be created

        • Any Errors: messages will an executable not be created

    • -o

      • g++ proj2.cc -o proj2.exe

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

  • Many Flag Options:

    • g++ -g -Wall proj2.cc -o proj2

      • This command will compile the source code file proj2.cc 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++ functions.cc proj2.cc

      • 3 files: proj2.cc contains the main() function and is the backbone of your program, functions.h contains the prototypes for functions that your program uses, and functions.cc 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.

      • YOU DO NOT PUT ANY .h FILES IN THE g++ COMMAND! DOING SO WILL GIVE ERRORS!

    • g++ -g -Wall functions.cc proj2.cc -o proj2

      • This command will compile the two source files functions.cc and proj2.cc 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
http://www.q8linux.net

The URL for this page is:
http://www.q8linux.net/modules.php?name=Content&pa=showpage&pid=5