gcc, g++, clang and LLVM

How does g++ work with c++11 codes?

Let’s say that we run a simple program which consists of following C++ codes.

1
2
3
4
5
6
7
8
9
10
11
12
13
// test_noc++11.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = {1,2,3};
vector<int> vi(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (vector<int>::iterator it=vi.begin(); it != vi.end(); it++){
cout << *it << endl;
}
return 0;
}

when you build this cpp file, it works well with g++.

1
2
3
4
5
$ g++ test_noc++11.cpp
$ ./a.out
1
2
3

My question starts from here. What about c++11 codes? Does it work with my complier without any problem? So let me go for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
// test_c++11.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arr[] = {1,2,3};
vector<int> vi(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (const int& data : vi){
cout << data << endl;
}
return 0;
}

This code is using the “Range-based for loop” enhancement in C++11. When I tried to compile this, we can see the warning like following.

1
2
3
4
5
6
7
8
9
$ g++ test.cpp
test.cpp:9:23: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for (const int& data : vi){
^
1 warning generated.
$ ./a.out
1
2
3

It works anyway, what about the version of gcc then?

1
2
3
4
5
6
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.38)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
version Release Date
GCC 4.2.2 October 7, 2007
GCC 4.2.1 July 18, 2007
GCC 4.2.0 May 13, 2007

the version of gcc is indeed 4.2.1, which doesn’t support C++11 since it’s developed in 2007.

What are clang and LLVM?

So what makes my code work? What are clang and LLVM? Let’s go a little deeper.

Clang /ˈklæŋ/[4] is a compiler front end for the programming languages C, C++, Objective-C, Objective-C++, OpenMP,[5] OpenCL, and CUDA. It uses LLVM as its back end and has been part of the LLVM release cycle since LLVM 2.6.

from Wikipedia

The LLVM compiler infrastructure project (formerly Low Level Virtual Machine) is a “collection of modular and reusable compiler and toolchain technologies” used to develop compiler front ends and back ends.
LLVM is written in C++ and is designed for compile-time, link-time, run-time, and “idle-time” optimization of programs written in arbitrary programming languages. Originally implemented for C and C++, the language-agnostic design of LLVM has since spawned a wide variety of front ends: languages with compilers that use LLVM include ActionScript, Ada, C#, Common Lisp, Crystal, D, Delphi, Fortran, OpenGL Shading Language, Halide, Haskell, Java bytecode, Julia, Lua, Objective-C, Pony, Python, R, Ruby, Rust, CUDA, Scala, and Swift.

from Wikipedia

All right. I just figured out when I run the g++, llvm-gcc is indeed running, which is the gcc frontend, and then the llvm backend. On the other side, clang++ is running clang, which is the clang frontend and then the llvm backend. Following

So, my g++ is actually using clang-LLVM 8.0, which supports c++11, even c++14.

DONE.

Further questions

  • what are front-end compiler and back-end complier? I should’ve taken the complier coursework during my B.S :(
  • who made the clang & LLVM? are they open-source projects?

Links

C++11 Wikipedia
C++11 Tutorial Video
Added featured in C++11 Slide
gcc release note
clang project homepage
clang Wikipedia
[LLVM Wikipedia] (https://en.wikipedia.org/wiki/LLVM)
g++, clang++ on mac

Share Comments