How to install CLang using precompiled binaries?
Asked Answered
R

3

22

How do I install CLang on Ubuntu, using precompiled binaries of CLang that I downloaded?

Here's how I downloaded CLang: "LLVM Download Page" -> "Download LLVM 3.2" -> "Clang Binaries for Ubuntu-12.04/x86_64" ( http://llvm.org/releases/3.2/clang+llvm-3.2-x86_64-linux-ubuntu-12.04.tar.gz .)

Then, I expanded the archive into a folder on my Ubuntu 12.04 LTS 64-bit machine. The contents of the expanded folder look like this:

$ ls clang+llvm-3.2-x86_64-linux-ubuntu-12.04
bin  docs  include  lib  share

Question: What do I do next? Do I have to copy these into some folders myself, and if so, which ones exactly? Most instructions I found online are for building CLang from source, which doesn't apply here.

I am a newbie to most of these tools. I created a basic hello-world C++ program, and was able to compile and run it, using GCC and autotools. Now, I want to compile the same program with CLang.

Riarial answered 11/6, 2013 at 13:56 Comment(0)
R
17

You can follow the same step as mentioned in https://askubuntu.com/questions/89615/how-do-i-install-llvm-clang-3-0

using GNU tar:

wget <clang-binaries-tarball-url> #  or `curl -O <url>`
tar xf clang*
cd clang*
sudo cp -R * /usr/local/

If your tar isn't GNU and

  • the archive you get is .tar.gz, you can use tar -xzf;
  • if you have .tar.xz archive, you can use tar -xJf;
  • for .tar.bz2 archive, you can use tar -xjf.
Ribble answered 12/9, 2013 at 13:3 Comment(4)
This did the trick for me, thanks. I got an error with tar xzf "not in gzip format". tar xf figured it out though.Wells
Thanks Matrix, it seems they changed the tarball compression. I updated it to tar xf to be more generic.Ribble
it mgiht be possible to direclty "unzip to /usr/local" byHeterogenesis
@JackWu there is additional level of folder there, so you need to either extract and copy or configure PATH env var afterwards..Ribble
I
4

Assuming you compiled your program with g++ hello.cpp

The equivalents of gcc and g++ are clang and clang++ accordingly. They are found in the bin folder.

It doesn't matter where you place the folders of clang, what matters is you don't move them later. So place them somewhere (I prefer $HOME and I'll assume this for the next)

Then:

  1. Prepend it to $PATH variable

export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:$PATH

  1. Make this permanent by adding it to ~/.bashrc

    echo "export PATH=~/clang+llvm-3.2-x86_64-linux-ubuntu-12.04/bin/:\$PATH" >> ~/.bashrc

Now you can do clang++ hello.cpp

Inedible answered 3/8, 2013 at 18:7 Comment(2)
These steps will not address linking to Clang libraries in the lib directory. One case I have found that is problematic is when using Clang's AddressSanitizer. This requires (implicitly) linking to Clang libraries. I don't have a solution when installing the pre-built binaries but wish I did.Sismondi
You can add the lib folder to the $LD_LIBRARY_PATH the way I showed for $PATHInedible
T
2

I would like to install clang in /home/s. i.e.,

/home/s
   bin  
   lib
   include 
   ...

I did the following in Ubuntu:

wget <clang-binaries-tarball-url>
sudo tar -xf <clang+llvm-..tar.xz> --strip-components=1 -C /home/s  

# Set the path environmental variable  
export PATH=/home/s/bin:$PATH

# Tell ldconfig about new shared library in /home/s/lib
cd /home/s
cat > libs.conf << "END"
/home/s/lib
END

sudo mv libs.conf /etc/ld.so.conf.d/libs.conf
sudo ldconfig

To test it:

clang --version

The output is:

clang version 7.0.0 (tags/RELEASE_700/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/s/bin

Let's test C++17 Filesystem withex1.cpp

#include <iostream>
#include <filesystem>

int main() {
    for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
        std::cout << file.path() << '\n';
    }
}

Compile it

clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic ex1.cpp -o ex1 -lc++fs

Run it

./ex1

The output:

"./ex1"
"./ex1.cpp"
Talkingto answered 5/11, 2018 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.