Linux系统开发之C语言基础(1)

简介: Linux系统开发之C语言基础

先来看一个经典小程序hello world!

1#include "stdio.h"
2#include "stdlib.h"
3
4int main(void)
5{
6    printf("Hello world!\n");
7    exit(0);
8}

注意:

①printf函数在stdio.h文件里,需要包含头文件stdio.h

②exit函数在stdlib.h文件里,需要包含头文件stdlib.h文件

③‘\n'除了换行的作用外,还有刷新缓冲区的功能

④关于main函数的写法,读者可能见过很多种,包括

void main(void)

void main()

int main()

int main(void)

int main(int agrc, char** argv)// char* agrv[]

编者在这里建议读者使用后面两种写法,严格上讲这两种才是标准的。

学习Linux最好的方式就是查man手册

我来man一下exit函数

在linux终端下输入: man 3 exit

1NAME
 2       exit - cause normal process termination
 3
 4SYNOPSIS
 5       #include <stdlib.h>
 6
 7       void exit(int status);
 8
 9DESCRIPTION
10       The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).
11
12       All  functions  registered with atexit(3) and on_exit(3) are called, in the reverse order of their registration.  (It is possible for one of these func‐
13       tions to use atexit(3) or on_exit(3) to register an additional function to be executed during exit processing; the new  registration  is  added  to  the
14       front  of  the  list of functions that remain to be called.)  If one of these functions does not return (e.g., it calls _exit(2), or kills itself with a
15       signal), then none of the remaining functions is called, and further exit processing (in particular, flushing of stdio(3) streams) is abandoned.   If  a
16       function has been registered multiple times using atexit(3) or on_exit(3), then it is called as many times as it was registered.
17
18       All open stdio(3) streams are flushed and closed.  Files created by tmpfile(3) are removed.
19
20       The  C standard specifies two constants, EXIT_SUCCESS and EXIT_FAILURE, that may be passed to exit() to indicate successful or unsuccessful termination,
21       respectively.
22
23RETURN VALUE
24       The exit() function does not return.

如果你英文不太好,没关系,这里先提供谷歌翻译给你~

1名称
 2       退出-导致正常进程终止
 3
 4概要
 5       #include <stdlib.h>
 6
 7       void exit(int status);
 8
 9描述
10       exit()函数导致正常进程终止,并且状态&0377的值返回给父级(请参阅wait(2))。
11
12       在atexit(3)和on_exit(3)上注册的所有函数均以与注册相反的顺序调用。 (这些功能之一可能
13       使用atexit(3)或on_exit(3)来注册要在退出处理期间执行的其他功能的说明;新注册已添加到
14       如果这些函数之一未返回(例如,它调用_exit(2)或使用
15       信号),则不调用其余功能,而放弃进一步的退出处理(特别是stdio(3)流的刷新)。如果一个
16       已使用atexit(3)或on_exit(3)多次注册该函数,然后调用该函数的次数与已注册的次数相同。
17
18       所有打开的stdio(3)流都被刷新并关闭。由tmpfile(3)创建的文件将被删除。
19
20       C标准指定了两个常量EXIT_SUCCESS和EXIT_FAILURE,可以将其传递给exit()来指示成功或失败的终止,
21       分别。
22
23返回值
24       exit()函数不会返回。

我们来编译一下hello.c

一个源文件经过一个什么样的过程才能被执行?

  源文件-->预处理-->编译-->汇编-->链接-->可执行文件

预处理

以#开头的命令称为预处理命令,像#include, #if, #ifndef, #indef等命令,预处理是将宏定义展开,根据条件编译选择使用到的代码,输出到.i文件等待下一步处理.

使用arm-linux-cpp工具

编译

编译就是将.i文件翻译成汇编代码

使用ccl工具

汇编

汇编就是将上一步输出的文件翻译成符合一定格式的机器码,机器码就是机器识别的代码,例如01010101这样的.在linux系统一般为elf文件或者obj文件.

使用arm-linux-as工具.

链接

链接就是将上一步得到的文件跟库文件链接起来,最终生成可以在特定平台运行的可执行文件.

运行如下:

Dookdpig:~ work/linux c study/001 gcc vims gcc -o hello hello.c bookdpig:~/work/linux_c study/001_gcc_vim$ ls hello hello.c
book@pig:~/work/linux_c_study/001_gcc_vim$ ./hello
Hello world! book@pig:~/work/linux_c_study/001_gcc_vim$

号主:一枚机械专业本科生,经历了转行,从外包逆袭到芯片原厂的Linux驱动开发工程师,深入操作系统的世界,贯彻终身学习、终身成长的理念。平时喜欢折腾,寒冬之下,抱团取暖,期待你来一起探讨技术、搞自媒体副业,程序员接单和投资理财。【对了,不定期送闲置开发板、书籍、键盘等等】。

如果你想了解我的转行经验,欢迎找我交流~gongzhong号【哆哆jarvis】

一起不断探索自我、走出迷茫、找到热爱,希望和你成为朋友,一起成长~

相关文章
|
2月前
|
存储 Linux C语言
Linux系统下C语言的构造数据类型
Linux系统下C语言的构造数据类型
12 0
|
2月前
|
存储 Linux C语言
Linux系统下C语言的文件操作
Linux系统下C语言的文件操作
19 0
|
2天前
|
存储 Shell Linux
操作系统实战(一)(linux+C语言)
本篇文章重点在于利用linux系统的完成操作系统的实验,巩固课堂知识
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
23 5
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
15 3
|
1月前
|
Linux API C语言
lua 如何在嵌入式Linux中与c语言结合
lua 如何在嵌入式Linux中与c语言结合
12 1
|
1月前
|
小程序 Linux API
Linux用C语言模拟‘ls‘命令
Linux用C语言模拟‘ls‘命令
12 1
|
1月前
|
Linux C语言
【Linux】C语言动态库和静态库的制作和使用
【Linux】C语言动态库和静态库的制作和使用
|
1月前
|
Linux 测试技术 C语言
【Linux】应用编程之C语言文件操作
【Linux】应用编程之C语言文件操作
|
2月前
|
前端开发 Unix Linux
Linux indent命令 (格式化C语言源代码的程序)
Linux indent命令 (格式化C语言源代码的程序)
21 0
Linux indent命令 (格式化C语言源代码的程序)