gcc/g++编译过程、system系统调用过程

x33g5p2x  于2022-05-22 转载在 其他  
字(1.4k)|赞(0)|评价(0)|浏览(629)

如图即一个简单的 hello world 程序。

  1. // 1、使用某个函数前,需要包含相应的头文件
  2. // 2、可以通过man手册查询或者其他资料查询
  3. // 3、头文件类似于菜单,头文件包含函数的声明,相当于菜单例举了菜名,函数调用,相当于点菜
  4. // 4、<>通过包含系统的头文件(标准的头文件),""包含自定义的头文件
  5. #include <stdio.h>
  6. // 1、C语言由函数组成,有且仅有一个主函数
  7. // 2、程序运行,先从main函数运行
  8. // 3、return 0,程序正常结束
  9. int main()
  10. {
  11. // 注释:不是有效代码
  12. // 1、行注释, //相应的注释
  13. // 2、块注释,/* 相应的注释 */
  14. printf("hello world\n");
  15. // 1、这是一个C代码
  16. // 2、函数调用,printf功能往标准输出设备(屏幕)上打印内容
  17. // 3、\n代表换行
  18. return 0;
  19. }

头文件目录:vi /usr/include/stdio.h。

一、system系统调用

system函数:

  1. int system(const char *command);

1.实例1:01_test.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. printf("before sys\n");
  6. // 1、需要头文件 #include <stdlib.h>
  7. // 2、system功能:调用外部程序
  8. system("ls -alh");
  9. printf("after sys\n");
  10. return 0;
  11. }

2.实例2:02_waibu.c

  1. #include <stdio.h>
  2. int main()
  3. {
  4. printf("我是小鲜肉,假的\n");
  5. printf("我是外部程序\n");
  6. return 0;
  7. }

3.实例3:03_system.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. printf("before sys\n");
  6. // 1、需要头文件 #include <stdlib.h>
  7. // 2、system功能:调用外部程序
  8. system("./waibu");
  9. printf("after sys\n");
  10. return 0;
  11. }

4.实例4:calc 计算器

vim:

vscode:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5. printf("before sys\n");
  6. system("calc");
  7. printf("after sys\n");
  8. return 0;
  9. }

在Linux下无效:

只在Windows下有效:

二、gcc/g++编译

C程序编译步骤:

1.预处理:gcc -E hello.c -o hello.i

2.编译: gcc -S hello.i -o hello.s

3.汇编: gcc -c hello.s -o hello.o

4.链接: gcc hello.o -o hello

5.运行

Linux查看需要链接的动态库:ldd。

相关文章