如何更改 Ubuntu 中的默认 GCC 编译器?
我已经在 ubuntu 11.04 上安装了 gcc-3.3/g++-3.3,它已经有 gcc/g++-4.4。所以在我的系统中 gcc-3.3 和 4.4 都可用。我可以根据需要调用两个编译器。如果我只调用命令 gcc
那么 gcc-4.4 将被调用。要调用 gcc-3.3,我必须使用命令 gcc-3.3
。
如何将默认编译器更改为 gcc-3.3?当我执行命令 gcc
时,它应该调用 gcc-3.3 而不是 gcc-4.4。
另外,如何将make文件中的变量CXX更改为gcc-3.3?我希望更改系统中一个通用的全局位置,而不是更改所有 make 文件。
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
.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 @Tommy 所建议的,您应该使用
update-alternatives
。它为系列中的每个软件分配值,以便定义应用程序的调用顺序。
它用于维护系统上同一软件的不同版本。就您而言,您将能够使用
gcc
的多种偏角,其中一种会受到青睐。要找出 gcc 当前的优先级,请输入 @tripleee 评论指出的命令:
现在,请注意
gcc-4.4
的优先级,因为您需要为提供更高的优先级>gcc-3.3
。要设置您的替代方案,您应该有类似的内容(假设您的
gcc
安装位于/usr/bin/gcc-3.3
,并且gcc -4.4
的优先级小于50):--edit--
最后还可以使用
update-alternatives 轻松地在版本之间切换。输入 update-alternatives --config gcc ,系统会要求您在已安装的 gcc 版本中选择要使用的版本。
--edit 2 --
现在,要修复系统范围内的 CXX 环境变量,您需要将 @DipSwitch 指示的行放入
.bashrc
文件中(这将应用仅针对您的用户进行更改,我认为这更安全):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.
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.To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:
Now, note the priority attributed to
gcc-4.4
because you'll need to give a higher one togcc-3.3
.To set your alternatives, you should have something like this (assuming your
gcc
installation is located at/usr/bin/gcc-3.3
, andgcc-4.4
's priority is less than 50):--edit--
Finally, you can also use the interactive interface of
update-alternatives
to easily switch between versions. Typeupdate-alternatives --config gcc
to be asked to choose the gcc version you want to use among those installed.--edit 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):这是 jHackTheRipper 为 TL;DR 人群提供的答案的完整示例。 :-) 在本例中,我想在默认为 4.6 的 Ubuntu 系统上运行 g++-4.5。作为 root :
这里,4.6 仍然是默认值(也称为“自动模式”),但我明确地暂时切换到 4.5(手动模式)。返回到 4.6:(
注意使用
cpp-bin
而不仅仅是cpp
。Ubuntu 已经有一个带有主链接的cpp
替代品重命名该链接将删除/lib/cpp
链接,这可能会破坏脚本。)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
: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:
(Note the use of
cpp-bin
instead of justcpp
. Ubuntu already has acpp
alternative with a master link of/lib/cpp
. Renaming that link would remove the/lib/cpp
link, which could break scripts.)这是精彩的描述和分步说明如何创建和管理主从(gcc 和 g++)替代方案。
不久之后就是:
This is the great description and step-by-step instruction how to create and manage master and slave (gcc and g++) alternatives.
Shortly it's:
在 4.8 和 6 之间,所有
--slaves
:并
使用
update-alternatives --config gcc
在它们之间进行更改。Between 4.8 and 6 with all
--slaves
:and
Change between them with
update-alternatives --config gcc
.现在,gcc-4.9 可用于 Ubuntu/precise。
创建一组编译器替代方案,其中发行版编译器具有更高的优先级:
注意: g++ 版本会通过 gcc 版本切换自动更改。 cpp-bin 必须单独完成,因为存在“cpp”主替代方案。
列出可用的编译器替代方案:
要手动选择 gcc、g++ 和 cpp 的 4.9 版本,请执行以下操作:
检查编译器版本:
恢复发行版编译器设置(此处:返回 v4.6):
Now, there is gcc-4.9 available for Ubuntu/precise.
Create a group of compiler alternatives where the distro compiler has a higher priority:
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.
List available compiler alternatives:
To select manually version 4.9 of gcc, g++ and cpp, do:
Check compiler versions:
Restore distro compiler settings (here: back to v4.6):
我在尝试安装新的 clang 编译器时发现了这个问题。事实证明 Debian 和 LLVM 维护者同意替代系统应该用于替代,而不是版本控制。
他们提出的解决方案是这样的:
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。
另外,检查名为 llvm-defaults (或 gcc-defaults)的包,它可能提供其他方法来执行此操作(我没有使用它)。
I found this problem while trying to install a new clang compiler. Turns out that both the Debian and the LLVM maintainers agree that the alternatives system should be used for alternatives, NOT for versioning.
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.
Also, check for a package named llvm-defaults (or gcc-defaults), which might offer other way to do this (I didn't use it).
如果您想要一种更快(但仍然非常干净)的方式来实现个人目的(例如,如果您想构建一个对编译器版本有强烈要求的特定项目),只需按照以下步骤
echo $PATH
并查找具有非常高优先级的个人目录(在我的例子中,我有~/.local/bin
);例如:
当然,这适用于单个用户(它不是系统范围的解决方案),但另一方面,我不喜欢在安装中更改太多内容。
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:
echo $PATH
and look for a personal directory having a very high priority (in my case, I have~/.local/bin
);For instance:
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.
我只使用了下面的几行并且它起作用了。我只想在
Ubuntu 14.04
上使用kernel 4.8.10
编译VirtualBox
和VMWare WorkStation
。最初,大多数事情都无法正常工作,例如图形和网络。我很幸运,VMWare 工作站
请求了gcc 6.2.0
。我无法启动我的Genymotion Android 模拟器
,因为 virtualbox 已关闭。如果有必要,稍后会发布结果。I used just the lines below and it worked. I just wanted to compile
VirtualBox
andVMWare WorkStation
usingkernel 4.8.10
onUbuntu 14.04
. Initially, most things were not working for example graphics and networking. I was lucky thatVMWare workstation
requested forgcc 6.2.0
. I couldn't start myGenymotion Android emulators
because virtualbox was down. Will post results later if necessary.