linux 编译器gcc

合集下载

Linux超详细gcc升级全过程

Linux超详细gcc升级全过程

Linux超详细gcc升级全过程⽬录前⾔1.当前gcc版本2.安装gcc3.gmp安装4.MPFR编译5.MPC编译6.GCC 配置7.GCC版本更新前⾔c c++ 等等需要这个编译器gcc,最近有DBA的朋友咨询RHEL7.6操作系统安装Mysql数据库时需要⾼版本的GCC,研究了下发现坑不少,总结本⽂分享给⼤家1.当前gcc版本[root@rhel76 ~]# gcc -vUsing built-in specs.COLLECT_GCC=gccCOLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapperTarget: x86_64-redhat-linuxConfigured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_at Thread model: posixgcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)从以上可以看出当前版本为4.8.5,本次我们升级到10.1.02.安装gcc--下载地址:https:///gnu/gcc/gcc-10.1.0/[root@rhel76 ~]# tar -vxf gcc-10.1.0.tar.gz[root@rhel76 gcc-10.1.0]# mkdir build[root@rhel76 gcc-10.1.0]# cd build/[root@rhel76 build]# ../configure --prefix=/usr/local/gcc-10.1.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilibchecking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking target system type... x86_64-pc-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether ln works... yeschecking whether ln -s works... yeschecking for a sed that does not truncate output... /usr/bin/sedchecking for gawk... gawkchecking for libatomic support... yeschecking for libitm support... yeschecking for libsanitizer support... yeschecking for libvtv support... yeschecking for libhsail-rt support... yeschecking for libphobos support... yeschecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables...checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for g++... g++checking whether we are using the GNU C++ compiler... yeschecking whether g++ accepts -g... yeschecking whether g++ accepts -static-libstdc++ -static-libgcc... nochecking for gnatbind... nochecking for gnatmake... nochecking whether compiler driver understands Ada... nochecking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2checking for objdir... .libschecking for the correct version of gmp.h... noconfigure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.Try the --with-gmp, --with-mpfr and/or --with-mpc options to specifytheir locations. Source code for these libraries can be found attheir respective hosting sites as well as athttps:///pub/gcc/infrastructure/. See also/install/prerequisites.html for additional info. Ifyou obtained GMP, MPFR and/or MPC from a vendor distribution package,make sure that you have installed both the libraries and the headerfiles. They may be located in separate packages.从⽇志总可以看出有如下报错,故下⾯每个都安装configure: error: Building GCC requires GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+.3.gmp安装[root@jeames007 ~]# tar -vxf gmp-5.0.1.tar.bz2[root@jeames007 ~]# cd gmp-5.0.1/[root@jeames007 gmp-5.0.1]# ./configure --prefix=/usr/local/gmp-5.0.1[root@jeames007 gmp-5.0.1]# make[root@jeames007 gmp-5.0.1]# make installmake[4]: Leaving directory `/root/gmp-5.0.1'make[3]: Leaving directory `/root/gmp-5.0.1'make[2]: Leaving directory `/root/gmp-5.0.1'make[1]: Leaving directory `/root/gmp-5.0.1'4.MPFR编译[root@jeames007 ~]# tar -vxf mpfr-3.1.5.tar.xz[root@jeames007 ~]# cd mpfr-3.1.5/[root@jeames007 ~]#./configure --prefix=/usr/local/mpfr-3.1.5 --with-gmp=/usr/local/gmp-5.0.1[root@jeames007 mpfr-3.1.5]# make[root@jeames007 mpfr-3.1.5]# make install5.MPC编译[root@jeames007 ~]# tar -vxf mpc-1.0.1.tar.gz[root@jeames007 ~]# cd mpc-1.0.1[root@jeames007 ~]# ./configure --prefix=/usr/local/mpc-1.0.1 --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.5[root@jeames007 mpc-1.0.1]# make[root@jeames007 mpc-1.0.1]# make install6.GCC 配置[root@rhel76 ~]# cd gcc-10.1.0[root@rhel76 gcc-10.1.0]# cd build/../configure --prefix=/usr/local/gcc-10.1.0/ --enable-checking=release --enable-languages=c,c++ --disable-multilib --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.5 --with-mpc=/usr/local/mpc-1.0.1 checking build system type... x86_64-pc-linux-gnuchecking host system type... x86_64-pc-linux-gnuchecking target system type... x86_64-pc-linux-gnuchecking for a BSD-compatible install... /usr/bin/install -cchecking whether ln works... yeschecking whether ln -s works... yeschecking for a sed that does not truncate output... /usr/bin/sedchecking for gawk... gawkchecking for libatomic support... yeschecking for libitm support... yeschecking for libsanitizer support... yeschecking for libvtv support... yeschecking for libhsail-rt support... yeschecking for libphobos support... yeschecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.outchecking for suffix of executables...checking whether we are cross compiling... nochecking for suffix of object files... ochecking whether we are using the GNU C compiler... yeschecking whether gcc accepts -g... yeschecking for gcc option to accept ISO C89... none neededchecking for g++... g++checking whether we are using the GNU C++ compiler... yeschecking whether g++ accepts -g... yeschecking whether g++ accepts -static-libstdc++ -static-libgcc... nochecking for gnatbind... nochecking for gnatmake... nochecking whether compiler driver understands Ada... nochecking how to compare bootstrapped objects... cmp --ignore-initial=16 $$f1 $$f2checking for objdir... .libschecking for the correct version of gmp.h... yeschecking for the correct version of mpfr.h... buggy but acceptablechecking for the correct version of mpc.h... yeschecking for the correct version of the gmp/mpfr/mpc libraries... yeschecking for isl 0.15 or later... norequired isl version is 0.15 or later*** This configuration is not supported in the following subdirectories:gnattools gotools target-libada target-libhsail-rt target-libphobos target-zlib target-libbacktrace target-libgfortran target-libgo target-libffi target-libobjc target-liboffloadmic(Any other directories should still work fine.)checking for default BUILD_CONFIG... bootstrap-debugchecking for --enable-vtable-verify... nochecking for bison... bison -ychecking for bison... bisonchecking for gm4... nochecking for gnum4... nochecking for m4... m4checking for flex... flexchecking for flex... flexchecking for makeinfo... no/root/gcc-10.1.0/missing: line 81: makeinfo: command not foundchecking for expect... nochecking for runtest... nochecking for ar... archecking for as... aschecking for dlltool... nochecking for ld... ldchecking for lipo... nochecking for nm... nmchecking for ranlib... ranlibchecking for strip... stripchecking for windres... nochecking for windmc... nochecking for objcopy... objcopychecking for objdump... objdumpchecking for otool... nochecking for readelf... readelfchecking for cc... ccchecking for c++... c++checking for gcc... gccchecking for gfortran... gfortranchecking for gccgo... nochecking for gdc... nochecking for ar... nochecking for ar... archecking for as... nochecking for as... aschecking for dlltool... nochecking for dlltool... nochecking for ld... nochecking for ld... ldchecking for lipo... nochecking for lipo... nochecking for nm... nochecking for nm... nmchecking for objcopy... nochecking for objcopy... objcopychecking for objdump... nochecking for objdump... objdumpchecking for otool... nochecking for otool... nochecking for ranlib... nochecking for ranlib... ranlibchecking for readelf... nochecking for readelf... readelfchecking for strip... nochecking for strip... stripchecking for windres... nochecking for windres... nochecking for windmc... nochecking for windmc... nochecking where to find the target ar... host toolchecking where to find the target as... host toolchecking where to find the target cc... just compiledchecking where to find the target c++... just compiledchecking where to find the target c++ for libstdc++... just compiledchecking where to find the target dlltool... host toolchecking where to find the target gcc... just compiledchecking where to find the target gfortran... host toolchecking where to find the target gccgo... host toolchecking where to find the target gdc... host toolchecking where to find the target ld... host toolchecking where to find the target lipo... host toolchecking where to find the target nm... host toolchecking where to find the target objcopy... host toolchecking where to find the target objdump... host toolchecking where to find the target otool... host toolchecking where to find the target ranlib... host toolchecking where to find the target readelf... host toolchecking where to find the target strip... host toolchecking where to find the target windres... host toolchecking where to find the target windmc... host toolchecking whether to enable maintainer-specific portions of Makefiles... noconfigure: creating ./config.statusconfig.status: creating Makefile[root@jeames007 ~]# make -j4make 时间很长,很长,耐⼼等待,本⼈编译了1个⼩时。

