llvm Getting started with llvm Installation or Setup


It is always recommend to go to the LLVM official website and follow the installation guides depending on your OS.

If you are working on posix then in short you have to add one of the official LLVM package repositories. For example if you work on Ubuntu Xenial (16.04) you add a deb and deb-src entry to your /etc/apt/sources.list file:

$ sudo su
$ echo deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main \ >> /etc/apt/sources.list
$ echo deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-4.0 main \ >> /etc/apt/sources.list

and once you do that the installation is as simple as calling

$ sudo apt update
$ sudo apt install clang-X

where X is the version you are looking for (4.0 is current at the time of writing this post).

Note that clang is a C/C++ compiler writen over LLVM (and actually is self hosted now) and comes together with all LLVM libraries. Once you do that you can go to any turorial and start coding.

If you wish you can install LLVM libraries manually. For that you just have to apt install llvm-Y where Y is a library you are looking for. However I do recommend compiling LLVM using projects with clang.

Once you do that you should have llvm-config tool. It is very useful to get compiler flags needed for correct LLVM project compilation. So the first test that it worked would be by calling

$ llvm-config-4.0 --cxxflags --libs engine
-I/usr/lib/llvm-4.0/include -std=c++0x -gsplit-dwarf -Wl,-fuse-ld=gold -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -Werror=date-time -std=c++11 -ffunction-sections -fdata-sections -O2 -g -DNDEBUG  -fno-exceptions -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS

You may get a different set of flags, do not worry about it. As long as it doesn't fail with command not found you should be fine.

Next step is to test the actual LLVM library itself. So lets create a simple llvmtest.cpp file:

#include <iostream>
#include "llvm/IR/LLVMContext.h"

int main() {
    llvm::LLVMContext context;
    std::cout << &context << std::endl;
    return 0;

Note that I use std::cout so that we actually use the context variable (so the compiler won't remove it during the compilation phase). Now compile the file with

$ clang++-4.0 -o llvmtest `llvm-config-4.0 --cxxflags --libs engine` llvmtest.cpp

and test it

$ ./llvmtest

Congratulations! You are ready to use LLVM.