Skip to main content

Running C/C++ Programs

Assuming you already have access to the cluster, you can follow these steps to access your desired compiler, move your file to the HPC, schedule your job, and run your code.

Load a Compiler Module

In order to load a module, you can use the 'module load' command followed by the name of the specific compiler you'd like to use. For example, you can use 'module load GCC' to load the default GCC compiler.

To see all available compilers, you can run the command 'module avail'.

Writing/Uploading Your C/C++ Program

If you are planning to write your C/C++ code in a file within one of the HPC's directories, you can do so using the VIM editor. You can create a new file by using the command 'vim yourFileName.c/cpp'. Alternatively, you can follow the VSCode IDE Section in our HPC Access page to learn how to connect your VSCode editor to your HPC files.

For beginners, it is recommended that you use the VSCode editor, as it is easier to debug and has many helpful tools and settings 

If you have an existing file that you'd like to upload to your HPC directory, you can use any of the following file transfer methods found in our File Transfer Tutorial.

Taking SCP for an example, once you have navigated to your desired local and remote HPC directories, you can use 'scp myfile.c username@login.wave.scu.edu:/your/fileDirectory' to move the file over. 

Compiling and Running Code

The next step is to take your loaded compiler and compile your program. Since our demonstration uses GCC, we can run the following example commands:

'gcc -o myFile myFile.c' or 'g++ -o myFile myFile.cpp'

Note: If you need to attach certain libraries for your program, please add the necessary flags (For example: -lm to link math libraries)

To execute the compiled code, simply run './myFile'.

Submitting a Job

At this point, you can schedule your program to run with Slurm either through interactive or batch mode. If you are going to create the script through batch mode, you can create a bash file (For example: job.sh) and fill it with the necessary resources, partitions, files, and execution commands.

You can submit the script using 'sbatch job.sh'.

You can find more details on how to properly schedule a job using the Requesting Resources Page.

When your job is finished, you can check output.txt and error.txt for results and debugging purposes.