很多服务器喜欢用Ubuntu的各种LTS,13.04还好,反正随便更新下就完事,偏偏有些没个调的货要我在他的12.04上跑个必须要c++11才支持的东西,所以不得不出手装个gcc 4.8.x

更新一些需要的内容

$ sudo apt-get install libmpfr-dev libgmp3-dev libmpc-dev flex bison

然后下个包

$ wget http://ftp.gnu.org/gnu/gcc/gcc-4.8.2/gcc-4.8.2.tar.bz2
$ tar xjvf gcc-4.8.2.tar.bz2
$ mkdir gcc-4.8.2_build
$ cd gcc-4.8.2_build/
$ ../gcc-4.8.2/configure --disable-checking --enable-languages=c,c++ \
  --enable-multiarch --enable-shared --enable-threads=posix \
  --program-suffix=4.8.2 --without-included-gettext --with-system-zlib \
  --with-tune=generic --prefix=/usr
$ make -j8
$ sudo make install

运气好的话,你的/usr/bin目录下就会多一个gcc4.8.2,试试用 gcc4.8.2 编译下,能用就可以用如下命令设置为默认的

$ cd /usr/bin
$ sudo mv gcc gcc.old
$ sudo ln -sf gcc4.8.2 gcc

然后就可以快乐的使用std=c++11了!

但是运气不好的话,可能还要fix一些问题。

如果出现错误如下:

In file included from /usr/include/stdio.h:28:0,
                 from ../../../svnsrc/libgcc/../gcc/tsystem.h:88,
                 from ../../../svnsrc/libgcc/libgcc2.c:29:
/usr/include/features.h:324:26: fatal error: bits/predefs.h: No such file or directory
 #include <bits/predefs.h>
                          ^
compilation terminated.

这是因为缺少了gcc-multilib。

sudo apt-get install gcc-multilib

这样应该可以继续编译了。

如果出现错误如下:

/usr/src/gcc-4.8.2/host-x86_64-unknown-linux-gnu/gcc/xgcc -B/usr/src/gcc-4.8.2/host-x86_64-unknown-linux-gnu/gcc/ -B/bin/x86_64-unknown-linux-gnu/bin/ -B/bin/x86_64-unknown-linux-gnu/lib/ -isystem /bin/x86_64-unknown-linux-gnu/include -isystem /bin/x86_64-unknown-linux-gnu/sys-include    -O2  -g -O2 -DIN_GCC   -W -Wall -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition  -isystem ./include   -fpic -mlong-double-80 -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector  -shared -nodefaultlibs -Wl,--soname=libgcc_s.so.1 -Wl,--version-script=libgcc.map -o 32/libgcc_s.so.1.tmp -g -O2 -m32 -B./ _muldi3_s.o _negdi2_s.o _lshrdi3_s.o _ashldi3_s.o _ashrdi3_s.o _cmpdi2_s.o _ucmpdi2_s.o _clear_cache_s.o _trampoline_s.o __main_s.o _absvsi2_s.o _absvdi2_s.o _addvsi3_s.o _addvdi3_s.o _subvsi3_s.o _subvdi3_s.o _mulvsi3_s.o _mulvdi3_s.o _negvsi2_s.o _negvdi2_s.o _ctors_s.o _ffssi2_s.o _ffsdi2_s.o _clz_s.o _clzsi2_s.o _clzdi2_s.o _ctzsi2_s.o _ctzdi2_s.o _popcount_tab_s.o _popcountsi2_s.o _popcountdi2_s.o _paritysi2_s.o _paritydi2_s.o _powisf2_s.o _powidf2_s.o _powixf2_s.o _powitf2_s.o _mulsc3_s.o _muldc3_s.o _mulxc3_s.o _multc3_s.o _divsc3_s.o _divdc3_s.o _divxc3_s.o _divtc3_s.o _bswapsi2_s.o _bswapdi2_s.o _clrsbsi2_s.o _clrsbdi2_s.o _fixunssfsi_s.o _fixunsdfsi_s.o _fixunsxfsi_s.o _fixsfdi_s.o _fixdfdi_s.o _fixxfdi_s.o _fixunssfdi_s.o _fixunsdfdi_s.o _fixunsxfdi_s.o _floatdisf_s.o _floatdidf_s.o _floatdixf_s.o _floatundisf_s.o _floatundidf_s.o _floatundixf_s.o _divdi3_s.o _moddi3_s.o _udivdi3_s.o _umoddi3_s.o _udiv_w_sdiv_s.o _udivmoddi4_s.o cpuinfo_s.o tf-signs_s.o sfp-exceptions_s.o addtf3_s.o divtf3_s.o eqtf2_s.o getf2_s.o letf2_s.o multf3_s.o negtf2_s.o subtf3_s.o unordtf2_s.o fixtfsi_s.o fixunstfsi_s.o floatsitf_s.o floatunsitf_s.o fixtfdi_s.o fixunstfdi_s.o floatditf_s.o floatunditf_s.o extendsftf2_s.o extenddftf2_s.o extendxftf2_s.o trunctfsf2_s.o trunctfdf2_s.o trunctfxf2_s.o enable-execute-stack_s.o unwind-dw2_s.o unwind-dw2-fde-dip_s.o unwind-sjlj_s.o unwind-c_s.o emutls_s.o libgcc.a -lc && rm -f 32/libgcc_s.so && if [ -f 32/libgcc_s.so.1 ]; then mv -f 32/libgcc_s.so.1 32/libgcc_s.so.1.backup; else true; fi && mv 32/libgcc_s.so.1.tmp 32/libgcc_s.so.1 && ln -s libgcc_s.so.1 32/libgcc_s.so
/usr/bin/ld: cannot find crti.o: No such file or directory
collect2: error: ld returned 1 exit status
make[5]: *** [libgcc_s.so] Error 1
make[5]: Leaving directory `/usr/src/gcc-4.8.2/x86_64-unknown-linux-gnu/32/libgcc'

这种是因为libc的问题。 比如安装下libc6-dev什么的可能是个解决方法。但一般这玩意儿早就装了,ld为毛找不到crti.o呢?因为这货藏在/usr/lib/x86_64-linux-gnu 之类的目录下,而ld的搜寻路径不对。

LIBRARY_PATH=/usr/lib/x86_64-linux-gnu:$LIBRARY_PATH 
export LIBRARY_PATH 

或者对32bit的

LIBRARY_PATH=/usr/lib/i386-linux-gnu:$LIBRARY_PATH 
export LIBRARY_PATH

这样就ld就能找到了。

为了让它能一直起效,也可以将这个路径加入 /etc/ld.so.conf 或者自己写个.conf文件一行一个路径加入/etc/ld.so.conf.d/目录下,不重启ldconfig即可生效。

还发现个这样的错误

configure: error: 
*** LIBRARY_PATH shouldn't contain the current directory when
*** building gcc. Please change the environment variable
*** and run configure again.

这错误如内容所示LIBRARY_PATH变量中不能包含当前目录,按照提示去掉当前目录即可。

distribution无关的下载编译和安装

如果不是使用Ubuntu系列,只要有网,其实也不困难。 首先下载gcc的source包同上。然后执行gcc-4.8.2/contrib/download_prerequisites文件,它会自动下载对应版本相关的包,包括mpr,gmp之类。 然后后续步骤和前面一样,修改下参数也一样用。

x86_64的CentOS下出现一个错误如下:

In file included from /usr/include/features.h:385:0,
                 from /usr/include/stdio.h:28,
                 from ../../../../gcc-4.8.3/libgcc/../gcc/tsystem.h:87,
                 from ../../../../gcc-4.8.3/libgcc/libgcc2.c:27:
/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory
 # include <gnu/stubs-32.h>

这是因为编译32位gcc的时候出的问题。fix如下: 对CentOS 5.x

# yum -y install glibc-devel.i386 glibc-devel

对CentOS 6.x

# yum -y install glibc-devel.i686 glibc-devel

对Debian/Ubuntu系列

$ sudo apt-get install g++-multilib libc6-dev-i386

附: 编译 8.2.0 configure 参数

../gcc-8.2.0/configure --prefix=/usr/local/opt/gcc/8.2.0 \
  --disable-checking --enable-languages=c,c++  \
  --enable-multiarch --enable-shared --enable-threads=posix  \
  --without-included-gettext --with-system-zlib  \
  --with-tune=generic --disable-multilib \
  --program-suffix=8.2.0

References

  1. gcc cannot find bits/predefs.h on i686
  2. cannot find crti.o: No such file or directory
  3. Compiling problems: cannot find crt1.o
  4. x86_64 Linux Error: gnu/stub-32.h Missing Error and Solution
Categories: Code

Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

2 Comments

屠龙 · May 31, 2014 at 20:52

Google Chrome 35.0.1916.114 Google Chrome 35.0.1916.114 Windows XP Windows XP

请问一下gcc是什么意思。

Leave a Reply

Your email address will not be published. Required fields are marked *