中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
[原创] 安装 GCC 编译器
作者:未知 时间:2005-09-13 19:28 出处:ChinaUnix.net 责编:chinaitpower
              摘要:[原创] 安装 GCC 编译器

2004年4月20日最新版本的GCC编译器3.4.0发布了。目前,GCC可以用来编译C/C++、FORTRAN、JAVA、OBJC、ADA等语言的程序,可根据需要选择安装支持的语言。GCC 3.4.0比以前版本更好地支持了C++标准。本文以在Redhat Linux上安装GCC3.4.0为例,介绍了GCC的安装过程。

安装之前,系统中必须要有cc或者gcc等编译器,并且是可用的,或者用环境变量CC指定系统上的编译器。如果系统上没有编译器,不能安装源代码形式的GCC 3.4.0。如果是这种情况,可以在网上找一个与你系统相适应的如RPM等二进制形式的GCC软件包来安装使用。本文介绍的是以源代码形式提供的GCC软件包的安装过程,软件包本身和其安装过程同样适用于其它Linux和Unix系统。

系统上原来的GCC编译器可能是把gcc等命令文件、库文件、头文件等分别存放到系统中的不同目录下的。与此不同,现在GCC建议我们将一个版本的GCC安装在一个单独的目录下。这样做的好处是将来不需要它的时候可以方便地删除整个目录即可(因为GCC没有uninstall功能);缺点是在安装完成后要做一些设置工作才能使编译器工作正常。在本文中我采用这个方案安装GCC 3.4.0,并且在安装完成后,仍然能够使用原来低版本的GCC编译器,即一个系统上可以同时存在并使用多个版本的GCC编译器。

按照本文提供的步骤和设置选项,即使以前没有安装过GCC,也可以在系统上安装上一个可工作的新版本的GCC编译器。

1. 下载
在GCC网站上(http://gcc.gnu.org/)或者通过网上搜索可以查找到下载资源。目前GCC的最新版本为 3.4.0。可供下载的文件一般有两种形式:gcc-3.4.0.tar.gz和gcc-3.4.0.tar.bz2,只是压缩格式不一样,内容完全一致,下载其中一种即可。

2. 解压缩
根据压缩格式,选择下面相应的一种方式解包(以下的“%”表示命令行提示符):

% tar xzvf gcc-3.4.0.tar.gz
或者
% bzcat gcc-3.4.0.tar.bz2 | tar xvf -

新生成的gcc-3.4.0这个目录被称为源目录,用${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 3.4.0安装到/usr/local/gcc-3.4.0目录下,则${destdir}就表示这个路径。

在我的机器上,我是这样配置的:

% ../gcc-3.4.0/configure --prefix=/usr/local/gcc-3.4.0 --enable-threads=posix --disable-checking --enable--long-long --host=i386-redhat-linux --with-system-zlib --enable-languages=c,c++,java

将GCC安装在/usr/local/gcc-3.4.0目录下,支持C/C++和JAVA语言,其它选项参见GCC提供的帮助说明。

5. 编译

% make

这是一个漫长的过程。在我的机器上(P4-1.6),这个过程用了50多分钟。

6. 安装
执行下面的命令将编译好的库文件等拷贝到${destdir}目录中(根据你设定的路径,可能需要管理员的权限):

% make install

至此,GCC 3.4.0安装过程就完成了。

6. 其它设置
GCC 3.4.0的所有文件,包括命令文件(如gcc、g++)、库文件等都在${destdir}目录下分别存放,如命令文件放在bin目录下、库文件在lib下、头文件在include下等。由于命令文件和库文件所在的目录还没有包含在相应的搜索路径内,所以必须要作适当的设置之后编译器才能顺利地找到并使用它们。

6.1 gcc、g++、gcj的设置
要想使用GCC 3.4.0的gcc等命令,简单的方法就是把它的路径${destdir}/bin放在环境变量PATH中。我不用这种方式,而是用符号连接的方式实现,这样做的好处是我仍然可以使用系统上原来的旧版本的GCC编译器。

首先,查看原来的gcc所在的路径:

% which gcc

在我的系统上,上述命令显示:/usr/bin/gcc。因此,原来的gcc命令在/usr/bin目录下。我们可以把GCC 3.4.0中的gcc、g++、gcj等命令在/usr/bin目录下分别做一个符号连接:

% cd /usr/bin
% ln -s ${destdir}/bin/gcc gcc34
% ln -s ${destdir}/bin/g++ g++34
% ln -s ${destdir}/bin/gcj gcj34

这样,就可以分别使用gcc34、g++34、gcj34来调用GCC 3.4.0的gcc、g++、gcj完成对C、C++、JAVA程序的编译了。同时,仍然能够使用旧版本的GCC编译器中的gcc、g++等命令。

6.2 库路径的设置
将${destdir}/lib路径添加到环境变量LD_LIBRARY_PATH中,最好添加到系统的配置文件中,这样就不必要每次都设置这个环境变量了。

例如,如果GCC 3.4.0安装在/usr/local/gcc-3.4.0目录下,在RH Linux下可以直接在命令行上执行或者在文件/etc/profile中添加下面一句:

setenv LD_LIBRARY_PATH /usr/local/gcc-3.4.0/lib:$LD_LIBRARY_PATH 

7. 测试
用新的编译命令(gcc34、g++34等)编译你以前的C、C++程序,检验新安装的GCC编译器是否能正常工作。

8. 根据需要,可以删除或者保留${srcdir}和${objdir}目录。

参考资料:
Installing GCC ( http://gcc.gnu.org/install/ )

后记:
前两天刚安装好了GCC 3.4.0,把安装规划、安装过程、一些注意事项记录下来就形成了这篇文章。希望对大家、特别是还没有安装过GCC的朋友在安装GCC的时候有所帮助。

 lenovo 回复于:2004-04-27 20:01:53
呵呵,给你加原创精华,
以后多多发表原创文章。

 whyglinux 回复于:2004-04-27 20:11:35
谢谢!一起努力吧。

 THEBEST 回复于:2004-04-27 20:19:11
只可惜GCC还不支持模板的分离编译模式.
把GCC3.4的新特性也帖上来就好了.:)
佩服whyglinux 兄.:)

 THEBEST 回复于:2004-04-27 20:23:21
稳定吗?

 whyglinux 回复于:2004-04-27 20:47:26
应该是吧。因为它是正式发行的版本(Released Version)。

 li2002 回复于:2004-04-28 08:15:48
蛮细的,不错

 kofd 回复于:2004-04-28 10:07:09
多谢了!

 mirnshi 回复于:2004-04-28 12:31:50
[quote:a7c69254b2="whyglinux"]]

例如,如果想将GCC 3.4.0安装到/usr/local/gcc-3.4.0目录下,则${destdir}就表示这个路径。

在我的机器上,我是这样配置的:

% ../gcc-3.4.0/configure --prefix=/usr/local/gcc-3.4.0 --enable-threads=..........[/quote:a7c69254b2]

how about this:
[url]http://www.freecode.com.cn/forum/viewtopic.php?t=16[/url]

 mirnshi 回复于:2004-04-28 12:33:05
用gcc编译时,建议使用静态连接,这样就不用跟着gcc的动态库了

 daixi 回复于:2004-04-28 12:36:06
支持一下。。。s
回去我也试试。

 qinjian1981 回复于:2004-04-28 14:17:06
是不是安装的时间太长了哦

 quil 回复于:2004-04-28 20:26:27
好像记得从哪个版本开始gcc不支持sco了。是不是这样啊

 THEBEST 回复于:2004-04-28 22:37:51
有没有rpm包?
我编译时出错了.
我是LINUX AS3
我参数和你的一样的有关系吗?

 dragondk 回复于:2004-04-29 00:50:04
编译GCC/G++ 好象是用make bootstrap吧。

 THEBEST 回复于:2004-04-29 07:52:56
有谁做一个RPM包出来然后可以指定安装位置就好了....:)

 wangyb 回复于:2004-04-29 16:17:21
太好了
以前都使用编译好的 现在可以使用源码安装了

 swingcoder 回复于:2004-04-29 18:16:27
能不能跟其它类型的编译器比较下呢???

如intel的

 whyglinux 回复于:2004-04-29 18:35:59
已经有人对 Intel C++ 和 GNU g++ 做了比较,有兴趣的话看这儿:
http://www.coyotegulch.com/reviews/intel_comp/intel_gcc_bench2.html

 knightwolf 回复于:2004-05-02 13:35:55
# make bootstrap
        cc -c  -g -DHAVE_CONFIG_H  -I. -I../../intl ../../intl/localealias.c
"../../intl/localealias.c", line 164: error: undefined symbol: LOCALE_ALIAS_PATH
"../../intl/localealias.c", line 164: warning: improper pointer/integer co
mbination: op "="
*** Error code 1 (bu21)
*** Error code 1 (bu21)

这怎么解决,LOCALE_ALIAS_PATH怎声明??????

 whyglinux 回复于:2004-05-02 14:25:18
不知道你的 os、你使用的cc以及你在安装GCC的过程中是怎样做的等任何信息,所以我想很难有人能够帮助你。以后要注意提供足够详细的信息,这才有助于问题的解决。

我只在Linux下用gcc编译器安装过 GCC 2.3.0。对于其它OS和使用其它的编译器,我没有这样的环境和经验,有问题的话也不知道如何解决。如果是这样,你可以用 google 等搜索,看网上有没有相关的解决方案。

 hsgzr 回复于:2004-05-02 18:38:46
谢谢

 honker 回复于:2004-05-08 17:41:56
很详细,谢谢。

 LX20010 回复于:2004-05-10 17:25:48
respecting......

 THEBEST 回复于:2004-05-10 23:20:28
[code:1:4e31c16ba4]GCC 3.4 Release Series
Changes, New Features, and Fixes[/code:1:4e31c16ba4]

[quote:4e31c16ba4]http://gcc.gnu.org/gcc-3.4/changes.html#cplusplus[/quote:4e31c16ba4]

[code:1:4e31c16ba4]C/Objective-C/C++

    * Precompiled headers are now supported. Precompiled headers can dramatically speed up compilation of some projects. There are some known defects in the current precompiled header implementation that will result in compiler crashes in relatively rare situations. Therefore, precompiled headers should be considered a "technology preview" in this release. Read the manual for details about how to use precompiled headers.
    * File handling in the preprocessor has been rewritten. GCC no longer gets confused by symlinks and hardlinks, and now has a correct implementation of #import and #pragma once. These two directives have therefore been un-deprecated.
    * The undocumented extension that allowed C programs to have a label at the end of a compound statement, which has been deprecated since GCC 3.0, has been removed.
    * The cast-as-lvalue extension has been removed for C++ and deprecated for C and Objective-C. In particular, code like this:

        int i;
        (char) i = 5;
        

      or this:

        char *p;
        ((int *) p)++;
        

      is no longer accepted for C++ and will not be accepted for C and Objective-C in a future version.
    * The conditional-expression-as-lvalue extension has been deprecated for C and Objective-C. In particular, code like this:

        int a, b, c;
        (a ? b : c) = 2;
        

      will not be accepted for C and Objective-C in a future version.
    * The compound-expression-as-lvalue extension has been deprecated for C and Objective-C. In particular, code like this:

        int a, b;
        (a, b) = 2;
        

      will not be accepted for C and Objective-C in a future version. A possible non-intrusive workaround is the following:

        (*(a, &b)) = 2;
        

    * Several built-in functions such as __builtin_popcount for counting bits, finding the highest and lowest bit in a word, and parity have been added.
    * The -fwritable-strings option has been deprecated and will be removed.
    * Many C math library functions are now recognized as built-ins and optimized.
    * The C, C++, and Objective-C compilers can now handle source files written in any character encoding supported by the host C library. The default input character set is taken from the current locale, and may be overridden with the -finput-charset command line option. In the future we will add support for inline encoding markers.

C++

    * G++ is now much closer to full conformance to the ISO/ANSI C++ standard. This means, among other things, that a lot of invalid constructs which used to be accepted in previous versions will now be rejected. It is very likely that existing C++ code will need to be fixed. This document lists some of the most common issues.
    * A hand-written recursive-descent C++ parser has replaced the YACC-derived C++ parser from previous GCC releases. The new parser contains much improved infrastructure needed for better parsing of C++ source codes, handling of extensions, and clean separation (where possible) between proper semantics analysis and parsing. The new parser fixes many bugs that were found in the old parser.
    * You must now use the typename and template keywords to disambiguate dependent names, as required by the C++ standard.

struct K {
  typedef int mytype_t;
};

template <class T1> struct A {
  template <class T2> struct B { 
      void callme(void);
    };

  template <int N> void bar(void) 
  {
    // Use 'typename' to tell the parser that T1::mytype_t names
    //  a type. This is needed because the name is dependent (in
    //  this case, on template parameter T1).
    typename T1::mytype_t x;
    x = 0;
  }
};

template <class T> void template_func(void)
{
  // Use 'template' to prefix member templates within 
  //  dependent types (a has type A<T>, which depends on
  //  the template parameter T).
  A<T> a;
  a.template bar<0>();

  // Use 'template' to tell the parser that B is a nested 
  //  template class (dependent on template parameter T), and 
  //  'typename' because the whole A<T>::B<int> is 
  //  the name of a type (again, dependent).
  typename A<T>::template B<int> b;
  b.callme();
}

