The gcc utility supports C, whereas g++ also supports C++.
GCC may be operated either from the command line or via a graphical IDE (Integrated Development Environment). Some folks also use it from within the vim or emacs text editors. I will cover the Code::Blocks IDE environment in a future post.
These are operated from the command line using commands such as the following:
Various command line switches such as -o, -g, ... tell it to save to a specific output file name, use the gdb debugger, etc.
The gcc and g++ compilers, by default, output to a file named a.out, unless you use the -o
To compile a program and output to a filename of your choosing, such as "My_File", you would do the following:
gcc file.c -o My_File
To quickly test my compiler, I typed out the ever popular "Hello World" program in my text editor as shown below:
printf("Hello from Karl's Lab!\n");
and saved it as "Hello_Test.c" in my home directory. I wanted an output file called "Hello_1". I also wanted debugger information. Here's the command I used:
gcc Hello_Test.c -g -o Hello_1
The "-g" switch activated the gdb debugger; the -o Hello_1 part told it to name the output file "Hello_1"; "the "Hello_Test.c" part told it the name of the source file to be used.
When I typed "Hello_1" without the quotes at the command line, I received the following output:
Hello from Karl's Lab!
See a Compiler error:
In C, you must use the ";" symbol at the end of a statement. I wanted to see how gcc responds to an actual error, so I removed the ";" symbol from the "printf" line in the code snippet above, then saved and compiled the code as before. Here's the message it returned:
Hello_Test.c: In function ‘main’:
Hello_Test.c:7: error: expected ‘;’ before ‘return’
The first line of information shows the function - "main" - the error occurred in. The second line of output shows which line number the error occurred in and there is a ";" symbol missing in the code. While the actual typo was in line 4 of the code, the 'return' statement at line 7 is where the compiler errored out. With this output, you are at least given somewhere to start in trouble-shooting your code.
See the gdb debugger work:
Suppose your working directory is /home/mike/c_progs, and your executable is My_File. The command you would enter at the command line is:
You will get something like the following:
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...
At this (gdb) prompt, RUN "My_File" by typing "r" (without quotes).
Starting program: /home/mike/c_progs/My_File
Hello from Mike!
Program exited normally.
This shows the program's output (Hello from Mike!), as well as that it executed without any errors or warnings.
When you are finished working, EXIT the gdb prompt by typing "q" without the quotes at the (gdb) prompt. This exits the debugger and returns you to the UNIX shell prompt.
Here are some good links on using gcc and g++ :
GCC's Homepage - Look here for the manuals to whatever version of gcc you have.
Some good info for getting g++ for a Windows machine:
Here are some links to a good tutorial on the gdb debugger:
The GCC Homepage: