第一步

  • 首先下载gcc源码包
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-4.9.3/gcc-4.9.3.tar.bz2

第二步

  • 解压文件,做一些准备工作
tar xjvf gcc-4.9.3.tar.bz2
cd gcc-4.9.3
./contrib/download_prerequisites
  • 安装gcc需要5个组件,download_prerequisites 的任务就是下载这些组件分别是:
cloog-0.18.1
gmp-4.3.2
isl-0.12.2
mpc-0.8.1
mpfr-2.4.2
  • 如果遇到download_prerequisites里面的地址无法访问
    推荐自行下载这些组件到目录gcc-4.9.3/,解压。
    然后将download_prerequisites里面的wget全部注释掉,再执行 :
./contrib/download_prerequisites
  • 做好上面的准备就可以configure了,建议另建一个目录来存放编译文件,默认安装目录是 /usr/local/ 可以使用 --prefix 修改自定义路径。
cd ..
mkdir gcc-4.9.3-build-temp
cd gcc-4.9.3-build-temp
../gcc-4.9.3/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
make -j4
make install
  • 编译过程中报莫名其妙的错误,尝试使用root用户或者root权限编译

  • 编译过程很慢,一切顺利的话,取决于你的配置40分钟-1个小时后再来看结果吧,我用公司的16核服务器编译了20分钟

第三步

  • 在.bashrc中配置库文件和头文件路径
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64/:$LD_LIBRARY_PATH
export C_INCLUDE_PATH=/usr/local/include/:$C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH=/usr/local/include/:$CPLUS_INCLUDE_PATH
  • 执行 gcc -v 可以看到下面的信息, 恭喜你成功一半了!
gcc -v
使用内建 specs。
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-unknown-linux-gnu/4.9.3/lto-wrapper
目标:x86_64-unknown-linux-gnu
配置为:../gcc-4.9.3/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
线程模型:posix
gcc 版本 4.9.3 (GCC) 


  • 如果在调试过程中某些库无法定位到源文件,例如 std::string 组件就定位到了我的编译目录.
像这样:
/root/gcc-4.9.3/gcc-build-4.9.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/basic_string.h: No such file or directory

/root/gcc-4.9.3/gcc-build-4.9.3/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/allocator.h: No such file or directory
  • 具体原因没细查~ 可能是因为 libstdc+±v3是一个过时的库,没有默认安装到/usr/local/include/
  • 其实不是没有这个文件,而是非root用户是没有权限访问

第四步

  • 难道上面不是已经完成安装了吗? 如果你只是编译不调试的话,确实安装完毕,不用往下看了

  • 很多教程都没有提到这一点,请继续看下一步


第五步

  • 升级完gcc还需要升级gdb!!! 因为版本不匹配导致调试的时候 print变量提示找不到符号,以及其他莫名其妙的问题…

  • 这里我选择安装的时候gdb-7.9 ,安装gdb需要4个组件 ,python,python-devel ,texinfo,ncurses-devel

  • 编译gdb时报错误 configure: error: no termcap library found 是缺少 ncurses-devel

  • 编译过程中遇到莫名奇妙的报错,例如:ser-unix.c:118:1: error: conflicting types for ‘get_tty_state’,清理干净configure目录,使用root用户安装就好了

yum install texinfo
yum install ncurses-devel
yum install python
yum install python-devel
wget http://mirrors.ustc.edu.cn/gnu/gdb/gdb-7.9.tar.gz
tar xzvf gdb-7.9.tar.gz
cd gdb-7.9
./configure --with-python
make
make install
  • 如果在安装过程中提示找不到makeinfo命令或其他错误,切换root用户再试一次

  • 没有python的支持也可以编译,去掉–with-python选项就行了,但是gdb是无法加载python脚本的,例如c++ 的 pretty-printer 脚本是无法使用的

备注

最近升级gcc-9.3 和 gdb-9.2 感觉新版本升级更加方便了,但是编译仍然报各种错误,切换使用root权限make可以顺利通过编译!

报错

libc_name_p 问题

