Intro to Using EECS Workstations

Version 2.0
2.8 Compiling C and Java [Back to Homepage]



C Files

First you need a C file to compile. Let's say you have your file already completed, and it's called foo.c. Change into the directory containing that file.

To compile a C file foo.c, use gcc with the following flags:

cory% gcc -g -Wall -o runfoo foo.c

This will try to compile your file foo.c. If errors are found in your code, messages will be displayed and you'll need to fix them before compilation will succeed. Once you get a working compilation, gcc produces an executable file, specified by the name after the -o option (in this case, runfoo).

The -g and the -Wall flags tell gcc to display messages on errors and warnings.

Now, to run your executable, try typing in its name at the prompt. If you get a path error, that just means your path doesn't include the "current directory" (.). You can either add that to your path, or, as a workaround, just type the path of the executable manually like so:

cory% ./runfoo

If you leave out the -o flag, you won't be able to name your executable. In that case, gcc will give it a default name (like a.out).

Important: Remember, when using C, you must recompile your files if you switch between machines that use different architectures (like, if you compiled first on cory, and then switch to pentagon, you wouldn't be able to run the executable you created on cory).

Note: Another useful flag is the -lm flag, which links the Math library for the compiler. You need this when you use math functions, like for your baseconvert lab.


Java Files

Let's say you have a file called foo.java, already complete with Java code and you want to compile it. To do so, you use javac.

cory% javac foo.java

This will try to compile foo.java. If there are errors in your code, you will see messages and you should try to fix them and compile again. Once you getting a working compilation, you will notice a .class file called foo.class in the same directory. To run it, type:

cory% java foo

Notice that you are using java to run the class file foo.class, but you don't include the .class extension in the parameter.

Also note that unlike with C files, you don't have to recompile your Java files when you switch between machines. That is, even if you compiled on cory, you can still run java using the same .class files if you log into pentagon.

©Copyright 2001, Richard Shiao. All rights reserved. Please send comments to: [email protected]
Hosted by www.Geocities.ws

1