void non_template_func(void)
{
  // Outside of any template class or function, no names can be
  //  dependent, so the use of the keyword 'typename' and 'template'
  //  is not needed (and actually forbidden).
  A<K> a;
  a.bar<0>();
  A<K>::B<float> b;
  b.callme();
}

    * In a template definition, unqualified names will no longer find members of a dependent base. For example,

        template <typename T> struct B {
  int m;
  int n;
  int f ();
  int g ();
};
int n;
int g ();
template <typename T> struct C : B<T> {
  void g ()
  {
    m = 0; // error
    f ();  // error
    n = 0; // ::n is modified
    g ();  // ::g is called
  }
};

      You must make the names dependent by prefixing them with this->. Here is the corrected definition of C<T>::g,

template <typename T> void C<T>::g ()
{
  this->m = 0;
  this->f ();
  this->n = 0
  this->g ();
}

    * In templates, all non-dependent names are now looked up and bound at definition time (while parsing the code), instead of later when the template is instantiated. For instance:

void foo(int);

template <int> struct A {
  static void bar(void){
    foo('a');
  }
};

void foo(char);

int main()
{
  A<0>::bar();    // Calls foo(int), used to call foo(char).
}


    * In an explicit instantiation of a class template, you must use class or struct before the template-id:

template <int N>
class A {};

