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

如图即一个简单的 hello world 程序。
在这里插入图片描述

// 1、使用某个函数前,需要包含相应的头文件
// 2、可以通过man手册查询或者其他资料查询
// 3、头文件类似于菜单,头文件包含函数的声明,相当于菜单例举了菜名,函数调用,相当于点菜
// 4、<>通过包含系统的头文件(标准的头文件),""包含自定义的头文件
#include <stdio.h>

// 1、C语言由函数组成,有且仅有一个主函数
// 2、程序运行,先从main函数运行
// 3、return 0,程序正常结束
int main()
{
    
    
    // 注释:不是有效代码
    // 1、行注释, //相应的注释
    // 2、块注释,/* 相应的注释 */
    
    printf("hello world\n");
    // 1、这是一个C代码
    // 2、函数调用,printf功能往标准输出设备(屏幕)上打印内容
    // 3、\n代表换行
    
    return 0;
}

头文件目录:vi /usr/include/stdio.h。
在这里插入图片描述

一、system系统调用

在这里插入图片描述
system函数:

int system(const char *command);

1.实例1:01_test.c

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    printf("before sys\n");

    // 1、需要头文件 #include <stdlib.h>
    // 2、system功能:调用外部程序
    system("ls -alh");

    printf("after sys\n");

    return 0;
}

在这里插入图片描述

2.实例2:02_waibu.c

在这里插入图片描述

#include <stdio.h>

int main()
{
    
    
    printf("我是小鲜肉,假的\n");

    printf("我是外部程序\n");

    return 0;
}

3.实例3:03_system.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    printf("before sys\n");

    // 1、需要头文件 #include <stdlib.h>
    // 2、system功能:调用外部程序
    system("./waibu");

    printf("after sys\n");

    return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.实例4:calc 计算器

vim:
在这里插入图片描述

vscode:
在这里插入图片描述

扫描二维码关注公众号,回复: 14189594 查看本文章
#include <stdio.h>
#include <stdlib.h>

int main()
{
    
    
    printf("before sys\n");

    system("calc");

    printf("after sys\n");

    return 0;
}

在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。
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44918090/article/details/124909904