linux下c语言创建进程实验报告,linux下的c语言编程实验报告

41528d3028836879cd698677c3999917.giflinux下的c语言编程实验报告

第五章:Linux 下的 C 语言编程姓名:学号:520913080429专业:信息安全 09-04实验内容:1.c 语言编程2.vi 编辑器3.gcc 编辑器4.gdb 编辑器5. gdb 中运行 Linux 的 shell 程序6.make7.env 系统变量1.c 语言编程一般模式编辑模式[root@localhost home]# cd /home/[root@localhost home]# vi hl.cYou have new mail in /var/spool/mail/root[root@localhost home]# lshl.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 *.hpci_ids.h:#define PCI_SUBVENDOR_ID_KEYSPAN 0 x11a9pci_ids.h:#define PCI_SUBDEVICE_ID_KEYSPAN_SX2 0 x53342.vi 编辑器一般模式 命令模式保存退出 不保存退出[Esc][:]wqq!编辑模式[root@localhost ~]# cd /home[root@localhost home]# vi cl.c[root@localhost home]# 3.gcc 编辑器gcc 的版本可以使用如下 gcc –v 命令查看:[david@DAVID david]$ gcc -vReading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specsConfigured 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-linuxThread model: posixgcc version 3.2.2 20030222 (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]# lscl.c student test.c vmware-tools-distrib[root@localhost home]# gcc test.c/usr/lib/gcc/i386-redhat-linux/4.1.1crt1.o: In function `_start :(.text+0 x18): undefined reference to `main collect2: ld 返回 1[root@localhost home]# . /a.outbash: /a.out: 没有那个文件或目录[root@localhost home]# gcc test.c -o test.i/usr/lib/gcc/i386-redhat-linux/4.1.1crt1.o: In function `_start :(.text+0 x18): undefined reference to `main collect2: ld 返回 1[root@localhost home]#Gcc 的警告提示功能 gcc 包含完整的出错检查和警告提示功能,它们可以帮助 Linux 程序员尽快找到错误代码,从而写出更加专业和优美的代码。先来读读例 3-2 所示的程序,这段代码写得很糟糕,仔细检查一下不难挑出如下毛病:main 函数的返回值被声明为 void,但实际上应该是 int; 使用了 GNU 语法扩展,即使用 long long 来声明 64 位整数,仍不符合 ANSI/ISO C 语言标准;main 函数在终止前没有调用 return 语句。 实例 3-2 bad.c¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬#include void main(void){long long int var = 1;printf(“It is not standard C code!\n“);}4.gdb 编辑器test.c#include int func(int n){int sum=0,i;for(i=0; i调用 Linux 的 shell 来执行,环境变量 SHELL 中定义的 Linux 的 shell 将会用来执行。如果 SHELL 没有定义,那就使用 Linux 的标准 shell:/bin/sh(在Windows 中使用 C 或 .)。还有一个 gdb 命令是 make:make 可以在 gdb 中执行 make 命令来重新 build 自己的程序。这个命令等价于 shell make 。 在 gdb 中运行程序当以 gdb 方式启动 gdb 后,gdb 会在 PATH 路径和当前目录中搜索 的源文件。如要确认 gdb 是否读到源文件,可使用 l 或 list 命令,看看 gdb 是否能列出源代码。在 gdb 中,运行程序使用 r 或是 run 命令。程序的运行,有可能需要设置下面四方面的事。程序运行参数set args 可指定运行时参数。如:set args 10 20 30 40 50show args 命令可以查看设置好的运行参数。运行环境path 可设定程序的运行路径。show paths 查看程序的运行路径。set environment varname [=value] 设置环境变量。如:set env USER=hchenshow environment [varname] 查看环境变量。工作目录cd 相当于 shell 的 cd 命令。pwd 显示当前的所在目录。程序的输入输出info terminal 显示程序用到的终端的模式。使用重定向控制程序输出。如:run

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值