Installing a C/C++ compiler
In order to compile and link C++ (or C) source code into an executable program, you need to install a compiler on your computer. How you do this will depend on your operating system and on your preferred method of software installation.
Installing a C/C++ compiler on macOS
Depending on how particular you are about which compiler (or compiler version) you wish to install, there are a few different options for installing a C/C++ compiler on your macOS system.
Installing a compiler with the Xcode command-line tools
The Xcode command-line tools are a good choice if you simply
want some C/C++ compiler and aren't picky about which one. This is probably
true if you are beginning to learn C/C++ (e.g., as a student in COSC 1020 at Georgetown
University). The command-line tools include many programs, one of which is the LLVM
compiler named clang
(usually said aloud as "klang").
Installing a compiler with Homebrew
If you are picky compilers (or compiler versions), then the Homebrew package manager will give you control over exactly what you want to install. Both the GCC and LLVM toolchains are available.
Homebrew formulas for C/C++ compilers
To install a specific version of GCC, use the formula
$ brew install gcc[<version-no>]
To install a specific version of LLVM, use the formula
$ brew install llvm[@<version-no>]
Note that Homebrew requires the Xcode command-line tools in order to function. To avoid
conflicts with the compiler that comes with the tools, Homebrew does not
install to /usr/bin
(typically, it installs to /usr/local/bin
instead). Be sure to set your PATH
, environment variables, and/or makefile
variables appropriately to use the compiler you want.
Note also that Homebrew does not cross-alias between GCC and LLVM tools like the Xcode command-line tools, so you need to install and explicitly invoke the compiler you want to use.
The Xcode command-line tools install the LLVM compiler, clang
, but they
also create aliases of the GNU compilers gcc
and
g++
to invoke their LLVM counterparts.
You can see this yourself by typing g++ --version
to print the version
of "g++" installed on your computer -- you'll see output mentioning LLVM instead,
because you actually ran clang++ --version
(just by a different name).
You can see this for yourself by typing g++ --version
to print the
version of "g++" installed on your machine -- you'll see output that mentions LLVM
instead, because you actually ran clang --version
(just by a different
name).
Installing a C/C++ compiler on Windows
The WinLibs project is a free, open-source C/C++ compiler providing native implementations of GCC (and, optionally, of LLVM/clang) as a stand-alone installation. This works both for GUI development environments like Visual Studio Code and for CLI invocations through CMD.exe or Powershell.
The instructions below provide a few different options for installing WinLibs on your computer.
Installing WinLibs with winget
You can use the Windows Package Manager to install Winlibs with a terminal command. There are a few options to chose from; the first one listed below is best unless you know you need one of the others (if you know, you know).
WinLibs installation with winget
The best choice for most people is to install the UCRT version of WinLibs:
$ winget install --id=BrechtSanders.WinLibs.POSIX.UCRT.LLVM -e
If you don't want LLVM in your install, then use instead:
$ winget install --id=BrechtSanders.WinLibs.POSIX.UCRT -e
Should you want the MSVCRT runtime instead of UCRT, use:
$ winget install --id=BrechtSanders.WinLibs.POSIX.MSVCRT.LLVM -e
This version is also available without LLMV:
$ winget install --id=BrechtSanders.WinLibs.POSIX.MSVCRT -e
Winget may need several minutes when "Extracting archive..."
as
WinLibs is delivered as a compressed archive of many small files (which takes time to
extract and install properly). Have patience!
Installing WinLibs as a downloadable archive
If you prefer to download and install WinLibs yourself, you can do so via the instructions below. These are, more or less, the steps that winget performs (see above) but gives you more control over the installation directory.
- Download the WinLibs archive
- Go to winlibs.com and scroll down to the "Download" section of the page.
- Look for the "Release Versions" / "UCRT runtime" section and find the first
bullet point, labeled "(LATEST)" which includes the most recent version of
GCC.
- You can choose an older version of GCC and/or scroll down to the "MSVCRT runtime" if your needs require.
- Look to the line labeled "Win64" and select the "Zip archive" link to download
the .zip file.
- The first link includes LLVM, while the second does not.
- Extract the archive to the install directory of your choice
- Extract the contents of the .zip to "install" the compiler
- Select a directory on your computer which has only simple characters and no
spaces in the folder/subfoldre names.
- You'll remember for the following step -- I'll call it
DIR
in the next step. For example, I chose to install inD:\
- You'll remember for the following step -- I'll call it
- Extract the contents of the .zip file to your chosen directory. This will create
a new folder named
mingw64
(with several subdirectories).
- Select a directory on your computer which has only simple characters and no
spaces in the folder/subfoldre names.
- Update your
PATH
- Open the Start Menu and search for "Edit the system environment variables"
- In the window that appears, click the "Environment variables" button
- In the second window that appears, look in the bottom-half ("System Variables") for a row containing "Path"; click on this row, then push the "Edit" button
- In the third window that appears, click the "New" button to add a row to the bottom of the list of directories.
- In the new row, type
DIR\mingw64\bin
, whereDIR
is the directory you chose in the previous step. For example, I wroteD:\mingw64\bin
- Press the "OK" button on each of the three windows to save your settings
- Restart your computer for the updated path to take effect
Installing a C/C++ compiler in Windows has, historically, been difficult -- more difficult than in macOS or Linux. Microsoft's Visual Studio toolchain works well for Windows-centric development but lacks portability.
An attractive alternative is the Windows Subsystem for Linux which provides a Linux CLI environment within Windows. This allows you to make use of standard Linux programming tools for C/C++ programming and more.
There are two major runtime libraries (that is, implementations of the C standard library) for Windows development.
MSVCRT is older and available on all versions of Windows. It combined a C standard library runtime with Microsoft-specific (Visual C) runtimes. It lacks comformance with certain modern C standards, but is backwards-compatible.
UCRT is newer (it was introduced in Windows 10) which is separated from the Visual C runtime (which moved to vcruntime) in order to be more standard-compliant.
Generally, UCRT is the better choice unless you are targeting older versions of Windows.