documentation:gcc
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
documentation:gcc [2010/02/08 21:24] – mcooper6 | documentation:gcc [2010/02/08 21:26] (current) – mcooper6 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | The GNU Compiler Collection includes front ends for C, C++, Objective-C, | ||
+ | |||
+ | This documentation only scratches the surface on the usage of the GCC/C++ compiler, there are many more options and functionality. | ||
+ | |||
+ | ===== Getting Started ===== | ||
+ | <WRAP info round> | ||
+ | This section contains collected information geared towards folks who are new to GCC and may be feeling | ||
+ | a bit lost in the barbaric jungles of GCC's source code. Although the GCC community is continuously trying | ||
+ | to improve GCC's internal modularity and interfaces, the fact remains that GCC is about 20 years old, fairly large | ||
+ | and needs to keep working on the multitude of supported architectures and languages. So, getting to | ||
+ | work with GCC can be a challenge. | ||
+ | </ | ||
+ | Everyone is welcome to add links to tutorials, HOWTOs, cheat sheets, etc that may be floating around the net. | ||
+ | |||
+ | ==== Example 1: Compiling a simple program ==== | ||
+ | Consider the following example: Let " | ||
+ | |||
+ | #include " | ||
+ | int main() | ||
+ | { | ||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | The standard way to compile this program is with the command | ||
+ | <cli> | ||
+ | Lab46:~$ g++ hello.C -o hello | ||
+ | </ | ||
+ | This command compiles hello.C into an executable program named " | ||
+ | Alternatively, | ||
+ | |||
+ | <cli> | ||
+ | Lab46:~$ g++ -c hello.C | ||
+ | Lab46:~$ g++ hello.o -o hello | ||
+ | </ | ||
+ | The end result is the same, but this two-step method first compiles hello.C into a machine code file named " | ||
+ | |||
+ | |||
+ | |||
+ | ==== Example 2: Compiling a program with multiple source files ==== | ||
+ | |||
+ | If the source code is in several files, say " | ||
+ | <cli> | ||
+ | Lab46:~$ g++ file1.C file2.C -o myprog | ||
+ | </ | ||
+ | The same result can be achieved using the following three commands: | ||
+ | <cli> | ||
+ | Lab46:~$ g++ -c file1.C | ||
+ | Lab46:~$ g++ -c file2.C | ||
+ | Lab46:~$ g++ file1.o file2.o -o myprog | ||
+ | </ | ||
+ | The advantage of the second method is that it compiles each of the source files separately. If, for instance, the above commands were used to create " | ||
+ | <cli> | ||
+ | Lab46:~$ g++ -c file1.C | ||
+ | Lab46:~$ g++ file1.o file2.o -o myprog | ||
+ | </ | ||
+ | Note that file2.C does not need to be recompiled, so the time required to rebuild myprog is shorter than if the first method for compiling myprog were used. When there are numerous source file, and a change is only made to one of them, the time savings can be significant. This process, though somewhat complicated, | ||
+ | |||
+ | ==== Finding and Dealing With Errors ==== | ||
+ | |||
+ | The fact that the compiler gives warnings is very helpful in debugging code. The example below contains a common error, a missing semicolon. | ||
+ | |||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 using namespace std; | ||
+ | 4 | ||
+ | 5 int main() | ||
+ | 6 { | ||
+ | 7 cout << "Hello World!" | ||
+ | 8 | ||
+ | 9 } | ||
+ | |||
+ | |||
+ | Attempting compilation will give you the following error. | ||
+ | |||
+ | <cli> | ||
+ | Lab46:~$ g++ test1.cc -o test1 | ||
+ | test1.cc: In function 'int main()': | ||
+ | test1.cc:8: error: expected `;' before ' | ||
+ | </ | ||
+ | As you can see, the compiler points out that there is a missing semicolon in function main() at or around line 8 (errors can and do cascade down) before the return statement. | ||
+ | |||
+ | The corrected code is: | ||
+ | |||
+ | 1 #include < | ||
+ | 2 | ||
+ | 3 using namespace std; | ||
+ | 4 | ||
+ | 5 int main() | ||
+ | 6 { | ||
+ | 7 cout << "Hello World!" | ||
+ | 8 | ||
+ | 9 } | ||
+ | |||
+ | When this is now compiled, the compiler will execute and exit, without any resulting output. | ||
+ | |||
+ | Testing the newly compiled program results in this: | ||
+ | <cli> | ||
+ | Lab46:~$ ./test1 | ||
+ | Hello World! | ||
+ | </ | ||
+ | Great success! | ||