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 系统开发员》
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架