1、Linux程序设计基础C环境Linux程序设计基础C环境一 VI编辑器的使用使用VI编辑下列几个函数文件: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(); sho
3、whello(); return 0;Vi编辑器的使用,可以参考PPT,在使用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.o目标文件gcc -c
4、 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 myprogrootlocalhost
5、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 function sh
6、owhellohello.c: In function showhello:hello.c:4: warning: implicit declaration of function hello体会-Wll -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 -infodir=/usr/shar
7、e/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 hello.h hello.
8、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库文件的命名格式,(1)(2)也可以用下
9、边命令替代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 (0x42000000) /lib/
10、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_hello# rm li
11、bhello.*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_gdbserver 目录执行rootvm-dev 02_gdb_gdbserver# gcc -g eg
12、1.c -o eg1rootvm-dev 02_gdb_gdbserver# gdb eg1GNU gdb Red Hat Linux (6.3.0.0-1.132.EL4rh)Copyright 2004 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 cop
13、ying to see the conditions.There is absolutely no warranty for GDB. Type show warranty for details.This GDB was configured as i386-redhat-linux-gnu.Using host libthread_db library /lib/tls/libthread_db.so.1. (gdb) runStarting program: /home/sprife/xpide/02_gdb_gdbserver/eg1 Program received signal S
14、IGFPE, Arithmetic exception.0x08048388 in wib (no1=8, no2=8) at eg1.c:66 result = no1 / diff;(gdb) (gdb) list1 #include 2 int wib(int no1, int no2)3 4 int result, diff;5 diff = no1 - no2;6 result = no1 / diff;7 return result;8 9 int main(int argc, char *argv)10 (gdb) list11 int value, div, result, i
15、, total;12 value = 10;13 div = 6;14 total = 0;15 for(i = 0; i 10; i+)16 17 result = wib(value, div);18 total += result;19 div+;20 value-;(gdb) list21 22 printf(%d wibed by %d equals %dn, value, div, total);23 return 0;24 (gdb) Line number 25 out of range; eg1.c has 24 lines.(gdb) print no1$1 = 8(gdb
16、) print diff$2 = 0(gdb)(gdb) break 17Breakpoint 1 at 0x80483d6: file eg1.c, line 17.(gdb) runStarting program: /home/sprife/xpide/02_gdb_gdbserver/eg1 Breakpoint 1, main (argc=1, argv=0xbff1a684) at eg1.c:1717 result = wib(value, div);(gdb) info localsvalue = 10div = 6result = 7602164i = 0total = 0(
17、gdb) cContinuing. Breakpoint 1, main (argc=1, argv=0xbff1a684) at eg1.c:1717 result = wib(value, div);(gdb) info localsvalue = 9div = 7result = 2i = 1total = 2(gdb) cContinuing. Breakpoint 1, main (argc=1, argv=0xbff1a684) at eg1.c:1717 result = wib(value, div);(gdb) info localsvalue = 8div = 8resul
18、t = 4i = 2total = 6(gdb) cContinuing. Program received signal SIGFPE, Arithmetic exception.0x08048388 in wib (no1=8, no2=8) at eg1.c:66 result = no1 / diff;(gdb) info localsvalue = 8div = 8result = 4i = 2total = 6(gdb) cContinuing. Program received signal SIGFPE, Arithmetic exception.0x08048388 in w
19、ib (no1=8, no2=8) at eg1.c:66 result = no1 / diff;(gdb) info localsresult = 4diff = 0(gdb)参考“使用 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 c
20、reated; pid = 121Remote debugging from host 192.168.0.199在PC上rootlocalhost 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 und
21、er certain conditions.Type show copying to see the conditions.There 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:12340x4000
22、0d00 in ? ()(gdb) list3 4 int result, diff;5 diff = no1 - no2;6 result = 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.(gd
23、b) continue Continuing. Breakpoint 1, main (argc=1, argv=0xbffffea4) 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的编写(参考)一、Makefile介绍Makefile是用于自动编译和链接的,一个工程有很多文件组成,每一个文件
24、的改变都会导致工程的重新链接,但是不是所有的文件都需要重新编译,Makefile中纪录有文件的信息,在make时会决定在链接的时候需要重新编译哪些文件。Makefile的宗旨就是:让编译器知道要编译一个文件需要依赖其他的哪些文件。当那些依赖文件有了改变,编译器会自动的发现最终的生成文件已经过时,而重新编译相应的模块。Makefile的基本结构不是很复杂,但当一个程序开发人员开始写Makefile时,经常会怀疑自己写的是否符合惯例,而且自己写的Makefile经常和自己的开发环境相关联,当系统环境变量或路径发生了变化后,Makefile可能还要跟着修改。这样就造成了手工书写Makefile的诸多
25、问题,automake恰好能很好地帮助我们解决这些问题。使用automake,程序开发人员只需要写一些简单的含有预定义宏的文件,由autoconf根据一个宏文件生成configure,由automake根据另一个宏文件生成Makefile.in,再使用configure依据Makefile.in来生成一个符合惯例的Makefile。下面我们将详细介绍Makefile的automake生成方法。二、使用的环境本文所提到的程序是基于Linux发行版本:Fedora Core release 1,它包含了我们要用到的autoconf,automake。三、从helloworld入手我们从大家最常使用
26、的例子程序helloworld开始。下面的过程如果简单地说来就是:新建三个文件:helloworld.cconfigure.inMakefile.am然后执行:aclocal; autoconf; automake -add-missing; ./configure; make; ./helloworld就可以看到Makefile被产生出来,而且可以将helloworld.c编译通过。很简单吧,几条命令就可以做出一个符合惯例的Makefile,感觉如何呀。现在开始介绍详细的过程:1、建目录在你的工作目录下建一个helloworld目录,我们用它来存放helloworld程序及相关文件,如在/h
27、ome/my/build下:$ mkdir helloword$ cd helloworld2、 helloworld.c然后用你自己最喜欢的编辑器写一个hellowrold.c文件,如命令:vi helloworld.c。使用下面的代码作为helloworld.c的内容。int main(int argc, char* argv)printf(Hello, Linux World!n);return 0;完成后保存退出。好了,现在在helloworld目录下就应该有一个你自己写的helloworld.c了。3、生成configure我们使用autoscan命令来帮助我们根据目录下的源代码生成
28、一个configure.in的模板文件。命令:$ autoscan$ lsconfigure.scan helloworld.c执行后在hellowrold目录下会生成一个文件:configure.scan,我们可以拿它作为configure.in的蓝本。现在将configure.scan改名为configure.in,并且编辑它,按下面的内容修改,去掉无关的语句:=configure.in内容开始=# -*- Autoconf -*-# Process this file with autoconf to produce a configure script.AC_INIT(helloworld.c)AM_INIT_AUTOMAKE(helloworld, 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)=configure.in内容结束=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1