In file included from /data/work/tools/gcc-4.8.5/gcc/cp/except.c:1008:0:
cfns.gperf: In function ‘const char* libc_name_p(const char*, unsigned int)’:
cfns.gperf:101:1: error: ‘const char* libc_name_p(const char*, unsigned int)’ redeclared inline with ‘gnu_inline’ attribute
cfns.gperf:26:14: note: ‘const char* libc_name_p(const char*, unsigned int)’ previously declared here
cfns.gperf: At global scope:
cfns.gperf:26:14: warning: inline function ‘const char* libc_name_p(const char*, unsigned int)’ used but never defined
Makefile:1059: recipe for target 'cp/except.o' failed
make[3]: *** [cp/except.o] Error 1
diff --git a/gcc/cp/cfns.gperf b/gcc/cp/cfns.gperf
index 2784edc..8d7f015 100644 (file)
--- a/gcc/cp/cfns.gperf
+++ b/gcc/cp/cfns.gperf
 
 
@@ -1,3 +1,5 @@
+%language=C++
+%define class-name libc_name
 %{
 /* Copyright (C) 2000-2016 Free Software Foundation, Inc.
 
@@ -16,14 +18,6 @@ for more details.
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
-#ifdef __GNUC__
-__inline
-#endif
-static unsigned int hash (const char *, unsigned int);
-#ifdef __GNUC__
-__inline
-#endif
-const char * libc_name_p (const char *, unsigned int);
 %}
 %%
 # The standard C library functions, for feeding to gperf; the result is used
diff --git a/gcc/cp/cfns.h b/gcc/cp/cfns.h
index d8e02c9..9a37249 100644 (file)
--- a/gcc/cp/cfns.h
+++ b/gcc/cp/cfns.h
 
 
 
 
@@ -1,5 +1,5 @@
-/* ANSI-C code produced by gperf version 3.0.3 */
-/* Command-line: gperf -o -C -E -k '1-6,$' -j1 -D -N libc_name_p -L ANSI-C cfns.gperf  */
+/* C++ code produced by gperf version 3.0.4 */
+/* Command-line: gperf -o -C -E -k '1-6,$' -j1 -D -N libc_name_p -L C++ --output-file cfns.h cfns.gperf  */
 
 #if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
       && ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
 
 
 
 
@@ -28,7 +28,7 @@
 #error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gnu-gperf@gnu.org>."
 #endif
 
-#line 1 "cfns.gperf"
+#line 3 "cfns.gperf"
 
 /* Copyright (C) 2000-2016 Free Software Foundation, Inc.
 
@@ -47,25 +47,18 @@ for more details.
 You should have received a copy of the GNU General Public License
 along with GCC; see the file COPYING3.  If not see
 <http://www.gnu.org/licenses/>.  */
-#ifdef __GNUC__
-__inline
-#endif
-static unsigned int hash (const char *, unsigned int);
-#ifdef __GNUC__
-__inline
-#endif
-const char * libc_name_p (const char *, unsigned int);
 /* maximum key range = 391, duplicates = 0 */
 
-#ifdef __GNUC__
-__inline
-#else
-#ifdef __cplusplus
-inline
-#endif
-#endif
-static unsigned int
-hash (register const char *str, register unsigned int len)
+class libc_name
+{
+private:
+  static inline unsigned int hash (const char *str, unsigned int len);
+public:
+  static const char *libc_name_p (const char *str, unsigned int len);
+};
+
+inline unsigned int
+libc_name::hash (register const char *str, register unsigned int len)
 {
   static const unsigned short asso_values[] =
     {
 
 
 
 
 
@@ -122,14 +115,8 @@ hash (register const char *str, register unsigned int len)
   return hval + asso_values[(unsigned char)str[len - 1]];
 }
 
-#ifdef __GNUC__
-__inline
-#ifdef __GNUC_STDC_INLINE__
-__attribute__ ((__gnu_inline__))
-#endif
-#endif
 const char *
-libc_name_p (register const char *str, register unsigned int len)
+libc_name::libc_name_p (register const char *str, register unsigned int len)
 {
   enum
     {
diff --git a/gcc/cp/except.c b/gcc/cp/except.c
index c73a16b..5336710 100644 (file)
--- a/gcc/cp/except.c
+++ b/gcc/cp/except.c
 
 
 
@@ -1040,7 +1040,8 @@ nothrow_libfn_p (const_tree fn)
      unless the system headers are playing rename tricks, and if
      they are, we don't want to be confused by them.  */
   id = DECL_NAME (fn);
-  return !!libc_name_p (IDENTIFIER_POINTER (id), IDENTIFIER_LENGTH (id));
+  return !!libc_name::libc_name_p (IDENTIFIER_POINTER (id),
+                                  IDENTIFIER_LENGTH (id));
 }
 
 /* Returns nonzero if an exception of type FROM will be caught by a

提示找不到 -lstdc++

yum install libstdc++-static
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