Linux学习笔记12--我的第一个C程序
2007-10-24 08:23
246 查看
今天学习点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 系统开发员》
先使用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 系统开发员》
相关文章推荐
- Linux学习笔记16--我的第一个PHP程序
- Android进阶#(7/12)装点程序“门面”——代码规范_第一个重点,排版
- H面试程序(12): 输出字符串中第一个只出现一次的字母
- H面试程序(12): 输出字符串中第一个只出现一次的字母
- 12、ffmpeg笔记-使用Android Studio编写第一个ffmpeg程序
- Java编程那些事儿12——第一个HelloWorld程序
- 编写你的第一个 Django 程序 第2部分
- 第一个cocos2d-x程序
- 配置XDMCP 从windows XP运行Fedora 12上的X程序
- 第一个struts2程序
- 第一个JAVA程序
- 我的第一个openCV程序,是改别人的成果,呵呵。
- 今天的第一个程序-南阳acm输入三个数排序
- Spring学习之第一个hello world程序
- Intellij Idea12第一个安卓程序开发(HelloWorld)及简单讲解Android
- 使用Keil MDK运行第一个STM32程序
- qt学习的第一个程序
- Spring Boot学习——第一个Spring Boot程序
- VS2012创建MFC项目、第一个VS2012版MFC程序及MFC程序发布(release)
- Struts2教程1:第一个Struts2程序