How to use an older version of gcc in Linux

前端 未结 4 1193
太阳男子
太阳男子 2020-12-20 13:39

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 gc

相关标签:
4条回答
  • 2020-12-20 14:05

    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).

    This is a little simpler - here's a script (from here) to easily switch your default gcc/g++ version:

    #!/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 $1 ] ; then 
            usage;
    fi 
    set_default() {
            if [ -e "$1-$2" ] ; then 
                    echo $1-$2 is now the default
                    ln -sf $1-$2 $1
            else 
                    echo $1-$2 is not installed
            fi
    }
    for i in gcc cpp g++ gcov gccbug ; do 
            set_default $i $1
    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

    sudo switch-gcc 3.2

    Further reading on update-alternatives:

    • https://lektiondestages.blogspot.com/2013/05/installing-and-switching-gccg-versions.html
    • https://codeyarns.com/2015/02/26/how-to-switch-gcc-version-using-update-alternatives/
    • https://askubuntu.com/questions/26498/choose-gcc-and-g-version
    0 讨论(0)
  • 2020-12-20 14:13

    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

    0 讨论(0)
  • 2020-12-20 14:17

    If you can find where the writeable strings are actually being used, another possibility would be to use strdup and free on 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.

    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.

    0 讨论(0)
  • 2020-12-20 14:23

    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):

    ./configure CXX=g++-3.4 CC=gcc-3.4
    

    For a make-based build system, sometimes this will work:

    make CXX=g++-3.4 CC=gcc-3.4
    

    Most makefiles ought to recognise overriding CC and CXX in this way.

    0 讨论(0)
提交回复
热议问题