template A<0>;         // error, not accepted anymore
template class A<0>;   // OK

    * The "named return value" and "implicit typename" extensions have been removed.
    * Default arguments in function types have been deprecated and will be removed.
    * ARM-style name-injection of friend declarations has been deprecated and will be removed. For example: struct S { friend void f(); }; void g() { f(); } will not be accepted by future versions of G++; instead a declaration of "f" will need to be present outside of the scope of "S".
    * Covariant returns are implemented for all but varadic functions that require an adjustment.
    * When -pedantic is used, G++ now issues errors about spurious semicolons. For example,

        namespace N {}; // Invalid semicolon.
        void f() {}; // Invalid semicolon.

    * G++ no longer accepts attributes for a declarator after the initializer associated with that declarator. For example,

        X x(1) __attribute__((...));

      is no longer accepted. Instead, use:

        X x __attribute__((...)) (1);

    * Inside the scope of a template class, the name of the class itself can be treated as either a class or a template. So GCC used to accept the class name as argument of type template, and template template parameter. However this is not C++ standard compliant. Now the name is not treated as a valid template template argument unless you qualify the name by its scope. For example, the code below no longer compiles.

        template <template <class> class TT> class X {};
        template <class T> class Y {
          X<Y> x; // Invalid, Y is always a type template parameter.
        };

      The valid code for the above example is

          X< ::Y> x; // Valid.

      (Notice the space between < and : to prevent GCC to interpret this as a digraph for [.)
    * Friend declarations that refer to template specializations are rejected if the template has not already been declared. For example,

template <typename T>
class C {
  friend void f<> (C&);
};

      is rejected. You must first declare f as a template,

template <typename T>
void f(T);

    * In case of friend declarations, every name used in the friend declaration must be accessible at the point of that declaration. Previous versions of G++ used to be less strict about this and allowed friend declarations for private class members, for example. See the ISO C++ Standard Committee's defect report #209 for details.
    * Declaration of member functions of class templates as friends are supported. For example,

template <typename T> struct A {
  void f();
};
class C {
  template <typename T> friend void A<T>::f();
};

    * You must use template <> to introduce template specializations, as required by the standard. For example,

template <typename T>
struct S;

struct S<int> { };

      is rejected. You must write,

template <> struct S<int> {};

    * G++ used to accept code like this,

struct S {
  int h();
  void f(int i = g());
  int g(int i = h());
};

      This behavior is not mandated by the standard. Now G++ issues an error about this code. To avoid the error, you must move the declaration of g before the declaration of f. The default arguments for g must be visible at the point where it is called.
    * The C++ ABI Section 3.3.3 specifications for the array construction routines __cxa_vec_new2 and __cxa_vec_new3 were changed to return NULL when the allocator argument returns NULL. These changes are incorporated into the libstdc++ runtime library.
    * Using a name introduced by a typedef in a friend declaration or in an explicit instantiation is now rejected, as specified by the ISO C++ standard.

class A;
typedef A B;
class C { 
  friend class B;      // error, no typedef name here
  friend B;            // error, friend always needs class/struct/enum
  friend class A;      // OK
};

template <int> class Q {};
typedef Q<0> R;
template class R;      // error, no typedef name here
template class Q<0>;   // OK

    * When allocating an array with a new expression, GCC used to allow parentheses around the type name. This is actually ill-formed and it is now rejected:

int* a = new (int)[10];    // error, not accepted anymore
int* a = new int[10];      // OK

    * When binding an rvalue of class type to a reference, the copy constructor of the class must be accessible. For instance, consider the following code:

class A 
{
public:
  A();
  
private:
  A(const A&);   // private copy ctor
};

A makeA(void);
void foo(const A&);

void bar(void)
{
  foo(A());       // error, copy ctor is not accessible
  foo(makeA());   // error, copy ctor is not accessible
  
  A a1;
  foo(a1);        // OK, a1 is a lvalue
}

      This might be surprising at first sight, especially since most popular compilers do not correctly implement this rule (further details).

Runtime Library (libstdc++)

    * Optimization work:
          o Streamlined streambuf, filebuf, separate synched with C Standard I/O streambuf.
          o All formatted I/O now uses cached locale information.
          o STL optimizations (memory/speed for list, red-black trees as used by sets and maps).
          o More use of GCC builtins.
          o String optimizations (avoid contention on increment/decrement-and-test of the reference count in the empty-string object, constructor from input_iterators speedup).
    * Static linkage size reductions.
    * Large File Support (files larger than 2 GB on 32-bit systems).
    * Wide character and variable encoding filebuf work (UTF-8, Unicode).
    * Generic character traits.
    * Also support wchar_t specializations on Mac OS 10.3.x, FreeBSD 5.x, Solaris 2.7 and above, AIX 5.x, Irix 6.5.
    * The allocator class is now standard-conformant, and two additional extension allocators have been added, mt_alloc and bitmap_allocator.
    * PCH support: -include bits/stdc++.h (2x compile speedup).
    * Rewrote __cxa_demangle with support for C++ style allocators.
    * New debug modes for STL containers and iterators.
    * Testsuite rewrite: five times as many tests, plus increasingly sophisticated tests, including I/O, MT, multi-locale, wide and narrow characters.
    * Use current versions of GNU "autotools" for build/configuration.

Objective-C

    * The Objective-C front end has been updated to include the numerous bug fixes and enhancements previously available only in Apple's version of GCC. These include:
          o Structured exception (@try... @catch... @finally, @throw) and synchronization (@synchronized) support. These are accessible via the -fobjc-exceptions switch; as of this writing, they may only be used in conjunction with -fnext-runtime on Mac OS X 10.3 and later. See Options Controlling Objective-C Dialect for more information.
          o An overhaul of @encode logic. The C99 _Bool and C++ bool type may now be encoded as 'B'. In addition, the back-end/codegen dependencies have been removed.
          o An overhaul of message dispatch construction, ensuring that the various receiver types (and casts thereof) are handled properly, and that correct diagnostics are issued.
          o Support for "Zero-Link" (-fzero-link) and "Fix-and-Continue" (-freplace-objc-classes) debugging modes, currently available on Mac OS X 10.3 and later. See Options Controlling Objective-C Dialect for more information.
          o Access to optimized runtime entry points (-fno-nil-receivers ) on the assumption that message receivers are never nil. This is currently available on Mac OS X 10.3 and later. See Options Controlling Objective-C Dialect for more information.
[/code:1:4e31c16ba4]

 tonggl150 回复于:2004-05-12 13:35:00
GCC高手:
    现有SDW原程序,因为我没有GCC软件,敬请哪位高手帮忙编译一下,以便本人读懂SDW类文件。先谢谢了。

 mxin 回复于:2004-05-25 13:45:48
p690,AIX 5.2,V C++6.0,安装 gcc-3.3.2时
make bootstrap报错如下


 cc -c -DIN_GCC -DLOCALEDIR=\"/usr/local/share/locale\" -DLOCALE_ALIAS_PATH=\"/usr/local/share/locale\"  -DLIBDIR=\"/usr/local/lib\" -DHAVE_CONFIG_H -I.. -I. -I../../../gcc/intl -I../../../gcc  -I../../../gcc/config -I../../../gcc/../include  -g    ../../../gcc/intl/plural.c -o plural.o
"plural.y", line 264.1: 1506-343 (S) Redeclaration of __gettextlex differs from previous declaration on line 73 of "plural.y".
"plural.y", line 264.1: 1506-381 (I) The type "const unsigned char**" of parameter 2 in the prototype declaration is not compatible with the corresponding parameter type "unsigned char**" in the nonprototype declaration.
cc: 1501-230 Internal compiler error; please contact your Service Representative
make: 1254-004 The error code from the last command is 41.


Stop.
make: 1254-004 The error code from the last command is 2.

 whyglinux 回复于:2004-05-25 14:48:19
从网上搜到有两种解决方式,可以试试。

1. 安装编译时用 GCC编译器而不用 cc

或者

2. 用 cc 编译,但是 configure 的时候加 --disable-nls

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有