预览加载中,请您耐心等待几秒...
1/7
2/7
3/7
4/7
5/7
6/7
7/7

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

课程名称:软件工程 实验项目名称:实验二GNUCompilerCollection的使用 一、实验目的: 1.掌握GNUC编译器(gcc)的用法; 2.学会怎样编译一个程序和怎样使用用于优化和调试的基本编译器选项。 二、实验环境: 硬件:PC机,coreⅡ双核CPU,2G以上内存; 软件:windows 三、实验内容及结果: 1、用gcc编译C程序。 1)编译一个简单的C程序,程序代码及编译命令如下: C程序:#include<stdio.h> int main(void) { printf("Hello,world!\n"); return0; } 编译命令 lenovo@6111-06~ $pwd /home/lenovo lenovo@6111-06~ $gcc-Wallhello.c-ohell lenovo@6111-06~ $./hello Hello,world! lenovo@6111-06~ $ 2)在简单程序中找到错误,程序代码及编译命令如下: C程序: #include<stdio.h> int main(void) { printf("Twoplustwois%f\n",4); return0; } 编译命令: lenovo@6111-06~ $gcc-Wallbad.c-obad bad.c:Infunction'main': bad.c:6:1:warning:format'%f'expectsargumentoftype'double',butargument 2hastype'int'[-Wformat=] printf("Twoplustwois%f\n",4); ^ lenovo@6111-06~ $ 3)编译多个源文件,程序代码及编译命令如下: 程序代码 Main:#include"hello.h" int main(void) { hello("world"); return0; } Hello_fn: #include<stdio.h> #include"hello.h" void hello(constchar*name) { printf("Hello,%s!\n",name); } Hello:voidhello(constchar*name); 编译命令: lenovo@6111-06~ $gcc-Wallmain.chello_fn.c-onewhello lenovo@6111-06~ $./newhello Hello,world! 2、独立地编译文件。 1)从源文件生成对象文件,程序代码及编译命令如下: lenovo@6111-06~ $gcc-Wall-cmain.c lenovo@6111-06~ $gcc-Wall-chello_fn.c lenovo@6111-06~ $ 2)从对象文件生成可执行文件,程序代码及编译命令如下: lenovo@6111-06~ $gccmain.ohello_fn.o-ohello lenovo@6111-06~ $./hello Hello,world! lenovo@6111-06~ $ 3)从对象文件的链接次序,程序代码及编译命令如下: lenovo@6111-06~ $gccmain.ohello_fn.o-ohello lenovo@6111-06~ $gcchello_fn.omain.o-ohello lenovo@6111-06~ $ 4)重新编译和重新链接,程序代码及编译命令如下: C程序:#include"hello.h" int main(void) { hello("everyone");/*changedfrom"world"*/ return0; } 命令: lenovo@6111-06~ $gcc-Wall-cmain.c lenovo@6111-06~ $gccmain.ohello_fn.o-ohello lenovo@6111-06~ $./hello Hello,everyone! lenovo@6111-06~ $ 5)与外部库文件链接,程序代码及编译命令如下: #include<math.h> #include<stdio.h> int main(void) { doublex=sqrt(2.0); printf("Thesquarerootof2.0is%f\n",x); return0; } lenovo@6111-06~ $gcc-Wallcalc.c-ocalc lenovo@6111-06~ $gcc-Wallcalc.c/usr/lib/libm.a-ocalc gcc.exe:error:C:/MinGW