在Linux上安装C/C++环境

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

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 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 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 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.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值