37

After installing the clang-3.5 package, typing clang or clang++ on the command line gives me a message stating which packages those programs can be found in. Attempting to run the install of clang-3.5 again, apt states it's already installed and at the latest version.

The original install was done with the command:

sudo apt-get install clang-3.5 llvm

build-essential was installed previously.

Any ideas on what may have happened?

8 Answers8

36

It's there, but it's still called clang-3.5.

You can either execute it as clang-3.5 (or clang++-3.5) or setup a symlink to it like I did (installing regular clang didn't work):

sudo ln -s /usr/bin/clang-3.5 /usr/bin/clang
sudo ln -s /usr/bin/clang++-3.5 /usr/bin/clang++

Ugly work-around, perhaps; but at least it works for now :)

guntbert
  • 13,475
17

The proper way to use clang as your default cc and c++ is to use update-alternatives:

It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time. For example, many systems have several text editors installed at once. This gives choice to the users of a system, allowing each to use a different editor, if desired, but makes it difficult for a program to make a good choice of editor to invoke if the user has not specified a particular preference.

so first you need to add clang-3.5 or clang++-3.5 as alternatives to e.g. gcc and g++:

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-3.5 100
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-3.5 100

If at any time you need to switch back to gcc or g++ you can use the --config option:

sudo update-alternatives --config c++
Yan Foto
  • 396
10

On Ubuntu 15.04 you can also install the clang package along the clang-x.x package. You can then type clang++ and the according executable should be found.

sudo apt-get install clang
tisch
  • 209
6

Adding to Yan Foto's answer (and just in case your aim is to get a usable clang but not necessarily use it as cc), you can actually add the entire set of programs in one go using

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.8 380 \
--slave /usr/bin/clang++ clang++ /usr/bin/clang++-3.8 \
--slave /usr/bin/clang-check clang-check /usr/bin/clang-check-3.8 \
--slave /usr/bin/clang-query clang-query /usr/bin/clang-query-3.8 \
--slave /usr/bin/clang-rename clang-rename /usr/bin/clang-rename-3.8

(Note that the set of binaries that come with each clang version might differ, e.g. 3.6 does have clang-tblgen, 3.8 doesn't.)

If you repeat this for every version of clang you install, you'll be able to switch between them using just a single update-alternatives command.

(Meanwhile, apparently, there is still an ongoing debate about whether to include these links with Ubuntu packages or not: https://bugs.launchpad.net/ubuntu/+source/llvm-3.1/+bug/991493)

Latanius
  • 203
2

This should set the update-alternatives for clang-4.0 if clang-3.8 (as on Ubuntu 16.04) is installed:

sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-4.0 100
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-4.0 100
sudo update-alternatives --install /usr/bin/clang-apply-replacements clang-apply-replacements /usr/bin/clang-apply-replacements-4.0 100
sudo update-alternatives --install /usr/bin/clang-check clang-check /usr/bin/clang-check-4.0 100
sudo update-alternatives --install /usr/bin/clang-query clang-query /usr/bin/clang-query-4.0 100
sudo update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-4.0 100
sudo update-alternatives --install /usr/bin/scan-build scan-build /usr/bin/scan-build-4.0 100
sudo update-alternatives --install /usr/bin/scan-view scan-view /usr/bin/scan-view-4.0 100
0

Adding to Johann Wendin's answer about symlinking. In a root-shell you can link all the various clang tools at once:

e.g.

find /usr/bin/ -name clang*  | sed -E 's/^(\/usr\/bin\/.*)(\-[0-9]*)$/ln -s -v \1\2 \1/' | xargs -d '\n' -n 1 bash -c

and then repeat for:

llc-*
lld-*
lldb-*
llvm-*

Then everything should be linked, not just a small selection. (Maybe I still forgot something).

This will find all the files according to the pattern in /usr/bin, remove any trailing numbers (-8 or -9 or even -10), and then create symlinks on each.

0

Create a .bashrc file in your home directory. Type this line of code.

alias clang++="clang++-3.5"

Close your terminal. Open it up again. Then try to compile your program again.

From what I can tell clang won't work unless you state the version in the command name "clang++-3.5". So I just aliased the name in my .bashrc file. So I only have to type "clang++".

By the way I downloaded clang 3.5, so adjust the previous advice to your version.

0

If you have clang correctly installed, another very easy way to save you the trouble of typing the version number along with the name of the program is to use the auto-completion feature, for example (having clang++-3.5 installed):

clang+[TAB][ENTER]

So you only have to type clang+, hit Tab and Enter. Works the same way with names of directories, file names, etc. (This won't help you, however, if you have more than one version of a program installed.)

smay
  • 1