在redhat9.0上源码编译gcc2.9.5过程记录
时间:2009-03-25 来源:tao1988tao2004
(先发几个以前的旧文)
花了我一晚上的时间终于搞定了,本来虚拟机里面有了一个gcc3.2.2。想多装一个gcc试试。
在网上找来找一些文章,熟悉了一下流程以及有可能碰到的问题,后来还真是碰到了二个问题,在网上也没怎么找到,先将其记录如下:
(以下有部分抄袭,万望作者勿怪,:))
-------------------美好的分割线-------copy---start-----!--------------
系统上原来的GCC编译器可能是把gcc等命令文件、库文件、头文件等分别存放到系统中的不同目录下的。与此不同,现在GCC建议我们将一个版本的GCC安装在一个单独的目录下。这样做的好处是将来不需要它的时候可以方便地删除整个目录即可(因为GCC没有uninstall功能);缺点是在安装完成后要做一些设置工作才能使编译器工作正常。在本文中我采用这个方案安装GCC 2.9.5,并且在安装完成后,仍然能够使用原来低版本的GCC编译器,即一个系统上可以同时存在并使用多个版本的GCC编译器。
先是下载和解压缩
GCC mirror sites:http://gcc.gnu.org/mirrors.html
其中一个下载网站:ftp://ftp.dti.ad.jp/pub/lang/gcc/releases/
% tar xzvf gcc-2.9.5.tar.gz
新生成的gcc-2.9.5这个目录被称为源目录,用${srcdir}表示它。以后在出现${srcdir}的地方,应该用真实的路径来替换它。用pwd命令可以查看当前路径。
在${srcdir}/INSTALL目录下有详细的GCC安装说明,可用浏览器打开index.html阅读。
3. 建立目标目录
目标目录(用${objdir}表示)是用来存放编译结果的地方。GCC建议编译后的文件不要放在源目录${srcdir]中(虽然这样做也可以),最好单独存放在另外一个目录中,而且不能是${srcdir}的子目录。
例如,可以这样建立一个叫 gcc-build 的目标目录(与源目录${srcdir}是同级目录):
% mkdir gcc-build
% cd gcc-build
以下的操作主要是在目标目录 ${objdir} 下进行。
4. 配置
配置的目的是决定将GCC编译器安装到什么地方(${destdir}),支持什么语言以及指定其它一些选项等。其中,${destdir}不能与${objdir}或${srcdir}目录相同。
配置是通过执行${srcdir}下的configure来完成的。其命令格式为(记得用你的真实路径替换${destdir}):
% ${srcdir}/configure --prefix=${destdir} [其它选项]
例如,如果想将GCC 2.9.5安装到/usr/local/gcc-2.9.5目录下,则${destdir}就表示这个路径。
在我的机器上,我是这样配置的:
% ../gcc-2.9.5/configure --prefix=/usr/local/gcc-2.9.5 --enable-threads=posix --disable-checking --enable--long-long --host=i386-redhat-linux --with-system-zlib --enable-languages=c,c++,java
将GCC安装在/usr/local/gcc-2.9.5目录下,支持C/C++和JAVA语言,其它选项参见GCC提供的帮助说明。
5. 编译
% make
这是一个漫长的过程。
---------------美好的分割线-----------copy ------end-----!--------------
5.1:第一个错误
../../../gcc-2.95/libio/indstream.cc: In method `struct streampos indirectbuf::seekoff(long long int, ios::seek_dir, int = 3)':
../../../gcc-2.95/libio/indstream.cc:82: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:85: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:87: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:89: conversion from `int' to non-scalar type `streampos' requested
../../../gcc-2.95/libio/indstream.cc: In method `struct streampos indirectbuf::seekpos(_G_fpos64_t, int = 3)':
../../../gcc-2.95/libio/indstream.cc:99: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:102: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:104: `struct streampos' used where a `int' was expected
../../../gcc-2.95/libio/indstream.cc:106: conversion from `int' to non-scalar type `streampos' requested
make[2]: *** [indstream.o] Error 1
make[2]: Leaving directory `/home/jlu/gcc-build/i686-pc-linux-gnu/libio'
make[1]: *** [all-target-libio] Error 2
make[1]: Leaving directory `/home/jlu/gcc-build'
make: *** [bootstrap] Error 2
解决办法:(一些解释:
一:-后面的是原文件,+后面表示的是对应的应该修改成的形式。-+对应出现。
二:@@后面是具体的行号eg:@@ -277,7 +277,7 @@ typedef struct
)
Index: libio/libio.h
===================================================================
RCS file: /cvs/gcc/egcs/libio/libio.h,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -p -r1.14 -r1.15
--- libio/libio.h 1998/06/29 18:06:26 1.14
+++ libio/libio.h 1999/12/22 09:01:59 1.15
@@ -277,7 +277,7 @@ typedef struct
{
_IO_ssize_t (*read) __PMT ((struct _IO_FILE *, void *, _IO_ssize_t));
_IO_ssize_t (*write) __PMT ((struct _IO_FILE *, const void *, _IO_ssize_t));
- _IO_fpos_t (*seek) __PMT ((struct _IO_FILE *, _IO_off_t, int));
+ _IO_off_t (*seek) __PMT ((struct _IO_FILE *, _IO_off_t, int));
int (*close) __PMT ((struct _IO_FILE *));
} _IO_cookie_io_functions_t;
@@ -348,11 +348,11 @@ extern _IO_ssize_t _IO_padn __P ((_IO_FI
extern _IO_size_t _IO_sgetn __P ((_IO_FILE *, void *, _IO_size_t));
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
-extern _IO_fpos64_t _IO_seekpos __P ((_IO_FILE *, _IO_fpos64_t, int));
+extern _IO_off64_t _IO_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
+extern _IO_off64_t _IO_seekpos __P ((_IO_FILE *, _IO_off64_t, int));
#else
-extern _IO_fpos_t _IO_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
-extern _IO_fpos_t _IO_seekpos __P ((_IO_FILE *, _IO_fpos_t, int));
+extern _IO_off_t _IO_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
+extern _IO_off_t _IO_seekpos __P ((_IO_FILE *, _IO_off_t, int));
#endif
extern void _IO_free_backup_area __P ((_IO_FILE *));
Index: libio/libioP.h
===================================================================
RCS file: /cvs/gcc/egcs/libio/libioP.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -p -r1.11 -r1.12
--- libio/libioP.h 1999/05/17 22:58:56 1.11
+++ libio/libioP.h 1999/12/22 09:01:59 1.12
@@ -146,10 +146,10 @@ typedef _IO_size_t (*_IO_xsgetn_t) __PMT
It matches the streambuf::seekoff virtual function.
It is also used for the ANSI fseek function. */
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-typedef _IO_fpos64_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off64_t OFF,
+typedef _IO_off64_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off64_t OFF,
int DIR, int MODE));
#else
-typedef _IO_fpos_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off_t OFF,
+typedef _IO_off_t (*_IO_seekoff_t) __PMT ((_IO_FILE *FP, _IO_off_t OFF,
int DIR, int MODE));
#endif
#define _IO_SEEKOFF(FP, OFF, DIR, MODE) JUMP3 (__seekoff, FP, OFF, DIR, MODE)
@@ -160,9 +160,9 @@ typedef _IO_fpos_t (*_IO_seekoff_t) __PM
It is also used for the ANSI fgetpos and fsetpos functions. */
/* The _IO_seek_cur and _IO_seek_end options are not allowed. */
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-typedef _IO_fpos64_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_fpos64_t, int));
+typedef _IO_off64_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_off64_t, int));
#else
-typedef _IO_fpos_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_fpos_t, int));
+typedef _IO_off_t (*_IO_seekpos_t) __PMT ((_IO_FILE *, _IO_off_t, int));
#endif
#define _IO_SEEKPOS(FP, POS, FLAGS) JUMP2 (__seekpos, FP, POS, FLAGS)
@@ -213,9 +213,9 @@ typedef _IO_ssize_t (*_IO_write_t) __PMT
It matches the streambuf::sys_seek virtual function, which is
specific to this implementation. */
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-typedef _IO_fpos64_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off64_t, int));
+typedef _IO_off64_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off64_t, int));
#else
-typedef _IO_fpos_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off_t, int));
+typedef _IO_off_t (*_IO_seek_t) __PMT ((_IO_FILE *, _IO_off_t, int));
#endif
#define _IO_SYSSEEK(FP, OFFSET, MODE) JUMP2 (__seek, FP, OFFSET, MODE)
@@ -298,11 +298,11 @@ struct _IO_FILE_plus
/* Generic functions */
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
-extern _IO_fpos64_t _IO_seekpos __P ((_IO_FILE *, _IO_fpos64_t, int));
+extern _IO_off64_t _IO_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
+extern _IO_off64_t _IO_seekpos __P ((_IO_FILE *, _IO_off64_t, int));
#else
-extern _IO_fpos_t _IO_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
-extern _IO_fpos_t _IO_seekpos __P ((_IO_FILE *, _IO_fpos_t, int));
+extern _IO_off_t _IO_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
+extern _IO_off_t _IO_seekpos __P ((_IO_FILE *, _IO_off_t, int));
#endif
extern void _IO_switch_to_main_get_area __P ((_IO_FILE *));
@@ -340,22 +340,22 @@ extern _IO_size_t _IO_default_xsputn __P
_IO_size_t));
extern _IO_size_t _IO_default_xsgetn __P ((_IO_FILE *, void *, _IO_size_t));
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_default_seekoff __P ((_IO_FILE *,
+extern _IO_off64_t _IO_default_seekoff __P ((_IO_FILE *,
_IO_off64_t, int, int));
-extern _IO_fpos64_t _IO_default_seekpos __P ((_IO_FILE *,
- _IO_fpos64_t, int));
+extern _IO_off64_t _IO_default_seekpos __P ((_IO_FILE *,
+ _IO_off64_t, int));
#else
-extern _IO_fpos_t _IO_default_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
-extern _IO_fpos_t _IO_default_seekpos __P ((_IO_FILE *, _IO_fpos_t, int));
+extern _IO_off_t _IO_default_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
+extern _IO_off_t _IO_default_seekpos __P ((_IO_FILE *, _IO_off_t, int));
#endif
extern _IO_ssize_t _IO_default_write __P ((_IO_FILE *, const void *,
_IO_ssize_t));
extern _IO_ssize_t _IO_default_read __P ((_IO_FILE *, void *, _IO_ssize_t));
extern int _IO_default_stat __P ((_IO_FILE *, void *));
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_default_seek __P ((_IO_FILE *, _IO_off64_t, int));
+extern _IO_off64_t _IO_default_seek __P ((_IO_FILE *, _IO_off64_t, int));
#else
-extern _IO_fpos_t _IO_default_seek __P ((_IO_FILE *, _IO_off_t, int));
+extern _IO_off_t _IO_default_seek __P ((_IO_FILE *, _IO_off_t, int));
#endif
extern int _IO_default_sync __P ((_IO_FILE *));
#define _IO_default_close ((_IO_close_t) _IO_default_sync)
@@ -389,11 +389,11 @@ extern void _IO_flush_all_linebuffered _
extern int _IO_file_doallocate __P ((_IO_FILE *));
extern _IO_FILE* _IO_file_setbuf __P ((_IO_FILE *, char *, _IO_ssize_t));
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
-extern _IO_fpos64_t _IO_file_seek __P ((_IO_FILE *, _IO_off64_t, int));
+extern _IO_off64_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
+extern _IO_off64_t _IO_file_seek __P ((_IO_FILE *, _IO_off64_t, int));
#else
-extern _IO_fpos_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
-extern _IO_fpos_t _IO_file_seek __P ((_IO_FILE *, _IO_off_t, int));
+extern _IO_off_t _IO_file_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
+extern _IO_off_t _IO_file_seek __P ((_IO_FILE *, _IO_off_t, int));
#endif
extern _IO_size_t _IO_file_xsputn __P ((_IO_FILE *, const void *, _IO_size_t));
extern int _IO_file_stat __P ((_IO_FILE *, void *));
@@ -427,9 +427,9 @@ extern int _IO_str_underflow __P ((_IO_F
extern int _IO_str_overflow __P ((_IO_FILE *, int));
extern int _IO_str_pbackfail __P ((_IO_FILE *, int));
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-extern _IO_fpos64_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
+extern _IO_off64_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off64_t, int, int));
#else
-extern _IO_fpos_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
+extern _IO_off_t _IO_str_seekoff __P ((_IO_FILE *, _IO_off_t, int, int));
#endif
extern void _IO_str_finish __P ((_IO_FILE *, int));
@@ -544,12 +544,12 @@ extern int _IO_vscanf __P ((const char *
where an _IO_fpos_t is a struct.
Note that _IO_off_t must be an integral type. */
-/* _IO_pos_BAD is an _IO_fpos_t value indicating error, unknown, or EOF. */
+/* _IO_pos_BAD is an _IO_off_t value indicating error, unknown, or EOF. */
#ifndef _IO_pos_BAD
# if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
-# define _IO_pos_BAD ((_IO_fpos64_t) -1)
+# define _IO_pos_BAD ((_IO_off64_t) -1)
# else
-# define _IO_pos_BAD ((_IO_fpos_t) -1)
+# define _IO_pos_BAD ((_IO_off_t) -1)
# endif
#endif
/* _IO_pos_as_off converts an _IO_fpos_t value to an _IO_off_t value. */
Index: libio/parsestream.h
===================================================================
RCS file: /cvs/gcc/egcs/libio/parsestream.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -p -r1.2 -r1.3
--- libio/parsestream.h 1999/09/04 15:08:50 1.2
+++ libio/parsestream.h 1999/12/22 09:01:59 1.3
@@ -42,7 +42,7 @@ extern "C++" {
class parsebuf : public streambuf {
protected:
- _IO_fpos_t pos_at_line_start;
+ _IO_off_t pos_at_line_start;
long _line_length;
unsigned long __line_number;
char *buf_start;
Index: libio/streambuf.h
===================================================================
RCS file: /cvs/gcc/egcs/libio/streambuf.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -p -r1.7 -r1.8
--- libio/streambuf.h 1999/08/09 00:07:09 1.7
+++ libio/streambuf.h 1999/12/22 09:01:59 1.8
@@ -72,10 +72,10 @@ class ostream; class streambuf;
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
typedef _IO_off64_t streamoff;
-typedef _IO_fpos64_t streampos;
+typedef _IO_off64_t streampos;
#else
typedef _IO_off_t streamoff;
-typedef _IO_fpos_t streampos;
+typedef _IO_off_t streampos;
#endif
typedef _IO_ssize_t streamsize;
--
------------------------第一个问题完成--------------------------------------------
第二个问题
错误:
> ../../../gcc-2.95.2/libio/iostream.cc:75: `_IO_USER_LOCK' undeclared (first
> usethis function)
> ../../../gcc-2.95.2/libio/iostream.cc:75: (Each undeclared identifier is
> reporte
> d only once
解决办法:
* libio.h (_IO_USER_LOCK): Define as in Red Hat 7's libio.h.
--- libio/libio.h~ Tue Oct 24 00:34:13 2000
+++ libio/libio.h Tue Oct 24 02:18:21 2000
@@ -136,6 +136,7 @@
#define _IO_IS_APPENDING 0x1000
#define _IO_IS_FILEBUF 0x2000
#define _IO_BAD_SEEN 0x4000
+#define _IO_USER_LOCK 0x8000
/* These are "formatting flags" matching the iostream fmtflags enum values. */
#define _IO_SKIPWS 01
-----------------------------------------------第二个问题完成---------------------
6. 安装
执行下面的命令将编译好的库文件等拷贝到${destdir}目录中(根据你设定的路径,可能需要管理员的权限):
% make install
至此,GCC 2.9.5安装过程就完成了。
7. 其它设置
GCC 2.9.5的所有文件,包括命令文件(如gcc、g++)、库文件等都在${destdir}目录下分别存放,如命令文件放在bin目录下、库文件在lib下、头文件在include下等。由于命令文件和库文件所在的目录还没有包含在相应的搜索路径内,所以必须要作适当的设置之后编译器才能顺利地找到并使用它们。
7.1 gcc、g++、gcj的设置
要想使用GCC 2.9.5的gcc等命令,简单的方法就是把它的路径${destdir}/bin放在环境变量PATH中。我不用这种方式,而是用符号连接的方式实现,这样做的好处是我仍然可以使用系统上原来的旧版本的GCC编译器。
首先,查看原来的gcc所在的路径:
% which gcc
在我的系统上,上述命令显示:/usr/bin/gcc。因此,原来的gcc命令在/usr/bin目录下。我们可以把GCC 2.9.5中的gcc、g++、gcj等命令在/usr/bin目录下分别做一个符号连接:
% cd /usr/bin
% ln -s ${destdir}/bin/gcc gcc295
% ln -s ${destdir}/bin/g++ g++295
% ln -s ${destdir}/bin/gcj gcj295
这样,就可以分别使用gcc295、g++295、gcj295来调用GCC 2.9.5的gcc、g++、gcj完成对C、C++、JAVA程序的编译了。同时,仍然能够使用原版本的GCC编译器中的gcc、g++等命令。
7.2 库路径的设置
将${destdir}/lib路径添加到环境变量LD_LIBRARY_PATH中,最好添加到系统的配置文件中,这样就不必要每次都设置这个环境变量了。
例如,如果GCC 2.9.5安装在/usr/local/gcc-2.9.5目录下,在RH Linux下可以直接在命令行上执行或者在文件/etc/profile中添加下面一句:
setenv LD_LIBRARY_PATH /usr/local/gcc-2.9.5/lib (或者是用export命令)
8. 测试
用新的编译命令(gcc295、g++295等)编译你以前的C、C++程序,检验新安装的GCC编译器是否能正常工作。
9. 根据需要,可以删除或者保留${srcdir}和${objdir}目录。
参考资料:
Installing GCC ( http://gcc.gnu.org/install/ )
ps:这基本是上个月就做好的工作,前面写了个草稿就放在那里,好几次想补全,都没做,今天终于写完了,不过也就是综合了一下,算是把他记录下来了吧。我也该去开一个技术blog了,记录下来!这里就写一下比较见不得人的东西吧!