Linux学习笔记12我的第一个C程序

今天学习点c语言编程,学习c语言编程时要了解如何编译调式及运行c语言程序。

先使用vi新建一个mytestc.c文件。
并且输入相关的c程序代码如下操作:
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
in main()
{
        printf("Hello,this is my first program.");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" [New] 5L, 77C written
  
--开始编译。
--gcc主要时用来编译c/c++程序的。
--这里只用到gcc的简单参数-o 和 -g。
-- -o 表示设定输出文件名。
-- -g 表示加入调式信息,为gdb准备                         
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c 
mytestc.c:2: error: syntax error before "main"
--编译错误,提示"main"前面语法错误,这里再使用vi来修改。发现main()前的数据类型int写成了in 比较晕哦,太马虎了。
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
int main()
{
        printf("Hello,this is my first program.");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" 5L, 78C written                                  
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c
[OK_008@CentOS4 ~]$ ls
--没有任何提示,说明测试通过,使用ls,我们发现目下下多了个编译后的mytestc文件。
debugtest    Desktop  mytestc.c   mytxt.txt   TestFile
debugtest.c  mytestc  mytxt1.txt  OK008Filed  untar
[OK_008@CentOS4 ~]$ ./mytestc 
--该命令是执行刚才生成的mytestc文件。
Hello,this is my first program.[OK_008@CentOS4 ~]$ --这里是执行结果,ok成功了
--为了好看,我修改一下程序加个回车。
[OK_008@CentOS4 ~]$ vi mytestc.c

#include "stdio.h"
int main()
{
        printf("Hello,this is my first program.\n");
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mytestc.c" 5L, 80C written
[OK_008@CentOS4 ~]$ gcc -o mytestc -g mytestc.c
--重新编译
[OK_008@CentOS4 ~]$ ./mytestc
Hello,this is my first program.
  --成功。
[OK_008@CentOS4 ~]$

原来在linux下编译c也不是很难。呵呵,鼓励一下自己,忽悠呀。
其实gcc命令中还有很多的参数没有使用。
这里也没有使用到gdb调试工具来调式代码,还有可以make和makefile来完成自动维护编译工作,功能不错。
到用到的时候再具体了解。

参考资料:《Linux 系统开发员》

原文地址:https://www.cnblogs.com/wghao/p/935451.html