compilers

programs that translate C++ to machine code (g++, clang)

In programming, the term compiler refers to a program that:

For C++, the two main compilers we use are g++ and clang++. Both are free and open source:

Why would I want to use clang++ instead of g++ (or vice-versa)?

Both compilers are good choices—there is no particular reason for us to recommend one over the other.

However, there are some circumstances where you might want to switch compilers:

How do I switch between using g++ and clang++?

On CSIL, by default, the compiler used by make is g++. However, you can change the default compiler to clang++ by defining the environment variable CXX, as shown in this transcript:

-bash-4.3$ hostname
csil.cs.ucsb.edu
-bash-4.3$ cat hello.cpp
#include <iostream>
using namespace std;

int main(void) {
  cout << "Hello, World!" << endl;
}
-bash-4.3$ make hello
g++     hello.cpp   -o hello
-bash-4.3$ ./hello
Hello, World!
-bash-4.3$ rm hello
-bash-4.3$ export CXX=clang++
-bash-4.3$ make hello
clang++     hello.cpp   -o hello
-bash-4.3$ ./hello
Hello, World!
-bash-4.3$ export CXX=g++
-bash-4.3$ rm hello
-bash-4.3$ make hello
g++     hello.cpp   -o hello
-bash-4.3$