zoukankan      html  css  js  c++  java
  • 【C】Re01

    一、GCC分步编译

    xxx.c文件经历的一系列编译过程:

    #include <stdio.h>
    
    int main() {
        printf("Hello, World!
    ");
        return 0;
    }
    
    /*
     * gcc 
     *
     * GCC的分布编译
     * xxx.c文件 -> xxx.exe可执行文件
     * 1、预处理 ------------------------------------------------------
     *  - hello.c文件 生成hello.i文件,需要指令 -E
     *  - hello.i 也是一个c程序
     *  - 预处理阶段展开头文件,宏替换,条件编译,取消注释
     *
     *  命令
     *  gcc 文件名 -E
     *  gcc 文件名.c -E -o 文件名.i
     *
     *  导入的头文件在预编译中展开,文件大小变化
     *
     * 2、编译 ------------------------------------------------------
     *  - xxx.i 文件 编译生成 xxx.s 文件 指令 -S
     *  - xxx.s 文件是一个汇编文件,
     *  - 编译阶段主要是语法的检测
     *  - gcc 文件名.i -S -o 文件名.s
     * 3、汇编 ------------------------------------------------------
     *  - xxx.s 汇编文件 生成 hello.o 文件需要指令 -c
     *  - xxx.o 是一个二进制文件
     *  - 汇编阶段主要是生成让机器识别的二进制文件
     *
     * 4、链接 ------------------------------------------------------
     *  - 将hello.o 生成 hello.exe 可执行文件
     *  - 可以在终端中执行此文件
     *      gcc 文件名.o -o 文件名.exe
     *
     *  指令参数
     *  -E -> -S -> -c
     *  xxx.c
     *  -> xxx.i 预处理后的源码文件
     *  -> xxx.s 编译后的汇编文件
     *  -> xxx.o 汇编编译后的二进制文件
     *  -> xxx.exe 可执行文件
     *
     * */
    

    二、System函数

    1、CMD终端命令:

    mspaint 画图
    notepad 记事本
    calc 计算器
    control 控制面板
    wmic memphysical get maxcapacity 查看最大内存容量

    2、System函数

    System函数可以让C语言程序执行操作系统命令

    也就是将命令以字符串的形式传入执行

    该函数需要包含标准库头文件 #include <stdlib.h> 

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        /*printf("Hello, World!
    ");*/
        system("gcc -v");
        return 0;
    }

    3、Pause暂停命令

    system("pause");

    可用于暂停程序,防止程序运行过快窗口结束

    三、自定义函数与调用

    #include <stdio.h>
    #include <stdlib.h>
    
    void myFunction() {
        printf("my custom function 
    我的自定义函数 
    ");
    }
    
    int main() {
        myFunction();
        return 0;
    }

    四、C4996错误问题

    #include <stdio.h>
    #include <stdlib.h>
    
    void myFunction() {
        printf("my custom function 
    我的自定义函数 
    ");
    }
    
    void errorC4996() {
        char buf[1024] = {0};
        sprintf(buf, "%s", "aaa");
        printf("%s", buf);
        system("pause");
    }
    
    int main() {
        errorC4996();
        return 0;
    }

    老版本的函数存在内存安全隐患

    解决方案1:使用加上后缀了_s的安全化的函数进行替代

    #include <stdio.h>
    #include <stdlib.h>
    //#define _CRT_SECURE_NO_WARNINGS
    
    void myFunction() {
        printf("my custom function 
    我的自定义函数 
    ");
    }
    
    void errorC4996() {
        char buf[1024] = {0};
        sprintf_s(buf, "%s", "aaa");
        printf_s("%s", buf);
        system("pause");
    }
    
    int main() {
        errorC4996();
        return 0;
    }

    解决方案2:或者是包含一个宏

    #include <stdio.h>
    #include <stdlib.h>
    #define _CRT_SECURE_NO_WARNINGS
    void myFunction() {
        printf("my custom function 
    我的自定义函数 
    ");
    }
    
    void errorC4996() {
        char buf[1024] = {0};
        sprintf(buf, "%s", "aaa");
        printf("%s", buf);
        system("pause");
    }
    
    int main() {
        errorC4996();
        return 0;
    }

    解决方式3:使用禁用警告

    #include <stdio.h>
    #include <stdlib.h>
    //#define _CRT_SECURE_NO_WARNINGS
    #pragma warnings(disable:4996)
    
    void myFunction() {
        printf("my custom function 
    我的自定义函数 
    ");
    }
    
    void errorC4996() {
        char buf[1024] = {0};
        sprintf(buf, "%s", "aaa");
        printf("%s", buf);
        system("pause");
    }
    
    int main() {
        errorC4996();
        return 0;
    }
  • 相关阅读:
    《Linux/Unix设计思想》随笔 ——Linux/Unix哲学概述
    C99 inline关键字
    关于C++内存对齐
    HLSL中constant variables的packing规则
    全局照明算法基础——从辐射亮度到渲染方程
    透视投影矩阵推导
    基于光线追踪的渲染中景深(Depth of field)效果的实现
    直线的光栅化算法
    透视投影后的线性插值校正
    linux内核编译与开发
  • 原文地址:https://www.cnblogs.com/mindzone/p/13930351.html
Copyright © 2011-2022 走看看