I decided to switch from gcc to clang : I downloaded the latest clang, available on the default repository (4.0). At the same time, when running clang -o prog prog.c -Wall , the older compiler is used, and the new one is available only with clang-4.0 -o prog prog.c -Wall . How to set a new default clang ? And another question: do not tell the repository with the newest clang-5.0 ?

    2 answers 2

    The method with manual translation of links is sad. Most likely, with the next update, everything will either be overwritten by default, or it will become a stake.

    But especially for your case, a system of alternatives has long been invented.

    The easiest way (it is actually very similar to the @diraria method) is as follows. First you need to "install"

     sudo update-alternatives \ --install /usr/bin/clang clang /usr/bin/clang-4.0 50 \ --slave /usr/bin/clang++ clang++ /usr/bin/clang++-4.0 \ --slave /usr/bin/lldb lldb /usr/bin/lldb-4.0 \ --slave /usr/bin/lldb-server lldb-server /usr/bin/lldb-server-4.0 

    (check and correct the ways!).

    Now, at any time you can run this command.

     sudo update-alternatives --config clang 

    and select the desired version of the compiler. The slave parameter allows you to connect several associated ones to the "master name".

    Also do not forget that, besides the clang itself, you need to tighten the corresponding llvm. Full sets of commands here .

    And here it is described how to collect the most fresh clang.

    short translation

    First we add the llvm.org repository:

     deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-5.0 main 

    To do this, open the editor

     sudo nano /etc/apt/sources.list.d/llvm.list 

    Also add the repository key

     wget -O - http://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - 

    now update the list

     sudo apt-get update 

    Now we put the necessary compiler

     sudo apt-get install clang-5.0 lldb-5.0 lld-5.0 

    Now everything should work

    If you want to see a list of everything in this repository, you need such a command.

     grep -i package: /var/lib/apt/lists/apt.llvm* | cut -f3 -d: | sort | uniq 

    there will be a list of

     clang-5.0 clang-5.0-doc clang-5.0-examples libclang-common-5.0-dev ... 

    Naturally, you can choose another version.

    • The method with manual translation of links is no worse: update-alternatives respects this choice, what is directly written in the mana ... - Fat-Zer
    • This is Linux. If the user wants to shave his chainsaw, that’s his choice. But if you cut off your head - this is his choice. - KoVadim
    • this is a documented opportunity, and not some kind of hack ... a current that quite has the right to exist ... - Fat-Zer
    • I absolutely do not mind. If the user understands what he is doing and remembers everything well, then at least fill the bytes with pens. - KoVadim

    tl; dr: ln -sf $(which clang-4.0) $(which clang)

    Probably not the best way, but still:

    1. Using the which command, we determine the file location for the clang command. For example, which clang gives me the path /usr/bin/clang
    2. We look at the file obtained in the previous paragraph, most likely it will be a symbolic link . For example, my /usr/bin/clang is a symbolic link to /usr/bin/clang-5.0
    3. Overwrite this file so that it is a symbolic link to the executable file for clang-4.0 (the location of which is found by the same which command:

       ln -sf /usr/bin/clang-4.0 /usr/bin/clang 

      explanation:

      • ln is the command to create links. It has syntax ln файл имя_ссылки
      • flag -s - create a symbolic link instead of a hard link
      • the -f flag - do not give an error if the link file being created exists