linux 编译安装GCC4.9.3(完整版)

第一步

  • 首先下载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
  • 14
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
安装GCC编译器的步骤如下: 1. 打开终端,输入以下命令安装GCC编译器: ``` sudo apt-get update sudo apt-get install gcc ``` 2. 安装完毕后,可以输入以下命令检查GCC版本: ``` gcc --version ``` 如果出现GCC的版本信息,则表示GCC编译器已成功安装。 在Linux下使用GCC编译器时,常见的问题及解决方法如下: 1. 缺少标准库文件 在编译时,有时会出现缺少标准库文件的情况,解决方法是安装相应的标准库文件。例如,在Ubuntu系统中,可以通过以下命令安装C标准库文件: ``` sudo apt-get install libc6-dev ``` 2. 编译时出现“undefined reference”错误 出现“undefined reference”错误通常是因为链接时缺少相应的库文件。解决方法是在编译时加上对应的库文件。例如,在链接时需要使用math库,可以在编译命令中加上“-lm”参数: ``` gcc test.c -o test -lm ``` 3. 编译时出现“error: ‘for’ loop initial declarations are only allowed in C99 mode”错误 出现这个错误是因为在C语言标准中,只有C99标准及以上才支持在for循环中定义变量。解决方法是在编译命令中加上“-std=c99”参数: ``` gcc test.c -o test -std=c99 ``` 4. 编译时出现“fatal error: xxx.h: No such file or directory”错误 出现这个错误通常是因为编译器找不到相应的头文件。解决方法是在编译命令中加上头文件所在的目录。例如,头文件在“/usr/include”目录下,可以在编译命令中加上“-I/usr/include”参数: ``` gcc test.c -o test -I/usr/include ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值