1、Linux程序设计基础实验指导第二章Linux程序设计基础C环境一,使用Vi编辑器完成以下4个文件的内容输入:hello.hstarfun.hhello.cstar.cStarfun.h 文件内容如下:/*starfun.h*/#ifndef STARFUN_H#define STARFUN_H#define NUM 4#define NUMBER 3int star1() int i,j,k; for(k=1;k=NUM;+k) for(i=1;i=(NUM-k);+i) printf( ); for(j=1;j=0;-k) for(i=1;i=(NUMBER-k+1);+i) printf
2、( ); for(j=1;j=(2*k-1);+j) printf(*); printf(n); return 0;#endifhello.h文件内容如下:/*hello.h*/#ifndef HELLO_H#define HELLO_Hvoid hello() star1(); printf(hello,my friendsn); #endifhello.c 文件内容如下:void showhello() hello();star.c文件内容如下:#include starfun.h#include hello.h#include int main() star1(); star2(); s
3、howhello(); return 0;Vi编辑器的使用,可以参考PPT和Vi.doc文档,在使用Vi编辑器的过程中,注意使用Vi的单行和多行复制命令,练习字符串查找替换命令,删除一个字符,删除光标后整个内容命令,删除一行命令,恢复删除,保存和退出命令等命令,并尝试使用其他命令。掌握#ifndef STARFUN_H #define STARFUN_H的宏定义用法二使用gcc编译器,编译程序第一种方法:分步进行1由star.c starfun.h 文件生成star.o 目标文件gcc -c star.c -o star.o2由hello.c hello.h starfun.h生成hello.
4、o目标文件gcc -c hello.c -o hello.o3由hello.o star.o 生成应用程序myproggcc star.o hello.o -o myprogrootlocalhost 01_hello# ./myprog * * * * * * * * *hello,my friends第二种方法:一条命令完成以上操作gcc star.c hello.c -o myprog结合本次课的PPT关于gcc部分,体会gcc编译器编译的过程,并在实验报告中描述在以上编译中尝试rootlocalhost 01_hello# gcc star.c hello.c -o myprogroo
5、tlocalhost 01_hello# gcc -w star.c hello.c -o myprogrootlocalhost 01_hello# gcc -Wall star.c hello.c -o myprogIn file included from star.c:1:starfun.h: In function star1:starfun.h:13: warning: implicit declaration of function printfstar.c: In function main:star.c:8: warning: implicit declaration of
6、function showhellohello.c: In function showhello:hello.c:4: warning: implicit declaration of function hello体会-Wall -w选项的作用查阅当前的gcc版本命令rootlocalhost 01_hello# gcc -vReading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specsConfigured with: ./configure -prefix=/usr -mandir=/usr/share/man -infod
7、ir=/usr/share/info -enable-shared -enable-threads=posix -disable-checking -with-system-zlib -enable-_cxa_atexit -host=i386-redhat-linuxThread model: posixgcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)三 使用动态库(1)rootlocalhost 01_hello# gcc -c -fpic hello.crootlocalhost 01_hello# lsamake hello.c he
8、llo.h hello.o makefile_01 makefile_02 makefile_03 Makefile_rule star.c starfun.h(2)rootlocalhost 01_hello# gcc -shared -s -o libhello.so hello.o rootlocalhost 01_hello# lsamake hello.c hello.h hello.o libhello.so makefile_01 makefile_02 makefile_03 Makefile_rule star.c starfun.h注意libhello.so库文件的命名格式
9、,(1)(2)也可以用下边命令替代gcc -fpic -shared -s hello.c -o libhello.sorootlocalhost 01_hello# cp libhello.so /usr/lib 注意/usr/lib为用户库自动搜索路径rootlocalhost 01_hello# gcc -lhello star.c -o mystarrootlocalhost 01_hello# ldd mystar libhello.so = /usr/lib/libhello.so (0x4002d000) libc.so.6 = /lib/tls/libc.so.6 (0x420
10、00000) /lib/ld-linux.so.2 = /lib/ld-linux.so.2 (0x40000000)rootlocalhost 01_hello# ./mystar * * * * * * * * *hello,my friendsrootlocalhost 01_hello#四,使用静态库rootlocalhost 01_hello# rm *.orm:是否删除一般文件hello.o? yrootlocalhost 01_hello# rm mystar rm:是否删除一般文件mystar? yrootlocalhost 01_hello#rootlocalhost 01_
11、hello# rm libhello.*rootlocalhost 01_hello# gcc -c hello.c -o hello.orootlocalhost 01_hello# ar -rc libhello.a hello.orootlocalhost 01_hello# gcc star.c libhello.a -o mystarrootlocalhost 01_hello# ./mystar * * * * * * * * *hello,my friends五 调试器的使用1gdb进入02_gdb 目录执行rootlocalhost 02_gdb# gcc -g eg1.c -
12、o eg1rootlocalhost 02_gdb# gdb eg1GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)Copyright 2003 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type show copying to
13、see the conditions.There is absolutely no warranty for GDB. Type show warranty for details.This GDB was configured as i386-redhat-linux-gnu.(gdb) runStarting program: /home/qianzg/ARM2410DEMO/02_gdb/eg1 Program received signal SIGFPE, Arithmetic exception.0x0804833f in wib (no1=8, no2=8) at eg1.c:66
14、 result = no1 / diff; 参考“使用 GDB 调试 Linux 软件.htm ”使用gdb2在开发板上进行远程调试在开发板上/mnt/yaffs/explseg1 gdbserver myshell termeg2 hello pthread/mnt/yaffs/exp./eg1Floating point exception/mnt/yaffs/expgdbserver 192.168.0.199:1234 eg1Process eg1 created; pid = 121Remote debugging from host 192.168.0.199在PC上rootloc
15、alhost 02_gdb# armv4l-unknown-linux-gdb eg1GNU gdb 5.2.1Copyright 2002 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome to change it and/or distribute copies of it under certain conditions.Type show copying to see the conditions.There
16、is absolutely no warranty for GDB. Type show warranty for details.This GDB was configured as -host=i686-pc-linux-gnu -target=armv4l-unknown-linux.(gdb) target remote 192.168.0.191:1234 Remote debugging using 192.168.0.191:12340x40000d00 in ? ()(gdb) list3 4 int result, diff;5 diff = no1 - no2;6 resu
17、lt = no1 / diff;7 return result;8 9 int main(int argc, char *argv)10 11 int value, div, result, i, total;12 value = 10;(gdb) info locals No symbol table info available.(gdb) break 17Breakpoint 1 at 0x2000424: file eg1.c, line 17.(gdb) continue Continuing. Breakpoint 1, main (argc=1, argv=0xbffffea4)
18、 at eg1.c:1717 result = wib(value, div);(gdb) info locals value = 10div = 6result = 1075053560i = 0total = 0(gdb) bt#0 main (argc=1, argv=0xbffffea4) at eg1.c:17(gdb)六,Makefile文件的编写1使用Makefile理论部分请参阅PPT和“熟悉Linux开发环境.doc”编写Makefile文件由该Makefile文件编译第一节中的hello.h starfun.h hello.c star.c 四个文件2使用automaker
19、ootlocalhost hello#vi hello.crootlocalhost hello# lshello.crootlocalhost hello# autoscan rootlocalhost hello# lsautoscan.log configure.scan hello.crootlocalhost hello# vi configure.scan rootlocalhost hello# lsautoscan.log configure.scan hello.crootlocalhost hello# mv configure.scan configure.inrootl
20、ocalhost hello# lsautoscan.log configure.in hello.c rootlocalhost hello# vi configure.in rootlocalhost hello# aclocalrootlocalhost hello# lsaclocal.m4 autoscan.log configure.in hello.crootlocalhost hello# autoconf rootlocalhost hello# lsaclocal.m4 autom4te.cache autoscan.log configure configure.in h
21、ello.crootlocalhost hello# vi Makefile.am rootlocalhost hello# lsaclocal.m4 autom4te.cache autoscan.log configure configure.in hello.c Makefile.amrootlocalhost hello# automake -add-missing configure.in: installing ./install-shconfigure.in: installing ./mkinstalldirsconfigure.in: installing ./missing
22、Makefile.am: installing ./depcomprootlocalhost hello# lsaclocal.m4 autoscan.log configure.in hello.c Makefile.am missingautom4te.cache configure depcomp install-sh Makefile.in mkinstalldirsrootlocalhost hello# ./configure checking for a BSD-compatible install. /usr/bin/install -cchecking whether bui
23、ld environment is sane. yeschecking for gawk. gawkchecking whether make sets $(MAKE). yeschecking for gcc. gccchecking for C compiler default output. a.outchecking whether the C compiler works. yeschecking whether we are cross compiling. nochecking for suffix of executables. checking for suffix of o
24、bject files. ochecking whether we are using the GNU C compiler. yeschecking whether gcc accepts -g. yeschecking for gcc option to accept ANSI C. none neededchecking for style of include used by make. GNUchecking dependency style of gcc. gcc3configure: creating ./config.statusconfig.status: creating
25、Makefileconfig.status: executing depfiles commandsrootlocalhost hello#makesource=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= -DPACKA
26、GE=hello -DVERSION=1.0 -I. -I. -g -O2 -c test -f hello.c | echo ./hello.cgcc -g -O2 -o hello hello.o rootlocalhost hello# ./hello Hello, GNU!rootlocalhost hello#关于configure.in的内容rootlocalhost hello# cat configure.in# -*- Autoconf -*-# Process this file with autoconf to produce a configure script. AC_INIT(hello.c)AM_INIT_AUTOMAKE(hello,1.0) # Checks for programs.AC_PROG_CC # Checks for libraries. # Checks for header files. # Checks for typedefs, structures, and compiler characteristics.# Checks for library functions.AC_OUTPUT(Makefile)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1