G++ is gnu's c++ compiler. It is the compiler that we
will use for all examples and is the only compiler for which we will provide
support. G++ is also the compiler that we will use to test and evaluate all
of your programming assignments. Even if you use a different compiler for
your code development, you must always move the project to your CS account and perform
the final tests there. Use g++ to ensure of correctness in the
environment your "customer" (that's us) will be using.
Compiling a single file program is very easy. To try
this, first get in your
Most coding projects will involve multiple files. When working with multiple files the question of how to include properly files always comes up. As you probably know there are two standard ways to include files. They look like:
#include <file1.h> // (1)You are used to seeing style (1) used for standard libraries. In early programming courses, often students use style (2) for their own files. For this course, you must use style (1) to receive full credit on your projects. To make this stye work the way you intend it to, you must tell the compiler where to look for the "include" files. This is where the '-I' flag comes in. Anything that follows immediately after the '-I', no spaces, will be searched. If you need to search more than one directory, simply use multiple '-I' statements.
Let's try compiling the multi-file listed on the slides. It is assumed that these files have been created (using a text editor such as Emacs) and are in the current working directory. Below are actual contents of the four files:
// file1.h const int magic_number = 1492; long multiply_by_MN (int number); // file1.cpp #include <file1.h> long multiply_by_MN (int number) { return (long)(number * magic_number); } // main.h constant int loop_n_times = 4; // main.cpp #include <iostream.h> #include <file1.h> #inlcude <main.h> int main() { int sum = 0; for (int i = 0; i < loop_n_times; i++) { sum += mulitply_by_MN (13); } cout << "Final number is" << sum << "\n"; return 0; }
Remember, you can't translate .h files to object or executable code. These are header files used to inform the compiler how to interpret and translate the code in a .cpp file. (An exception is when the file contains template code. Then the header file contains the implementation, so there is no .cpp file. Even in this case, however, you can't make workable object code from the header file.) Compiling the object code would require a statement like g++ -c file.cpp, but that causes the error statement file1.cpp:2: file1.h: No such file or directory.
Why did this happen? When you use the " "'s to include a file, the compiler looks in the current directory to find the files. Basically you're telling the compiler where to find the file. When you use the <>'s to include a file, the compiler searches all the places it expects to find include files. But it doesn't search your current directory by default. That's why you get the error message. To solve this problem, use the '-I' flag like this: g++ -c -I. file.cpp. Now when you do an ls you see the 'file.o' file.
Now, compile 'main.cpp' the same way, g++ -c -I. main.cpp. To create the executable, link the pieces together and name the output, g++ -o sample main.o file.o. This creates an executable named 'sample'. Try running it by typing sample at the command prompt.
Comments: If you change one of the
.cpp files, for instance 'file1.cpp', you need only recompile it and then
relink all the pieces. There is no need to recompile 'main.cpp'.
This is the benefit of using .o files. Finally, be wary of using 'test'
or 'exec' as the name of your executable. There are standard unix
commands that already have these names. They will be called instead
of the ones you created. If you think this type of conflict might
be occurring with some name you have chosen, you can enter ./<filename> to guarantee that your version is
executed.
The man pages are the best place to get
more information about g++. To view the man pages
for g++, type man g++
at the command prompt. G++ is a specialized front end for the
gcc compiler. Gcc compiles c code. If you can't find what you
want in g++, try man gcc.
Most c and many c++ functions also have man pages, so you should get
comfortable using them.