It’s cross platform. LLVM is available on Linux, Windows and Mac OS X.
An older version of LLVM is available in the official extras repository on CentOS 7. But If you want, you can also download and install the latest version of LLVM from the official website of LLVM at http://llvm.org
In this article, I will show you how to install and use LLVM Clang on CentOS 7. Let’s get started.
Installing C and C++ Libraries for LLVM Clang
Before you install LLVM Clang, you should install the C and C++ libraries. Otherwise, you won’t be able to compile C and C++ programs.
The easiest way to install C and C++ libraries for LLVM Clang is to install gcc and g++ on CentOS 7.
You can install gcc and g++ on CentOS 7 with the following commands:
Press y and then press <Enter> to continue.
gcc and g++ should be installed.
Installing LLVM Clang from the Official Package Repository
LLVM Clang version 3.4.2 is available on CentOS 7 operating system in the extras repository as you can see in the screenshot below.
To install the extras repository version of LLVM Clang on CentOS 7, first you have to enable the extras repository on CentOS 7.
It should be enabled by default on CentOS 7, but in case you don’t have it enabled, I will show you how to enable it.
Listing the Enabled CentOS 7 Repositories:
Run the following command to list all the enabled repository of your CentOS 7 operating system:
If you have extras repository enabled, it should be listed as you can see in the marked section of the screenshot below. In that case, you can skip ahead a little bit. Otherwise follow along.
Enabling extras Repository on CentOS 7:
Install yum-utils package with the following command:
Press y and then press <Enter> to continue.
yum-utils should be installed.
Now enable extras repository with the following command:
It should be enabled.
Installing LLVM Clang:
Now update the yum package repository cache with the following command:
Finally install LLVM Clang with the following command:
Now press y and then press <Enter> to continue.
LLVM Clang should be installed.
As you can see from the screenshot below, LLVM Clang 3.4.2 was installed correctly.
Using LLVM Clang
In this section I will write a simple C and C++ program and compile it with LLVM Clang to show you how it works.
First I am navigating to the ~/codes directory where I saved my hello.c and world.cpp file with the following command:
I have two files here as you can see in the screenshot below:
The Contents of hello.c File:
The Contents of world.cpp File:
using namespace std;
int main(void) {
cout << "It works for C++!" << endl;
return 0;
}
Compiling and Running C Programs:
Now you can compile hello.c C source file with the following command:
NOTE: Here hello.c is the source code file, and hello after the -o option is the output file. hello will be the generated executable binary after the compilation process is completed.
Once you compile hello.c, you should find a new binary file hello in the same directory as the hello.c source file as you can see in the screenshot below.
You can run hello binary file as follows:
As you can see, ‘It works for C!’ is printed on the screen. So we are able to compile C programs with LLVM Clang.
Compiling and Running C++ Programs:
You can compile world.cpp C++ source file with the following command:
NOTE: Here world.cpp is the source code file, and world after the -o option is the output file. world will be the generated executable binary after the compilation process is completed.
Once you compile world.cpp, you should find a new binary file world in the same directory as world.cpp file as you can see in the screenshot below.
Now you can run world binary file as follows:
As you can see, ‘It works for C++!’ is printed on the screen. So we are able to compile C++ programs with LLVM Clang.
That’s how you install and use LLVM Clang on CentOS 7. Thanks for reading this article.