gcc使用大全

gcc使用大全

1、用于linux系统下编程的编译器概述GCC(GNU Compiler Collection,GNU编译器套装),是一套由GNU 开发的编程语言编译器。

它是一套GNU编译器套装以GPL 及LGPL 许可证所发行的自由软件,也是GNU计划的关键部分,亦是自由的类Unix及苹果电脑Mac OS X 操作系统的标准编译器。

GCC 原名为GNU C 语言编译器,因为它原本只能处理C语言。

GCC 很快地扩展,变得可处理C++。

之后也变得可处理Fortran、Pascal、Objective-C、Jav a, 以及Ada与其他语言。

历史GCC是由理查德·马修·斯托曼在1985年开始的。

他首先扩增一个旧有的编译器,使它能编译C,这个编译器一开始是以Pastel语言所写的。

Pastel是一个不可移植的Pascal语言特殊版,这个编译器也只能编译Pastel语言。

为了让自由软件有一个编译器,后来此编译器由斯托曼和Len Tower在1987年以C语言重写并成为GNU 专案的编译器。

GCC的建立者由自由软件基金会直接管理。

在1997年,一群不满GCC缓慢且封闭的创作环境者,组织了一个名为EGCS 〈Experimental/Enhanced GNU Compiler System〉的专案,此专案汇整了数项实验性的分支进入某个GCC专案的分支中。

