如何在 Linux Ubuntu 上使用 2 个不同版本的 GCC 并强制 MAKE 使用其中之一

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4152283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:55:56  来源:igfitidea点击:

How to use 2 different versions of GCC on Linux Ubuntu and force MAKE to use one of them

linuxgcccompiler-constructionubuntu

提问by blackLabrador

I'm using the last version of Ubuntu which come with the gcc 4.4.5 version. I need to recompile a program that was not written by me and that can be only compiled with an older version of gcc like the 4.0. I managed to configure this older version and used a prefix during the install process so that my old gcc version is in the /opt/gcc-4.0.1/bin. I have tried to create a symlink using ln -s /opt/gcc-4.0.1/bin/gcc gcc. But when I invoke gcc -vI still get the result gcc version 4.4.5. To compile my program which come already with a makefile, if I do make, it's still using the new version of gcc. How could I tell maketo use the old version?

我正在使用 gcc 4.4.5 版本附带的最新版本的 Ubuntu。我需要重新编译一个不是我写的程序,它只能用旧版本的 gcc 编译,比如 4.0.1。我设法配置了这个旧版本并在安装过程中使用了一个前缀,以便我的旧 gcc 版本位于 /opt/gcc-4.0.1/bin 中。我尝试使用ln -s /opt/gcc-4.0.1/bin/gcc gcc. 但是当我调用时,gcc -v我仍然得到结果gcc version 4.4.5。要编译已经带有 makefile 的程序,如果我这样做make,它仍在使用新版本的 gcc。我怎么知道make要使用旧版本?

采纳答案by falstro

Make uses some standard variables in order to determine which tools to use, the C-compiler variable is called "CC". You can set the CC variable, either directly in your Makefile

Make 使用一些标准变量来确定使用哪些工具,C 编译器变量称为“CC”。您可以直接在 Makefile 中设置 CC 变量

CC=/opt/gcc-4.0.1/bin/gcc

which is fine if you're working alone on it, or everyone has the same setup. Or you can pass it on the command line like so:

如果你是单独工作,或者每个人都有相同的设置,这很好。或者你可以像这样在命令行上传递它:

make CC=/opt/gcc-4.0.1/bin/gcc

the third option is set /opt/gcc-4.0.1/bin before everything else in your path (which is why it doesn't work for you, the current directory isn't in the path, so the symlink you put there will not be considered when searching)

第三个选项是在路径中的其他所有内容之前设置 /opt/gcc-4.0.1/bin (这就是为什么它对你不起作用,当前目录不在路径中,所以你放在那里的符号链接会搜索时不考虑)

export PATH=/opt/gcc-4.0.1/bin:$PATH

For completeness, in your symlink solution, you'd have to invoke ./gccto get the right gcc instance, but IMHO this is probably not the best solution.

为了完整起见,在您的符号链接解决方案中,您必须调用./gcc以获得正确的 gcc 实例,但恕我直言,这可能不是最佳解决方案。

HTH

HTH