简体   繁体   English

在不同的GCC版本之间切换

[英]Switch between different GCC versions

I recently built an older version of GCC and installed it in my home directory (spec. ~/local/gcc-5.3.0 ). 我最近构建了一个旧版本的GCC并将其安装在我的主目录(spec . ~/local/gcc-5.3.0 )中。 However, I need this compiler only for CUDA projects, and will be working with the system compiler (GCC 6.2.1) rest of the time. 但是,我只需要这个编译器用于CUDA项目,并且将在其余时间使用系统编译器(GCC 6.2.1)。 So, I guess I need to find a way to switch between these as and when needed, and in a way that also changes the library and include paths appropriately. 所以,我想我需要找到一种在需要时和在需要时切换它们的方法,并且还可以更改库并适当地包含路径。

I understand that update-alternatives is one way to do so, but it seems to require root permissions to be set up, which I don't have. 我知道update-alternatives是这样做的一种方法,但它似乎需要设置root权限,这是我没有的。

The next best thing might be to write a shell function in .bashrc that ensures the following: 下一个最好的事情可能是在.bashrc中编写一个shell函数来确保以下内容:

  • Each call switches between system and local gcc 每个呼叫在系统和本地gcc之间切换

  • Whenever a switch is made, it adjusts paths so that when local gcc is chosen, it first looks for header files and libraries that were installed by itself before looking in system paths like /usr/local/include or usr/local/lib . 无论何时进行切换,它都会调整路径,以便在选择本地gcc时,它首先查找自己安装的头文件和库,然后再查看/usr/local/includeusr/local/lib等系统路径。 A previous answer suggests that modifying LD_LIBRARY_PATH should be sufficient, because a GCC installation "knows" where its own header files and static libraries are (I am not sure if it's correct, I was thinking I might need to modify CPATH , etc). 之前的回答表明修改LD_LIBRARY_PATH应该足够了,因为GCC安装“知道”它自己的头文件和静态库的位置(我不确定它是否正确,我以为我可能需要修改CPATH等)。

Is the above the best way to achieve this? 以上是实现这一目标的最佳方法吗? If so, what paths should I set while implementing such a function? 如果是这样,我在实现这样的功能时应该设置哪些路径?

Is the above the best way to achieve this? 以上是实现这一目标的最佳方法吗? If so, what paths should I set while implementing such a function? 如果是这样,我在实现这样的功能时应该设置哪些路径?

As others pointed out, PATH and LD_LIBRARY_PATH are mandatory. 正如其他人所指出的, PATHLD_LIBRARY_PATH是强制性的。 You may also update MANPATH for completeness. 您也可以更新MANPATH以获得完整性。

Rather than reinventing the wheel in .bashrc I suggest to employ a little known but extremely handy and modular Environment Modules that were designed for this specific purpose. 而不是重新发明.bashrc的轮子我建议使用一些鲜为人知但非常方便和模块化的环境模块 ,专为此特定目的而设计。 You could use them like (once you set up config for gcc/3.1.1): 你可以使用它们(一旦你为gcc / 3.1.1设置配置):

$ module load gcc/3.1.1
$ which gcc
/usr/local/gcc/3.1.1/linux/bin/gcc
$ module unload gcc
$ which gcc
gcc not found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM