Linux程序设计环境上机.docx
- 文档编号:6388858
- 上传时间:2023-01-06
- 格式:DOCX
- 页数:13
- 大小:185.18KB
Linux程序设计环境上机.docx
《Linux程序设计环境上机.docx》由会员分享,可在线阅读,更多相关《Linux程序设计环境上机.docx(13页珍藏版)》请在冰豆网上搜索。
Linux程序设计环境上机
第二章Linux程序设计基础—C环境
一,使用Vi编辑器完成以下4个文件的内容输入:
hello.h
starfun.h
hello.c
star.c
Starfun.h文件内容如下:
/*****starfun.h*****/
#ifndefSTARFUN_H
#defineSTARFUN_H
#defineNUM4
#defineNUMBER3
intstar1(){
inti,j,k;
for(k=1;k<=NUM;++k){
for(i=1;i<=(NUM-k);++i)
printf("");
for(j=1;j<=(2*k-1);++j)
printf("*");
printf("\n");
}
return0;
}
intstar2(){
inti,j,k;
for(k=NUMBER;k>=0;--k){
for(i=1;i<=(NUMBER-k+1);++i)
printf("");
for(j=1;j<=(2*k-1);++j)
printf("*");
printf("\n");
}
return0;
}
#endif
hello.h文件内容如下:
/*hello.h*/
#ifndefHELLO_H
#defineHELLO_H
voidhello(){
star1();
printf("hello,myfriends\n");
}
#endif
hello.c文件内容如下:
voidshowhello(){
hello();
}
star.c文件内容如下:
#include"starfun.h"
#include"hello.h"
#include
intmain(){
star1();
star2();
showhello();
return0;
}
Vi编辑器的使用,可以参考PPT和Vi.doc文档,在使用Vi编辑器的过程中,注意使用Vi的单行和多行复制命令,练习字符串查找替换命令,删除一个字符,删除光标后整个内容命令,删除一行命令,恢复删除,保存和退出命令等命令,并尝试使用其他命令。
掌握
#ifndefSTARFUN_H
#defineSTARFUN_H
的宏定义用法
二.使用gcc编译器,编译程序
第一种方法:
分步进行
1.由star.cstarfun.h文件生成star.o目标文件
gcc-cstar.c-ostar.o
2.由hello.chello.hstarfun.h生成hello.o目标文件
gcc-chello.c-ohello.o
3.由hello.ostar.o生成应用程序myprog
gccstar.ohello.o-omyprog
[root@localhost01_hello]#./myprog
*
***
*****
*******
*****
***
*
*
***
*****
*******
hello,myfriends
第二种方法:
一条命令完成以上操作
gccstar.chello.c-omyprog
结合本次课的PPT关于gcc部分,体会gcc编译器编译的过程,并在实验报告中描述
在以上编译中尝试
[root@localhost01_hello]#gccstar.chello.c-omyprog
[root@localhost01_hello]#gcc-wstar.chello.c-omyprog
[root@localhost01_hello]#gcc-Wallstar.chello.c-omyprog
Infileincludedfromstar.c:
1:
starfun.h:
Infunction`star1':
starfun.h:
13:
warning:
implicitdeclarationoffunction`printf'
star.c:
Infunction`main':
star.c:
8:
warning:
implicitdeclarationoffunction`showhello'
hello.c:
Infunction`showhello':
hello.c:
4:
warning:
implicitdeclarationoffunction`hello'
体会-Wll-w选项的作用
查阅当前的gcc版本命令
[root@localhost01_hello]#gcc-v
Readingspecsfrom/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configuredwith:
../configure--prefix=/usr--mandir=/usr/share/man--infodir=/usr/share/info--enable-shared--enable-threads=posix--disable-checking--with-system-zlib--enable-__cxa_atexit--host=i386-redhat-linux
Threadmodel:
posix
gccversion3.2.220030222(RedHatLinux3.2.2-5)
三.使用动态库
(1)[root@localhost01_hello]#gcc-c-fpichello.c
[root@localhost01_hello]#ls
amakehello.chello.hhello.omakefile_01makefile_02makefile_03Makefile_rulestar.cstarfun.h
(2)[root@localhost01_hello]#gcc-shared-s-olibhello.sohello.o
[root@localhost01_hello]#ls
amakehello.chello.hhello.olibhello.somakefile_01makefile_02makefile_03Makefile_rulestar.cstarfun.h
注意libhello.so库文件的命名格式,
(1)
(2)也可以用下边命令替代
gcc-fpic-shared-shello.c-olibhello.so
[root@localhost01_hello]#cplibhello.so/usr/lib
注意/usr/lib为用户库自动搜索路径
[root@localhost01_hello]#gcc-lhellostar.c-omystar
[root@localhost01_hello]#lddmystar
libhello.so=>/usr/lib/libhello.so(0x4002d000)
libc.so.6=>/lib/tls/libc.so.6(0x42000000)
/lib/ld-linux.so.2=>/lib/ld-linux.so.2(0x40000000)
[root@localhost01_hello]#./mystar
*
***
*****
*******
*****
***
*
*
***
*****
*******
hello,myfriends
[root@localhost01_hello]#
四,使用静态库
[root@localhost01_hello]#rm*.o
rm:
是否删除一般文件‘hello.o’?
y
[root@localhost01_hello]#rmmystar
rm:
是否删除一般文件‘mystar’?
y
[root@localhost01_hello]#
[root@localhost01_hello]#rmlibhello.*
[root@localhost01_hello]#gcc-chello.c-ohello.o
[root@localhost01_hello]#ar-rclibhello.ahello.o
[root@localhost01_hello]#gccstar.clibhello.a-omystar
[root@localhost01_hello]#./mystar
*
***
*****
*******
*****
***
*
*
***
*****
*******
hello,myfriends
五.调试器的使用
1.gdb
进入02_gdb目录
执行
[root@localhost02_gdb]#gcc-geg1.c-oeg1
[root@localhost02_gdb]#gdbeg1
GNUgdbRedHatLinux(5.3post-0.20021129.18rh)
Copyright2003FreeSoftwareFoundation,Inc.
GDBisfreesoftware,coveredbytheGNUGeneralPublicLicense,andyouare
welcometochangeitand/ordistributecopiesofitundercertainconditions.
Type"showcopying"toseetheconditions.
ThereisabsolutelynowarrantyforGDB.Type"showwarranty"fordetails.
ThisGDBwasconfiguredas"i386-redhat-linux-gnu"...
(gdb)run
Startingprogram:
/home/qianzg/ARM2410DEMO/02_gdb/eg1
ProgramreceivedsignalSIGFPE,Arithmeticexception.
0x0804833finwib(no1=8,no2=8)ateg1.c:
6
6result=no1/diff;
参考“使用GDB调试Linux软件.htm”使用gdb
2.在开发板上进行远程调试
在开发板上
[/mnt/yaffs/exp]ls
eg1gdbservermyshellterm
eg2hellopthread
[/mnt/yaffs/exp]./eg1
Floatingpointexception
[/mnt/yaffs/exp]gdbserver192.168.0.199:
1234eg1
Processeg1created;pid=121
Remotedebuggingfromhost192.168.0.199
在PC上
[root@localhost02_gdb]#armv4l-unknown-linux-gdbeg1
GNUgdb5.2.1
Copyright2002FreeSoftwareFoundation,Inc.
GDBisfreesoftware,coveredbytheGNUGeneralPublicLicense,andyouare
welcometochangeitand/ordistributecopiesofitundercertainconditions.
Type"showcopying"toseetheconditions.
ThereisabsolutelynowarrantyforGDB.Type"showwarranty"fordetails.
ThisGDBwasconfiguredas"--host=i686-pc-linux-gnu--target=armv4l-unknown-linux"...
(gdb)targetremote192.168.0.191:
1234
Remotedebuggingusing192.168.0.191:
1234
0x40000d00in?
?
()
(gdb)list
3{
4intresult,diff;
5diff=no1-no2;
6result=no1/diff;
7returnresult;
8}
9intmain(intargc,char*argv[])
10{
11intvalue,div,result,i,total;
12value=10;
(gdb)infolocals
Nosymboltableinfoavailable.
(gdb)break17
Breakpoint1at0x2000424:
fileeg1.c,line17.
(gdb)continue
Continuing.
Breakpoint1,main(argc=1,argv=0xbffffea4)ateg1.c:
17
17result=wib(value,div);
(gdb)infolocals
value=10
div=6
result=1075053560
i=0
total=0
(gdb)bt
#0main(argc=1,argv=0xbffffea4)ateg1.c:
17
(gdb)
六,Makefile文件的编写
1.使用Makefile
理论部分请参阅PPT和“熟悉Linux开发环境.doc”编写Makefile文件
由该Makefile文件编译第一节中的hello.hstarfun.hhello.cstar.c四个文件
2.使用automake
[root@localhosthello]#vihello.c
[root@localhosthello]#ls
hello.c
[root@localhosthello]#autoscan
[root@localhosthello]#ls
autoscan.logconfigure.scanhello.c
[root@localhosthello]#viconfigure.scan
[root@localhosthello]#ls
autoscan.logconfigure.scanhello.c
[root@localhosthello]#mvconfigure.scanconfigure.in
[root@localhosthello]#ls
autoscan.logconfigure.inhello.c
[root@localhosthello]#viconfigure.in
[root@localhosthello]#aclocal
[root@localhosthello]#ls
aclocal.m4autoscan.logconfigure.inhello.c
[root@localhosthello]#autoconf
[root@localhosthello]#ls
aclocal.m4autom4te.cacheautoscan.logconfigureconfigure.inhello.c
[root@localhosthello]#viMakefile.am
[root@localhosthello]#ls
aclocal.m4autom4te.cacheautoscan.logconfigureconfigure.inhello.cMakefile.am
[root@localhosthello]#automake--add-missing
configure.in:
installing`./install-sh'
configure.in:
installing`./mkinstalldirs'
configure.in:
installing`./missing'
Makefile.am:
installing`./depcomp'
[root@localhosthello]#ls
aclocal.m4autoscan.logconfigure.inhello.cMakefile.ammissing
autom4te.cacheconfiguredepcompinstall-shMakefile.inmkinstalldirs
[root@localhosthello]#./configure
checkingforaBSD-compatibleinstall.../usr/bin/install-c
checkingwhetherbuildenvironmentissane...yes
checkingforgawk...gawk
checkingwhethermakesets$(MAKE)...yes
checkingforgcc...gcc
checkingforCcompilerdefaultoutput...a.out
checkingwhethertheCcompilerworks...yes
checkingwhetherwearecrosscompiling...no
checkingforsuffixofexecutables...
checkingforsuffixofobjectfiles...o
checkingwhetherweareusingtheGNUCcompiler...yes
checkingwhethergccaccepts-g...yes
checkingforgccoptiontoacceptANSIC...noneneeded
checkingforstyleofincludeusedbymake...GNU
checkingdependencystyleofgcc...gcc3
configure:
creating./config.status
config.status:
creatingMakefile
config.status:
executingdepfilescommands
[root@localhosthello]#make
source='hello.c'object='hello.o'libtool=no\
depfile='.deps/hello.Po'tmpdepfile='.deps/hello.TPo'\
depmode=gcc3/bin/sh./depcomp\
gcc-DPACKAGE_NAME=\"\"-DPACKAGE_TARNAME=\"\"-DPACKAGE_VERSION=\"\"-DPACKAGE_STRING=\"\"-DPACKAGE_BUGREPORT=\"\"-DPACKAGE=\"hello\"-DVERSION=\"1.0\"-I.-I.-g-O2-c`test-f'hello.c'||echo'./'`hello.c
gcc-g-O2-ohellohello.o
[root@localhosthello]#./hello
Hello,GNU!
[root@localhosthello]#
关于configure.in的内容
[root@localhosthello]#catconfigure.in
#-*-Autoconf-*-
#Processthisfilewithautoconftoproduceaconfigurescript.
AC_INIT(hello.c)
AM_INIT_AUTOMAKE(hello,1.0)
#Checksforprograms.
AC_PROG_CC
#Checksforlibraries.
#Checksforheaderfiles.
#Checksfortypedefs,structures,andcompilercharacteristics.
#Checksforlibraryfunctions.
AC_OUTPUT(Makefile)
七,CVS的使用
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Linux 程序设计 环境 上机