Using C and C++ at csc or copihue is rather simple. Both machines have GNU's C/C++ installed under /usr/local/bin, a directory that should be in your PATH environment.
To verify the compilers work for you, try the following from your shell prompt:
$ gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.4/2.7.0/specs gcc version 2.7.0 $ g++ -v gcc -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.4/2.7.0/specs gcc version 2.7.0gcc is the C compiler. g++ is the C++ compiler. The compilers should display their versions. If, instead, the operating system responds:
gcc: Command not found.you do not have /usr/local/bin in your PATH environment. Assuming you are using tcsh as your login shell, edit the file named .cshrc in your home directory and add /usr/local/bin to your PATH. Log out, log in, and try the above example again.
Since the C and the C++ compilers use the same options and produce similar results, we will use the C compiler as example in this section. If you are interested in C++, just replace the gcc in this examples with g++.
Create the following C program in a file named test.c:
#includeTo compile this program, type the following at the shell prompt:main() { printf("Hello World!\n"); }
$ gcc test.c $If all goes well, you should get the shell prompt back with no error or warning messages. Check the contents of the directory (use ls) and you will see a file named a.out has been created. This is the result of the compilation and is, therefore, the executable code. To run the program, simpy type a.out at the shell prompt.
$ a.out Hello World! $
Now suppose you want to compile the same test.c program and have the executable be named test (instead of that ugly a.out). To tell the compiler the name of the output executable, use the -o flag. For example:
$ gcc test.c -o test $ test Hello World! $ $ gcc -o test test.c $ test Hello World! $As your programming assignments become more complex you may need to create programs that use multiple files. For example, assume your program is divided among three files: main.c, stack.c, and routines.c. To compile all this pieces into one executable named myprog you would write:
$ gcc main.c stack.c routines.c -o myprog $(of course, if you do not use the -o flag, the executable will be named a.out).
A problem with the above method of compiling programs divided among several files is that all three files will be recompiled every time. Even if you only did a small modification in, say, stack.c, all three files will be recompiled and you must wait for this to end before you can run the program. A better way of dealing with multiple source code files is to compile each one to object code and then link them all into an executable. This way, if you change only one file, you will compile that one file into an object code and then link the new object code with the others to create the executable.
The C compiles (and C++ compiler) serve both as the object code compiler and as the linker. To compile a C program into object code, use the -c flag. The output will be a program with the same name as the input .c file with the .c suffix replaced with .o. We call the .o files object files (or just dot-oh files). Here is what you would do in this example:
$ gcc -c main.c $ gcc -c stack.c $ gcc -c routines.c $The result is three .o files: main.o, stack.o, and routines.o. To link them into the executable, you write:
$ gcc main.o stack.o routines.o -o myprogThe compiler is smart enough to know that you are passing as argument object files and, thus, it needs to link them.
Although it seems you write a lot more compiling this way, you save considerable compilation time in big programs. The compilation process can also be automated using Makefiles. Makefiles include a number of rules on how to build (compile) different files. Knowledge of this compilation and linking trick is key to writing efficient makefiles.
Home | Information | Academics | People | Resources | Contact
The Department of Computer Science at the University of Illinois at Springfield
Copyright© 2002 University of Illinois at Springfield