CentOS

How to Install LLVM on CentOS7

Like GCC, LLVM is a C/C++ compiler toolset. LLVM can compile C, C++ and Objective-C programs. LLVM Clang can compile C and C++ codes faster than GCC. A very memory efficient debugger LLDB is available in LLVM. LLDB is very fast at loading symbols compared to GCC. LLVM support C++11, C++14 and C++17 through libc++ and libc++ ABI projects.

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:

$ sudo yum makecache

$ sudo yum install gcc gcc-c++

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.

$ sudo yum info clang

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:

$ sudo yum repolist

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:

$ sudo yum install yum-utils

Press y and then press <Enter> to continue.

yum-utils should be installed.

Now enable extras repository with the following command:

$ sudo yum-config-manager --enable extras

It should be enabled.

Installing LLVM Clang:

Now update the yum package repository cache with the following command:

$ sudo yum makecache

Finally install LLVM Clang with the following command:

$ sudo yum install clang

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.

$ clang --version

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:

$ cd ~/codes

Install LLVM CentOS7

I have two files here as you can see in the screenshot below:

$ ls -lh

The Contents of hello.c File:

#include <stdio.h>
 

int main(void) {

printf("It works for C!\n");

return 0;

}

The Contents of world.cpp File:

#include <iostream>

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:

$ clang -o hello hello.c

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:

$ ./hello

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:

$ clang++ -o world world.cpp

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:

$ ./world

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.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.