如何在 Linux 中使用旧版本的 gcc
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3698441/
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 use an older version of gcc in Linux
提问by node ninja
In Linux I am trying to compile something that uses the -fwritable-strings option. Apparently this is a gcc option that doesn't work in newer version of gcc. I installed gcc-3.4 on my system, but I think the newer version is still being used because I'm still get the error that says it can't recognize the command line option -fwritable-strings. How can I get make to use the older version of gcc?
在 Linux 中,我试图编译使用 -fwritable-strings 选项的东西。显然,这是一个 gcc 选项,在较新版本的 gcc 中不起作用。我在系统上安装了 gcc-3.4,但我认为仍在使用较新的版本,因为我仍然收到错误消息,指出它无法识别命令行选项 -fwritable-strings。如何让 make 使用旧版本的 gcc?
回答by Michael Aaron Safyan
If you can find where the writeable strings are actually being used, another possibility would be to use strdupand freeon the subset of literal strings that the code is actually editing. This might be more complicated than downgrading versions of GCC, but will make the code much more portable.
如果您可以找到实际使用可写字符串的位置,另一种可能性是在代码实际编辑的文字字符串子集上使用strdup和free。这可能比降级 GCC 版本更复杂,但会使代码更具可移植性。
Edit
In response to the clarification question / comment below, if you saw something like:
编辑
回应下面的澄清问题/评论,如果您看到类似的内容:
char* str = "XXX";
str[1] = 'Y';
str[2] = 'Z';
// ... use of str ...
You would replace the above with something like:
您可以用以下内容替换上述内容:
char* str = strdup("XXX");
str[1] = 'Y';
str[2] = 'Z';
// ... use of str ...
free(str);
And where you previously had:
以及您以前拥有的地方:
char* str = "Some string that isn't modified";
You would replace the above with:
您可以将上述内容替换为:
const char* str = "Some string that isn't modified";
Assuming you made these fixes, "-fwritable-strings" would no longer be necessary.
假设您进行了这些修复,则不再需要“-fwritable-strings”。
回答by itisravi
Maybe you could just give the whole path of the gcc-3.4 install while compiling your program: /path_to_gcc_3.4/gcc your_program
也许你可以在编译你的程序时给出 gcc-3.4 安装的完整路径:/path_to_gcc_3.4/gcc your_program
回答by Hyman Kelly
You say nothing about the build system in use, but usually old versions of gcc can be invoked explicitly, by something like (this is for an autotools-based build):
您没有提及正在使用的构建系统,但通常可以通过类似(这是基于自动工具的构建)显式调用旧版本的 gcc:
./configure CXX=g++-3.4 CC=gcc-3.4
For a make-based build system, sometimes this will work:
对于基于 make 的构建系统,有时这会起作用:
make CXX=g++-3.4 CC=gcc-3.4
Most makefiles ought to recognise overriding CC
and CXX
in this way.
大多数的makefile应该认识到覆盖CC
并CXX
以这种方式。
回答by Aaron V
If editing the configuration/Makefile is not an option, Linux includes a utility called update-alternatives
for such situations. However, it's a pain to use (links to various tutorials included below).
如果编辑配置/Makefile 不是一个选项,Linux 包含一个update-alternatives
用于此类情况的实用程序。但是,使用起来很痛苦(链接到下面包含的各种教程)。
This is a little simpler - here's a script (from here) to easily switch your default gcc
/g++
version:
这有点简单 - 这是一个脚本(来自这里)可以轻松切换您的默认gcc
/g++
版本:
#!/bin/bash
usage() {
echo
echo Sets the default version of gcc, g++, etc
echo Usage:
echo
echo " gcc-set-default-version <VERSION>"
echo
exit
}
cd /usr/bin
if [ -z ] ; then
usage;
fi
set_default() {
if [ -e "-" ] ; then
echo - is now the default
ln -sf -
else
echo - is not installed
fi
}
for i in gcc cpp g++ gcov gccbug ; do
set_default $i
done
If you 1) name this script switch-gcc
, 2) put it in your path, and 3) make it executable (chmod +x switch-gcc
), you can then switch compiler versions just by running
如果您 1) 命名此脚本switch-gcc
,2) 将其放在您的路径中,并且 3) 使其可执行 ( chmod +x switch-gcc
),那么您只需运行即可切换编译器版本
sudo switch-gcc 3.2
sudo switch-gcc 3.2
Further reading on update-alternatives
:
进一步阅读update-alternatives
: