You are here: Home Help/User Support Programming (C, FORTRAN, etc.) Writing and Compiling C, C++, Java, or FORTRAN Programs on Unix Systems

Writing and Compiling C, C++, Java, or FORTRAN Programs on Unix Systems

Both free and commercial compilers for C, C++, and FORTRAN are available on all Unix systems in the CAE Lab. The following is a brief introduction to editing, compiling, and running code on those systems.

The Sun workstations (ch208l, ch208m, ch208n, and ch208o) have the free GNU Compiler Collection (gcc) family of compilers, the commercial Sun Forte Compilers, and Sun's Java Development Kit v1.2. The GNU/Linux systems (ch208a, ch226-1, ch226-2, ch226-3, ch226-4, and ch226-5) have gcc, the Portland Group's Cluster Development Kit v4.0, and Blackdown's Java Development Kit 1.3.1.

Editing Source Code

For general usage, we recommend the GNU Emacs text editor (or the XEmacs variant). This works similarly to the Emacs editor on Gemini, and a brief introduction for new users is available.

You can start Emacs by typing:

    emacs

or:

    emacs myfile.f

at the $ prompt, where myfile.f is the name of a FORTRAN program you want to create or edit.

Emacs is language-sensitive: that is, it will automatically configure itself to easily edit source code in C, C++, FORTRAN, or many other languages. It will attempt to determine the type of program from the file's extension.

FORTRAN source code file extensions
.f, .F, .for (.f and .for are preferred - some compilers treat .F files differently)
C source code file extensions
.c, .h
C++ source code file extensions
.C, .c++, .cpp
Java source code file extensions
.java

Refer to the Emacs documentation for more information on the actual usage of Emacs.

Compiling Source Code

Scroll down the following list to find the right Unix system and programming language. Both free and commercial compiler commands are shown if applicable.

Solaris Systems

C
cc -o programname programname.c

gcc -o programname programname.c

C++
CC -o programname programname.C

g++ -o programname programname.C

FORTRAN 77
f77 -o programname programname.f

g77 -o programname programname.f

FORTRAN 90
f90 -o programname programname.f
FORTRAN 95
f95 -o programname programname.f
Java
javac myprogram.java

GNU/Linux Systems

C
pgcc -o programname programname.c

gcc -o programname programname.c

C++
pgCC -o programname programname.C

g++ -o programname programname.C

FORTRAN 77
pgf77 -o programname programname.f

g77 -o programname programname.f

FORTRAN 90
pgf90 -o programname programname.f
FORTRAN 95
pghpf -o programname programname.f
Java
javac myprogram.java

Running Compiled Programs

For any language other than Java, if the compiled executable is in the current working directory:

  ./program-name

Or, if the program is in some other directory:

  /path/to/programs/directory/program-name

For Java, you must run the Java interpreter on your previously-compiled class file:

  java myclass

Make sure you don't include the .class extension that the Java compiler adds to your class file. For further information on other flags you can pass the Java interpreter, type man java.

Parallel Execution of Program Code

One unique feature in the CAE Lab is the universal availability of parallel processor systems. The Sun workstations contain 2 or 4 processors each, and the GNU/Linux systems have 2 each. The following instructions show how to build program code to take advantage of multiple processors.

Solaris Systems

The Sun C, C++, and FORTRAN compilers can enable your programs to use more than one processor on a single system with no modifications to your existing code.

First, use the -autopar flag when you compile the code, for example:

    f77 -autopar -o program-name myprogram.f

Next, set the environment variable PARALLEL equal to the number of processors you'd like to use (normally equal to the number of processors in the workstation):

    export PARALLEL=4

Then run your code as normal. On a four-processor system (ch208l or ch208m), non-parallel code is limited to 25% of the total processing capacity, similar to the following:

    mwr@ch208m:~$ ps -u ss5954 -o 'user pid ppid pcpu comm'
        USER   PID  PPID %CPU COMMAND
      ss5954 26710 26708  0.0 /bin/bash
      ss5954 26686 26680 24.5 ./obs16area26
      ss5954 26696 26690 24.2 ./obs20area26
      ss5954 26680 26678  0.0 /bin/bash
      ss5954 26700 26698  0.0 /bin/bash
      ss5954 26716 26710 24.6 ./obs30area26
      ss5954 26706 26700 24.6 ./obs26area26
      ss5954 26690 26688  0.0 /bin/bash

A similar parallelized program could take up to 100% of the total processing capacity:

    mwr@ch208m:~$ ps -u an6640 -o 'user pid ppid pcpu comm'
        USER   PID  PPID %CPU COMMAND
      an6640 16256 23371 99.0 /home/CAE/an6640/research/TOWHEE
      an6640 16257 23371  0.0 tee
      an6640 23371 23369  0.0 /bin/bash

GNU/Linux Systems

The Portland Group C, C++, and FORTRAN compilers can enable your programs to use more than one processor on a single system with no modifications to your existing code.

First, use the -Mautopar flag when you compile the code:

    pghpf -Mautopar -o program-name myprogram.f

Then, when you run the code, add the flags -pghpf and -np N, where N is the number of processors you'd like to use (normally equal to the number of processors in the system):

    ./program-name -pghpf -np 2
Filed under: , , ,