---
title: "Cpp, C++ Programming Language"
id: "26934"
type: "epkb_post_type_1"
slug: "cpp-programming-language"
published_at: "2024-04-30T13:44:17+00:00"
modified_at: "2025-01-08T03:57:08+00:00"
url: "https://docs.rc.fas.harvard.edu/kb/cpp-programming-language/"
markdown_url: "https://docs.rc.fas.harvard.edu/kb/cpp-programming-language.md"
excerpt: "Description C++ (C plus plus) is an object-oriented high-level programing language. C++ files typically have .cpp as the file extension. You can compile C++ codes with either GNU compilers (gcc) or Intel compilers (intel). Best Practice We recommend requesting an..."
taxonomy_epkb_post_type_1_category:
  - "Languages"
taxonomy_epkb_post_type_1_tag:
  - "C"
  - "Cpp"
---

# Cpp, C++ Programming Language

## Description

C++ (C plus plus) is an object-oriented high-level programing language. C++ files typically have `.cpp` as the file extension. You can compile C++ codes with either GNU compilers (gcc) or Intel compilers (intel).

## Best Practice

We recommend requesting an [interactive job](https://docs.rc.fas.harvard.edu/kb/running-jobs/#Interactive_jobs_and_salloc)
 to compile a C++ program on a compute node (instead of a login node). The compilation could take up to few seconds to a minute and depending on the complexity of the code. Additionally, it is best to utilize the `test` partition to compile and test a program before executing its production run on the cluster as a batch-job.

It is best practice to compile a C++ code separately and then use the executable, generated during compilation, in the production run using the `sbatch` script. If possible, avoid including the compilation command in the `sbatch` script, which will recompile the program every time the job is submitted. If any changes are made to the source code, compile the source code separately, and then submit the production run as a batch-job.

## Compilers

You can compile a C++ code using either a GNU or an Intel compiler.

### GNU compiler

To use C++ with gcc on the FASRC clusters, load gcc compiler via our [module system](https://docs.rc.fas.harvard.edu/kb/modules-intro/)
. For example, this command will load the latest gcc version:

```
module load gcc
```

If you need a specific version of R, you can search with the command

```
module spider gcc
```

To load a specific version

```
module load gcc/10.2.0-fasrc01
```

For more information on modules, see the [Lmod Modules](https://docs.rc.fas.harvard.edu/kb/modules-intro/)
 page.

To compile a code using a specific version of the GNU compiler and the `O2` [optimization flag](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html)
, you can do the following:

```
module load gcc 
g++ -O2 -o sum.x sum.cpp
```

### **Intel compiler**

To use C++ with Intel on the FASRC clusters, load intel compiler via our [module system](https://docs.rc.fas.harvard.edu/kb/modules-intro/)
. For example, this command will load the latest intel version:

```
module load intel
```

If you need a specific version of R, you can search with the command

```
module spider intel
```

To load a specific version

```
module load intel/24.0.1-fasrc01
```

For more information on modules, see the [Lmod Modules](https://docs.rc.fas.harvard.edu/kb/modules-intro/)
 page.

#### Intel recommendations and notes

- Intel released Intel OneAPI 23.2 with `icpx`, however, this version does not contain all the features, so we highly recommend using Intel 24 for `icpx`
- Intel 17 is quite old. Avoid using it as it can have many incompatibilities with the current operating system
- Intel has changed its compiler in the past few years and each module may need different flags. Below is a table of executables and possible flags

| Intel module version | Command | Additional flag |
| --- | --- | --- |
| intel/17.0.4-fasrc01 | icpc | -std=gnu++98 |
| intel/23.0.0-fasrc01 | icpc |  |
| intel/23.2.0-fasrc01 | icpx |  |
| intel/24.0.1-fasrc01 | icpx |  |

To compile using a specific version of the Intel compiler, execute:

```
module load intel/24.0.1-fasrc01
icpx -O2 -o sum.x sum.cpp
```

## Examples

[FASRC User Codes](https://github.com/fasrc/User_Codes/tree/master/Languages/Cpp)

Bookmarkable Links

- [1. Description](#Description)
- [2. Best Practice](#Best_Practice)
- [3. Compilers](#Compilers)
  - [3.1. GNU compiler](#GNU_compiler)
  - [3.2. Intel compiler](#Intel_compiler)
    - [3.2.1. Intel recommendations and notes](#Intel_recommendations_and_notes)

- [4. Examples](#Examples)

Last UpdatedJanuary 8, 2025

Tags: - [C](https://docs.rc.fas.harvard.edu/kb/tag/c/)
- [Cpp](https://docs.rc.fas.harvard.edu/kb/tag/cpp/)