EGCS比起GCC的建构环境更有活力,且EGCS最终也在1999年四月成为GCC的官方版本。

GCC目前由世界各地不同的数个程序设计师小组维护。

它是移植到中央处理器架构以及操作系统最多的编译器。

由于GCC已成为GNU系统的官方编译器(包括GNU/Linux家族),它也成为编译与建立其他操作系统的主要编译器,包括BSD家族、Mac OS X、NeXTSTEP 与BeOS。

GCC通常是跨平台软件的编译器首选。

有别于一般局限于特定系统与执行环境的编译器,GCC在所有平台上都使用同一个前端处理程序,产生一样的中介码,因此此中介码在各个其他平台上使用GCC编译,有很大的机会可得到正确无误的输出程序。

linux的gcc使用方法

linux的gcc使用方法

linux的gcc使用方法Linux是一种开源的操作系统,广泛应用于服务器和嵌入式系统中。

而GCC(GNU Compiler Collection)是Linux下最常用的编译器套件之一,用于将源代码编译成可执行文件。

本文将介绍GCC的使用方法,帮助读者快速上手。

一、安装GCC在Linux系统中,默认情况下已经安装了GCC。

可以通过运行以下命令来验证是否已经安装了GCC:```gcc --version```如果GCC已经安装,则会显示GCC的版本信息;如果没有安装,则可以通过运行以下命令来安装GCC:```sudo apt-get install gcc```二、编写源代码在使用GCC之前,我们需要先编写源代码。

可以使用任何文本编辑器创建一个以.c为后缀的源文件,例如hello.c。

下面是一个示例的源代码:```c#include <stdio.h>int main() {printf("Hello, world!\n");return 0;}```三、编译源代码编写完源代码后,我们可以使用GCC来将其编译成可执行文件。

在终端中运行以下命令:```gcc -o hello hello.c```其中,-o参数用于指定编译后生成的可执行文件的名称,hello为示例的可执行文件名,hello.c为源代码文件名。

如果编译成功,GCC将会生成一个名为hello的可执行文件。

四、运行可执行文件在编译成功后,我们可以通过以下命令来运行可执行文件:```./hello```如果一切顺利,终端将会输出"Hello, world!"的字符串。

五、GCC的其他常用选项除了上述基本的使用方法外,GCC还提供了许多其他的选项,用于控制编译过程的行为。

以下是一些常用的选项:- -Wall:开启所有警告信息的显示。

- -g:生成供调试器使用的调试信息。

- -O2:进行优化处理,提高程序执行效率。

linux gcc编译格式 -回复

linux gcc编译格式 -回复

linux gcc编译格式-回复Linux GCC编译格式详解GCC是GNU编译器集合(GNU Compiler Collection)的缩写,它是一款功能强大且广泛使用的编译工具。

在Linux操作系统中,GCC被广泛用来编译各种类型的程序,包括C、C++和Fortran等。

本文将从基本介绍开始,一步一步详细解释GCC编译格式,帮助读者更好地理解和使用GCC。

1. GCC介绍GCC是一个由GNU Project创建的自由软件,它是一个以GPL(GNU通用公共许可证)方式发布的编译器。

GCC支持多种编程语言,包括C、C++、Fortran、Ada和Objective-C等,因此广泛应用于不同的领域。

2. GCC命令格式GCC的命令格式一般为:gcc [options] [source files] [object files] [libraries]其中,方括号内的部分代表可选参数,源文件(source files)指的是待编译的源代码文件,目标文件(object files)是通过编译生成的中间文件,库文件(libraries)是用于链接程序所需的额外库文件。

3. 基本编译命令GCC可以根据源代码文件进行编译,生成可执行文件。

以下是基本编译命令的格式:gcc -o <output> <input>其中,-o选项用于指定生成的可执行文件的名称,<output>代表输出文件的名称(通常为可执行文件的名字),<input>为输入文件的名称(通常为源代码文件的名字)。

例如,假设我们有一个名为hello.c的源代码文件,可以使用如下命令编译生成可执行文件hello:gcc -o hello hello.c4. 高级编译选项GCC提供了丰富的编译选项,用于控制编译过程中的各种参数和行为。

以下是一些常用的选项:- -Wall:启用所有警告信息。

- -g:生成调试信息,用于调试程序。

Linux编程 使用gcc编译C程序

Linux编程  使用gcc编译C程序
}
}
编译时可以使用如下命令:
gcc -o func main.c fun.c
编译完成后,使用下面的命令执行并显示结果:
./func 5
5的阶乘是:120
2.gcc的错误类型及对策
gcc编译器如果发现源程序中有错误,就无法继续进行,也无法生成最终的可执行文件。为了便于修改,gcc给出错误资讯,用户必须对这些错误资讯逐个进行分析、处理,并修改相应的语言,才能保证源代码的正确编译连接。gcc给出的错误资讯一般可以分为四大类,这里分别来讨论其产生的原因和对策。
排除编译、连接过程中的错误,应该说这只是程序设计中最简单、最基本的一个步骤,可以说只是开了个头。这个过程中的错误,只是在使用C语言描述一个算法中所产生的错误,是比较容易排除的。一个程序,到编译、连接通过为止,应该说刚刚开始,程序在运行过程中所出现的问题,还需要更加深入地测试、调试和修改。稍为复杂的程序,往往要经过多次的编译、连接和测试、修改,用户在学习中应该不断提高程序的维护和调试能力。
#include <stdio.h>
int main()
{
printf("你好!");
return 0;
}
接下开打开终端窗口,进入到该文件所在目录,并输入下面的命令:
gcc -o mytest test.c
上述命令执行完毕后,会在该源文件所在目录下生成一个新文件,该文件名称为mytest,该文件就是编译过的可执行文件,在终端容器中再次输入以下命令:
int i=1;
for(i;i<=100;n++)
sum=sum+i;
printf("最后结果为:%d",sum);

gcc 用法

gcc 用法

gcc 用法GCC (GNU Compiler Collection) 是一套自由软件编译器,可以用于编译多种编程语言的源代码,包括 C、C++、Objective-C、Fortran、Ada 和其他一些语言。

GCC 是GNU计划的一部分,由自由软件基金会维护和发展。

本文将详细介绍 GCC 的使用方法。

一、环境准备在使用GCC之前,需要先安装好GCC。

GCC是开源软件,常见的Linux发行版都默认安装GCC,Windows下可以通过安装Cygwin或MinGW等方式安装GCC。

安装完成后,在命令行中输入gcc --version查看gcc的版本号确认是否安装成功。

除了GCC之外,还需要一个文本编辑器来编写源代码。

Windows下常用的文本编辑器有Notepad++,Linux下则使用vim或emacs等编辑器。

还需要了解一些基本的编程知识和语法。

二、GCC 编译 C 语言程序以下是一个简单的 C 语言程序,可以输出 "Hello, world!":```c#include <stdio.h>printf("Hello, world!\n");return 0;}```将上述代码保存为 helloworld.c 文件,然后在命令行中进入文件所在目录,输入以下命令编译该程序:```gcc helloworld.c -o helloworld```gcc是编译器的命令,helloworld.c是待编译的源代码文件名,-o helloworld是生成的可执行文件名。

执行上述命令后,GCC 会把源代码编译成可执行文件 helloworld。

运行该可执行文件,可以得到以下输出:```Hello, world!```三、GCC 编译 C++ 程序GCC 也可以编译 C++ 程序,以下是一个简单的 C++ 程序,可以输出 "Hello,world!":```c++#include <iostream>std::cout << "Hello, world!" << std::endl;return 0;}```将上述代码保存为 helloworld.cpp 文件,然后在命令行中进入文件所在目录,输入以下命令编译该程序:```g++ helloworld.cpp -o helloworld```g++是编译器的命令,helloworld.cpp是待编译的源代码文件名,-o helloworld是生成的可执行文件名。

arm-linux-gcc 常用参数讲解 gcc编译器使用方法

arm-linux-gcc 常用参数讲解 gcc编译器使用方法

arm-linux-gcc常用参数讲解gcc编译器使用方法我们需要编译出运行在ARM平台上的代码,所使用的交叉编译器为arm-linux-gcc。

下面将arm-linux-gcc编译工具的一些常用命令参数介绍给大家。

在此之前首先介绍下编译器的工作过程,在使用GCC编译程序时,编译过程分为四个阶段:1. 预处理(Pre-Processing)2. 编译(Compiling)3. 汇编(Assembling)4. 链接(Linking)Linux程序员可以根据自己的需要让GCC在编译的任何阶段结束,以便检查或使用编译器在该阶段的输出信息,或者对最后生成的二进制文件进行控制,以便通过加入不同数量和种类的调试代码来为今后的调试做好准备。

和其它常用的编译器一样,GCC也提供了灵活而强大的代码优化功能,利用它可以生成执行效率更高的代码。

以文件example.c为例说明它的用法0. arm-linux-gcc -o example example.c不加-c、-S、-E参数,编译器将执行预处理、编译、汇编、连接操作直接生成可执行代码。

-o参数用于指定输出的文件,输出文件名为example,如果不指定输出文件,则默认输出a.out1. arm-linux-gcc -c -o example.oexample.c-c参数将对源程序example.c进行预处理、编译、汇编操作,生成example.0文件去掉指定输出选项"-o example.o"自动输出为example.o,所以说在这里-o加不加都可以2.arm-linux-gcc -S -o example.sexample.c-S参数将对源程序example.c进行预处理、编译,生成example.s文件-o选项同上3.arm-linux-gcc -E -o example.iexample.c-E参数将对源程序example.c进行预处理,生成example.i文件(不同版本不一样,有的将预处理后的内容打印到屏幕上)就是将#include,#define等进行文件插入及宏扩展等操作。

linux安装配置交叉编译器arm-linux-gnueabi-gcc

linux安装配置交叉编译器arm-linux-gnueabi-gcc

linux安装配置交叉编译器arm-linux-gnueabi-gcc要使我们在x86架构下运⾏的程序迁移⾄ARM架构的开发板中运⾏时,需要通过交叉编译器将x86下编写的程序进⾏编译后,开发版才能运⾏。

在安装之前我们需要了解,什么是。

⼀、下载交叉编译器1.新版本的下载⼊⼝如下图所⽰:下载流程如下所⽰:“GNU Toolchain Integration Builds → 11.0-2021.03-1 → arm-linux-gnueabihf → gcc-linaro-11.0.1-2021.03-x86_64_arm-linux-gnueabihf.tar.xz。

”注意:随着时间的不同可能版本号有所变化,不过下载流程应给是⼀样的,除⾮⽹站的变化很⼤。

2.历史版本下载⼊⼝如下图所⽰:下载流程如下所⽰:“View Releases → components → toolchain → binaries → 6.2-2016.11 → arm-linux-gnueabihf → gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf.tar.xz”⼆、安装交叉编译器进⼊linux系统,在/usr/local下创建arm⽂件,将下载的交叉编译⼯具链拷贝到linux系统的/usr/local/arm路径下,并进项解压,如下图所⽰:三、设置环境变量打开/etc/profile⽂件sudo vim /etc/profile在⽂件的最后⼀⾏添加交叉编译链的路径,完成后保存退出export PATH=$PATH:/usr/local/arm/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin如下图所⽰:重新加载环境变量的配置⽂件source /etc/profile检验是否安装成功arm-linux-gnueabihf-gcc -v如果出现以下信息说明安装成功。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.8 Linux 上的C/C++ 编译器和调试器/man/linux_tsinghua/compiler.html∙运行 gcc/egcs∙gcc/egcs 的主要选项∙gdb∙gdb 的常用命令∙gdb 使用范例∙其他程序/库工具 (ar, objdump, nm, size, strings, strip, ...)1.8.1 运行 gcc/egcsLinux 中最重要的软件开发工具是 GCC。

GCC 是 GNU 的 C 和 C++ 编译器。

实际上,GCC 能够编译三种语言:C、C++ 和 Object C(C 语言的一种面向对象扩展)。

利用 gcc 命令可同时编译并连接 C 和 C++ 源程序。

#DEMO#: hello.c如果你有两个或少数几个 C 源文件,也可以方便地利用 GCC 编译、连接并生成可执行文件。

例如,假设你有两个源文件 main.c 和 factorial.c 两个源文件,现在要编译生成一个计算阶乘的程序。

清单 factorial.c-----------------------#include#includeint factorial (int n){if (n <= 1)return 1;elsereturn factorial (n - 1) * n;}----------------------------------------------清单 main.c-----------------------#include#includeint factorial (int n);int main (int argc, char **argv){int n;if (argc < 2) {printf ("Usage: %s n\n", argv [0]);return -1;}else {n = atoi (argv[1]);printf ("Factorial of %d is %d.\n", n, factorial (n));}return 0;}-----------------------利用如下的命令可编译生成可执行文件,并执行程序:$ gcc -o factorial main.c factorial.c$ ./factorial 5Factorial of 5 is 120.GCC 可同时用来编译 C 程序和 C++ 程序。

一般来说,C 编译器通过源文件的后缀名来判断是 C 程序还是 C++ 程序。

在 Linux 中,C 源文件的后缀名为 .c,而 C++ 源文件的后缀名为 .C 或 .cpp。

但是,gcc 命令只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库连接。

因此,通常使用 g++ 命令来完完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。

假设我们有一个如下的 C++ 源文件(hello.C):#includevoid main (void){cout << "Hello, world!" << endl;}则可以如下调用 g++ 命令编译、连接并生成可执行文件:$ g++ -o hello hello.C$ ./helloHello, world!1.8.2 gcc/egcs 的主要选项表 1-3 gcc 命令的常用选项选项解释-ansi 只支持 ANSI 标准的 C 语法。

这一选项将禁止 GNU C 的某些特色,例如 asm 或 typeof 关键词。

-c 只编译并生成目标文件。

-DMACRO 以字符串“1”定义 MACRO 宏。

-DMACRO=DEFN 以字符串“DEFN”定义 MACRO 宏。

-E 只运行 C 预编译器。

-g 生成调试信息。

GNU 调试器可利用该信息。

-IDIRECTORY 指定额外的头文件搜索路径DIRECTORY。

-LDIRECTORY 指定额外的函数库搜索路径DIRECTORY。

-lLIBRARY 连接时搜索指定的函数库LIBRARY。

-m486 针对 486 进行代码优化。

-o FILE 生成指定的输出文件。

用在生成可执行文件时。

-O0 不进行优化处理。

-O 或 -O1 优化生成代码。

-O2 进一步优化。

-O3 比 -O2 更进一步优化,包括 inline 函数。

-shared 生成共享目标文件。

通常用在建立共享库时。

-static 禁止使用共享连接。

-UMACRO 取消对 MACRO 宏的定义。

-w 不生成任何警告信息。

-Wall 生成所有警告信息。

1.8.3 gdbGNU 的调试器称为 gdb,该程序是一个交互式工具,工作在字符模式。

在 X Window 系统中,有一个 gdb 的前端图形工具,称为 xxgdb。

gdb 是功能强大的调试程序,可完成如下的调试任务:* 设置断点;* 监视程序变量的值;* 程序的单步执行;* 修改变量的值。

在可以使用 gdb 调试程序之前,必须使用 -g 选项编译源文件。

可在 makefile 中如下定义 CFLAGS 变量:CFLAGS = -g运行 gdb 调试程序时通常使用如下的命令:gdb progname在 gdb 提示符处键入help,将列出命令的分类,主要的分类有:* aliases:命令别名* breakpoints:断点定义;* data:数据查看;* files:指定并查看文件;* internals:维护命令;* running:程序执行;* stack:调用栈查看;* statu:状态查看;* tracepoints:跟踪程序执行。

键入 help 后跟命令的分类名,可获得该类命令的详细清单。

1.8.4 gdb 的常用命令表 1-4 常用的 gdb 命令命令解释break NUM 在指定的行上设置断点。

bt 显示所有的调用栈帧。

该命令可用来显示函数的调用顺序。

clear 删除设置在特定源文件、特定行上的断点。

其用法为:clear FILENAME:NUM。

continue 继续执行正在调试的程序。

该命令用在程序由于处理信号或断点而致停止运行时。

display EXPR 每次程序停止后显示表达式的值。

表达式由程序定义的变量组成。

file FILE 装载指定的可执行文件进行调试。

help NAME 显示指定命令的帮助信息。

info break 显示当前断点清单,包括到达断点处的次数等。

info files 显示被调试文件的详细信息。

info func 显示所有的函数名称。

info local 显示当函数中的局部变量信息。

info prog 显示被调试程序的执行状态。

info var 显示所有的全局和静态变量名称。

kill 终止正被调试的程序。

list 显示源代码段。

make 在不退出 gdb 的情况下运行 make 工具。

next 在不单步执行进入其他函数的情况下,向前执行一行源代码。

print EXPR 显示表达式 EXPR 的值。

1.8.5 gdb 使用范例-----------------清单一个有错误的 C 源程序 bugging.c-----------------#include#includestatic char buff [256];static char* string;int main (){printf ("Please input a string: ");gets (string);printf ("\nYour string is: %s\n", string);}-----------------上面这个程序非常简单,其目的是接受用户的输入,然后将用户的输入打印出来。

该程序使用了一个未经过初始化的字符串地址 string,因此,编译并运行之后,将出现 Segment Fault 错误:$ gcc -o test -g test.c$ ./testPlease input a string: asfdSegmentation fault (core dumped)为了查找该程序中出现的问题,我们利用 gdb,并按如下的步骤进行:1.运行 gdb bugging 命令,装入 bugging 可执行文件;2.执行装入的 bugging 命令;3.使用 where 命令查看程序出错的地方;4.利用 list 命令查看调用 gets 函数附近的代码;5.唯一能够导致 gets 函数出错的因素就是变量 string。

用 print 命令查看string 的值;6.在 gdb 中,我们可以直接修改变量的值,只要将 string 取一个合法的指针值就可以了,为此,我们在第11 行处设置断点;7.程序重新运行到第 11 行处停止,这时,我们可以用 set variable 命令修改 string 的取值;8.然后继续运行,将看到正确的程序运行结果。

#DEMO#1.8.6 其他程序/库工具strip:nm:size:string:。

相关文档
最新文档