第五章:
Linux下的C语言编程
姓名:
学号:520913080429
专业:信息安全09-04
实验内容:
1.c语言编程
2.vi编辑器
3.gcc编辑器
4.gdb编辑器
5. gdb中运行Linux的shell程序
6.make
7.env系统变量
c语言编程
一般模式
编辑模式
[root@localhost home]# cd /home/
[root@localhost home]# vi hl.c
You have new mail in /var/spool/mail/root
[root@localhost home]# ls
hl.c student vmware-tools-distrib
[root@localhost home]#
在调用C语言编译器的时候,可以通过给出“ -I”编译命令标志来引用保存在下级子目录或者非标准位置的头文件,类似命令如下:
[david@localhost linux]$ gcc -I /usr/openwin/include hello.c
先进入/usr/include子目录,然后在grep命令里给出该名字的几个字母,如下所示:
[david@localhost linux]$ grep KEYSPAN *.h
pci_ids.h:#define PCI_SUBVENDOR_ID_KEYSPAN 0x11a9
pci_ids.h:#define PCI_SUBDEVICE_ID_KEYSPAN_SX2 0x5334
vi编辑器
[root@localhost ~]# cd /home
[root@localhost home]# vi cl.c
[root@localhost home]#
3.gcc编辑器
gcc的版本可以使用如下gcc –v命令查看:
[david@DAVID david]$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/
sr/share/info --enable-shared --enable-threads=posix
--disable-checking --with-
ystem-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2(Red Hat Linux 3.2.2-5)
以上显示的就是Redhat linux 9.0里自带的gcc的版本3.2.2。
下面将以一个实例来说明如何使用gcc编译器。例3-1能够帮助大家迅速理解gcc的工作原理,并将其立即运用到实际的项目开发中去。
实例3-1 hello.c????????????????????????????
#include
int main (int argc,char **argv) {
printf("Hello Linux\n");
}
[root@localhost home]# ls
cl.c student test.c vmware-tools-distrib
[root@localhost home]# gcc test.c
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld 返回 1
[root@localhost home]# . /a.out
bash: /a.out: 没有那个文件或目录
[root@localhost home]# gcc test.c -o test.i
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld 返回 1
[root@localhost home]#
Gcc的警告提示功能
gcc包含完整的出错检查和警告提示功能,它们可以帮助Linux程序员尽快找到错误代码,从而写出更加专业和优美的代码。先来读读例3-2所示的程序,这段代码写得很糟糕,仔细检查一下