Linux 如何更改 Ubuntu 中的默认 GCC 编译器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7832892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the default GCC compiler in Ubuntu?
提问by RoboAlex
I have installed gcc-3.3/g++-3.3 on ubuntu 11.04 which already has gcc/g++-4.4. So in my system both gcc-3.3 and 4.4 are available. I am able to call both compilers as I want. If I just call the command gcc
then gcc-4.4 will get called. To call gcc-3.3, I have to use the command gcc-3.3
.
我已经在已经有 gcc/g++-4.4 的 ubuntu 11.04 上安装了 gcc-3.3/g++-3.3。所以在我的系统中,gcc-3.3 和 4.4 都可用。我可以根据需要调用两个编译器。如果我只是调用该命令,gcc
则会调用gcc-4.4。要调用 gcc-3.3,我必须使用命令gcc-3.3
.
How can I change the default compiler as gcc-3.3? When I execute the command gcc
it should call the gcc-3.3 and not gcc-4.4.
如何将默认编译器更改为 gcc-3.3?当我执行命令时,gcc
它应该调用 gcc-3.3 而不是 gcc-4.4。
In addition, how can I change the variable CXX in a make file to gcc-3.3? I wish to change one common global place in the system instead of changing all make files.
另外,如何将make文件中的变量CXX更改为gcc-3.3?我希望更改系统中的一个通用全局位置,而不是更改所有 make 文件。
采纳答案by jopasserat
As @Tommy suggested, you should use update-alternatives
.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.
正如@Tommy 建议的那样,您应该使用update-alternatives
.
它为一个系列的每个软件分配值,以便定义调用应用程序的顺序。
It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc
, and one will be favoured.
它用于在系统上维护同一软件的不同版本。在您的情况下,您将能够使用 的多个偏角gcc
,其中一个将受到青睐。
To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:
要确定 gcc 当前的优先级,请输入@tripleee 评论指出的命令:
update-alternatives --query gcc
Now, note the priority attributed to gcc-4.4
because you'll need to give a higher one to gcc-3.3
.
To set your alternatives, you should have something like this (assuming your gcc
installation is located at /usr/bin/gcc-3.3
, and gcc-4.4
's priority is less than 50):
现在,请注意归因于的优先级,gcc-4.4
因为您需要为 提供更高的优先级gcc-3.3
。
要设置您的替代方案,您应该有这样的东西(假设您的gcc
安装位于/usr/bin/gcc-3.3
,并且gcc-4.4
的优先级小于 50):
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50
--edit--
- 编辑 -
Finally, you can also use the interactive interface of update-alternatives
to easily switch between versions. Type update-alternatives --config gcc
to be asked to choose the gcc version you want to use among those installed.
最后,您还可以使用 的交互界面update-alternatives
在版本之间轻松切换。键入update-alternatives --config gcc
要求在已安装的 gcc 版本中选择要使用的 gcc 版本。
--edit 2 --
-- 编辑 2 --
Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc
file (this will apply the change only for your user, which is safer in my opinion):
现在,要在系统范围内修复 CXX 环境变量,您需要将 @DipSwitch 指示的行放在您的.bashrc
文件中(这将仅对您的用户应用更改,我认为这更安全):
echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
回答by Trevor Robinson
Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root
:
这是 jHackTheRipper 对 TL;DR 人群的回答的完整示例。:-) 在这种情况下,我想在默认为 4.6 的 Ubuntu 系统上运行 g++-4.5。作为root
:
apt-get install g++-4.5
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50
update-alternatives --set g++ /usr/bin/g++-4.5
update-alternatives --set gcc /usr/bin/gcc-4.5
update-alternatives --set cpp-bin /usr/bin/cpp-4.5
Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:
在这里,4.6 仍然是默认的(又名“自动模式”),但我明确地暂时切换到 4.5(手动模式)。回到 4.6:
update-alternatives --auto g++
update-alternatives --auto gcc
update-alternatives --auto cpp-bin
(Note the use of cpp-bin
instead of just cpp
. Ubuntu already has a cpp
alternative with a master link of /lib/cpp
. Renaming that link would remove the /lib/cpp
link, which could break scripts.)
(请注意,使用cpp-bin
而不只是cpp
。Ubuntu 已经有一个cpp
主链接为的替代方案/lib/cpp
。重命名该链接将删除该/lib/cpp
链接,这可能会破坏脚本。)
回答by Anton K
This is the great descriptionand step-by-step instruction how to create and manage master and slave (gcc and g++) alternatives.
这是如何创建和管理主从(gcc 和 g++)替代方案的精彩描述和分步说明。
Shortly it's:
很快就是:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.7
sudo update-alternatives --config gcc
回答by dileks
Now, there is gcc-4.9 available for Ubuntu/precise.
现在,有适用于 Ubuntu/precise 的 gcc-4.9。
Create a group of compiler alternatives where the distro compiler has a higher priority:
创建一组编译器替代方案,其中发行版编译器具有更高的优先级:
root$ VER=4.6 ; PRIO=60
root$ update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER
root$ update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-$VER $PRIO
root$ VER=4.9 ; PRIO=40
root$ update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER
root$ update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-$VER $PRIO
NOTE:g++ version is changed automatically with a gcc version switch. cpp-bin has to be done separately as there exists a "cpp" master alternative.
注意:g++ 版本会通过 gcc 版本开关自动更改。cpp-bin 必须单独完成,因为存在“cpp”主替代方案。
List available compiler alternatives:
列出可用的编译器替代品:
root$ update-alternatives --list gcc
root$ update-alternatives --list cpp-bin
To select manually version 4.9 of gcc, g++ and cpp, do:
要手动选择 gcc、g++ 和 cpp 的 4.9 版,请执行以下操作:
root$ update-alternatives --config gcc
root$ update-alternatives --config cpp-bin
Check compiler versions:
检查编译器版本:
root$ for i in gcc g++ cpp ; do $i --version ; done
Restore distro compiler settings (here: back to v4.6):
恢复发行版编译器设置(此处:回到 v4.6):
root$ update-alternatives --auto gcc
root$ update-alternatives --auto cpp-bin
回答by Thomas Baruchel
In case you want a quicker (but still very clean) way of achieving it for a personal purpose (for instance if you want to build a specific project having some strong requirements concerning the version of the compiler), just follow the following steps:
如果您想要一种更快(但仍然非常干净)的方式来实现个人目的(例如,如果您想构建一个对编译器版本有一些强烈要求的特定项目),只需按照以下步骤操作:
- type
echo $PATH
and look for a personaldirectory having a very high priority (in my case, I have~/.local/bin
); - add the symbolic links in this directory:
- 键入
echo $PATH
并查找具有非常高优先级的个人目录(在我的情况下,我有~/.local/bin
); - 在此目录中添加符号链接:
For instance:
例如:
ln -s /usr/bin/gcc-WHATEVER ~/.local/bin/gcc
ln -s /usr/bin/g++-WHATEVER ~/.local/bin/g++
Of course, this will work for a single user (it isn't a system wide solution), but on the other hand I don't like to change too many things in my installation.
当然,这适用于单个用户(它不是系统范围的解决方案),但另一方面,我不喜欢在我的安装中更改太多东西。
回答by hmijail mourns resignees
I found this problem while trying to install a new clang compiler. Turns out that both the Debianand the LLVM maintainers agreethat the alternatives system should be used for alternatives,NOT for versioning.
我在尝试安装新的 clang 编译器时发现了这个问题。事实证明,Debian和LLVM 维护者都同意替代系统应该用于替代,而不是用于版本控制。
The solution they propose is something like this:PATH=/usr/lib/llvm-3.7/bin:$PATH
where /usr/lib/llvm-3.7/bin is a directory that got created by the llvm-3.7 package, and which contains all the tools with their non-suffixed names. With that, llvm-config (version 3.7) appears with its plain name in your PATH. No need to muck around with symlinks, nor to call the llvm-config-3.7 that got installed in /usr/bin.
他们提出的解决方案是这样的:PATH=/usr/lib/llvm-3.7/bin:$PATH
其中 /usr/lib/llvm-3.7/bin 是由 llvm-3.7 包创建的目录,其中包含所有带有非后缀名称的工具。这样,llvm-config(3.7 版)就会在您的 PATH 中显示其纯名称。无需使用符号链接,也无需调用安装在 /usr/bin 中的 llvm-config-3.7。
Also, check for a package named llvm-defaults (or gcc-defaults), which might offer other way to do this (I didn't use it).
另外,检查一个名为 llvm-defaults(或 gcc-defaults)的包,它可能提供其他方法来做到这一点(我没有使用它)。
回答by nyxee
I used just the lines below and it worked. I just wanted to compile VirtualBox
and VMWare WorkStation
using kernel 4.8.10
on Ubuntu 14.04
. Initially, most things were not working for example graphics and networking. I was lucky that VMWare workstation
requested for gcc 6.2.0
. I couldn't start my Genymotion Android emulators
because virtualbox was down. Will post results later if necessary.
我只使用了下面的几行,它奏效了。我只是想编译VirtualBox
和VMWare WorkStation
使用kernel 4.8.10
on Ubuntu 14.04
。最初,大多数东西都不起作用,例如图形和网络。我很幸运,VMWare workstation
要求gcc 6.2.0
. 我无法启动我的,Genymotion Android emulators
因为 virtualbox 已关闭。如有必要,稍后将发布结果。
VER=4.6 ; PRIO=60
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER
VER=6 ; PRIO=50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER
VER=4.8 ; PRIO=40
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-$VER $PRIO --slave /usr/bin/g++ g++ /usr/bin/g++-$VER
回答by szotsaki'gofundme-DefendFromSO
Between 4.8 and 6 with all --slaves
:
在 4.8 和 6 之间,所有--slaves
:
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 \
10 \
--slave /usr/bin/cc cc /usr/bin/gcc-4.8 \
--slave /usr/bin/c++ c++ /usr/bin/g++-4.8 \
--slave /usr/bin/g++ g++ /usr/bin/g++-4.8 \
--slave /usr/bin/gcov gcov /usr/bin/gcov-4.8 \
--slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-4.8 \
--slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-4.8 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-4.8 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-4.8 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-4.8
and
和
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 \
15 \
--slave /usr/bin/cc cc /usr/bin/gcc-6 \
--slave /usr/bin/c++ c++ /usr/bin/g++-6 \
--slave /usr/bin/g++ g++ /usr/bin/g++-6 \
--slave /usr/bin/gcov gcov /usr/bin/gcov-6 \
--slave /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-6 \
--slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-6 \
--slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-6 \
--slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-6 \
--slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-6
Change between them with update-alternatives --config gcc
.
用 改变它们update-alternatives --config gcc
。