How does g++ work with c++11 codes?
Let’s say that we run a simple program which consists of following C++ codes.
when you build this cpp file, it works well with g++.
|
|
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.
|
|
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.
|
|
It works anyway, what about the version of gcc then?
|
|
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