Compilers and IDEs
The “traditional” development environment for compiled languages was a text editor and a command line. Many programmers continue to use these successfully, but modern tools can greatly improve programmer productivity. Some such tools are especially recommended for the Windows operating system, since it does not support command-line usage as cleanly as Linux or macOS.
Compilers
Compilers are sophisticated software packages. They must perform a complex analysis of the code, translate it to machine language, and invoke a linker to create an executable. This “big picture” view and direct machine language output is what enables compiled programs to run generally with much higher performance than interpreted scripts. Compilers also offer a large number of compile-time options that can significantly impact the performance and sometimes the results of the executable.
Many compilers are available, but we will focus on those from three vendors.
Gnu Compiler Collection
The
Gnu Compiler Collection is a well-established, free and open-source bundle. The base compiler is gcc
for C. Several add-on languages are supported, the most widely used of which are g++
(C++) and gfortran
(Fortran).
NVIDIA HPC SDK
The
NVIDIA HPC SDK is another free (though not open-source) compiler suite for C/C++/Fortran. Formerly the Portland Group compilers, it is a general-purpose package but is oriented toward extensions for programming NVIDIA GPUs. These compilers are nvcc
, nvc++
, and nvfortran
.
Intel Compilers
The Intel compilers have a reputation for producing the fastest executables on Intel architectures. Most high-performance computing sites provide the commercial Intel suite icc
, icpc
, and ifort
. Intel’s Parallel Studio package also ships with high-performance Math Kernel Libraries (MKL), MPI (IntelMPI), and threading (tbb). The Parallel Studio package is available on the UVA HPC system.
Intel has recently released the
oneAPI Toolkits. They are free but not open source, and are supported only through a community forum. In order to obtain the “classic” compilers described above, the HPC Toolkit must be installed. The newer compilers provided in the Base Toolkit for Intel are icx
, and icpx
. Both the classic and the new Fortran compilers ifort
and ifx
are in the HPC Toolkit.
Integrated Development Environments
An Integrated Development Environment (IDE) combines an editor and a way to compile and run programs in the environment. A well-known IDE for Microsoft Windows is Visual Studio. This is available through the Microsoft Store; it is not free for individuals. macOS uses Xcode as its native IDE. Xcode includes some compilers, particularly for Swift, but it can manage several other languages. Available at the App Store and free.
A full-featured cross-platform IDE is [Eclipse] (http://www.eclipse.org/). Free.
A lighter-weight IDE for Windows and Linux is [Code::Blocks] (http://www.codeblocks.org/). Free.
Windows programmers using Intel’s oneAPI distribution must also install Visual Studio.
An increasingly popular IDE is Visual Studio Code ( VSCode) from Microsoft. It is also cross-platform, with versions available for Windows, macOS, and Linux. It does not support C, C++, or Fortran by default; extensions must be installed to provide syntax highlighting and debugging for those languages. C and C++ are installed with one extension that can be found at the top of the list.
In our examples, we will use a very lightweight IDE called Geany since it is free, easy to install and use, and works on all three platforms. It is more of a programmer’s editor than a full-featured IDE, but it does include some build tools.
Building an Executable
Creating an executable is generally a multistep process. Of course, the first step is the preparation of a source file. The convention for C++ was once a file extension of .cpp
, but that conflicts with the name of the preprocessor (C PreProcessor) so .cxx
is increasingly common. We will use .cxx
but .cpp
still works.
From each source file, the compiler first produces an object file. In Unix these end in .o
, or .obj
on Windows.
This is the compilation step.
Object files are binary (machine language) but cannot be executed. They must be linked into an executable by a program called a linker (also called a loader). The linker is normally invoked through the compiler. The entire process of compiling and linking is called building the executable.
If not told otherwise a compiler will attempt to compile and link the source file(s) it is instructed to compile. If more than one file is needed to create the executable, linking will not work until all object files are available, so the compiler must be told to skip that step.
For Unix compilers the -c
option suppresses linking. The compiler must then be run again to build the executable from the object files.
The linker option -o
is used to name the binary something other than a.out
.
Unix and macOS do not care about file extensions, but Windows will expect an executable to end in .exe
.
Command Line
For full control and access to more compiler options, we can build from the command line. For Linux and Mac this is a terminal application. On Windows, use a command prompt for gcc. The Intel oneAPI distribution ships with an integrated command prompt in its folder in the “applications” menu; this command prompt is aware of the location of the compiler executables and libraries.
Example
g++ -c mymain.cxx
g++ -c mysub.cxx
IDEs generally manage basic compiler options and usually name the executable based on the project name. Our examples of command line usage will all assume a Unix operating system; there are some differences between Linux and macOS, with larger differences for Windows. On macOS and especially Windows, using an IDE makes code management simpler. Using Geany as our example, clicking the icon showing a pyramid pointing to a circle will compile the current file without attempting to invoke the linker. The brick icon builds the current file, so it must be possible to create a standalone executable from a single file.