作业:GCC

一、学习并掌握可执行程序的编译、组装过程

A. 在Linux系统(Ubuntu)下仿做一遍

  1. 准备过程

    • 创建文件:hello.c, hello.h, main.c

    • 在这里插入图片描述

    • 在这里插入图片描述

    • 在这里插入图片描述

    • 编辑代码并生成 hello.o 文件, check with ls

    • 在这里插入图片描述

  2. 由.o 文件创建静态库

    • Naming convention: libmyhello.a
    • Command: ar -crv libmyhello.a hello.o
    • Check with ls
    • 在这里插入图片描述
  3. 在程序中使用静态库

    • Generate main.o with gcc -c main.c
    • Command: gcc -o hello main.c libmyhello.a
    • Generate executable: ./hello
    • 在这里插入图片描述
  4. 由.o 文件创建动态库文件

    • (1) Create shared library: libmyhello.so

      • 在这里插入图片描述
    • (2) Execute with dynamic library: gcc -o hello main.c -L.

    • 在这里插入图片描述

B. 第一次作业的程序代码改编

  1. 准备过程

    • 创建文件:sub1.c, sub2.c, main1.c
    • Edit code
    • 在这里插入图片描述
    • 在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
  2. 生成.o文件并查看

    • Command: gcc -c sub1.c sub2.c
    • 在这里插入图片描述
  3. 静态库

    • Command: ar crv libsub.a sub1.o sub2.o
    • Linking: gcc -o main main.c libsub.a
    • 在这里插入图片描述
  4. 动态库

    • Command: gcc -shared -fPIC -o libsub.so sub1.o sub2.o
    • Linking: gcc -o main main.c libsub.so
    • Check with ls
    • 在这里插入图片描述
  5. 动态静态库对比

  6. 在这里插入图片描述

二、Gcc不是一个人在战斗

解读GCC编译工具集和EFF文件格式

  • 学习任务:仿做材料“Linux GCC常用命令.pdf”和“GCC编译器背后的故事.pdf”

代码处理过程

  1. 生成.i文件并查看:gcc -E hello.c -o hello.i
    在这里插入图片描述

  2. 预处理:删除宏定义、处理条件预编译指令、插入被包含文件、删除注释、添加行号和文件标识

  3. 编译:gcc -S hello.i -o hello.s
    在这里插入图片描述

三、全局常量、全局变量、局部变量、静态变量、堆、栈等概念

A. 在Ubuntu上进行验证

  1. 编辑main.c文件及代码
    在这里插入图片描述

  2. 执行./main
    在这里插入图片描述

B. STM32中代码演示

#include “stm32f10x.h”
#include “stdio.h”

void func() {
int local_var = 300; // 局部变量

USART_InitTypeDef USART_InitStructure;

// 初始化串口
// ...

while (1) {
    char buffer[64];
    sprintf(buffer, "Local var address in STM32: %p\n", &local_var);
    for (int i = 0; buffer[i] != '\0'; i++) {
        USART_SendData(USART2, buffer[i]);
        while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    }
}

}

int main() {
int global_var = 200; // 全局变量

USART_InitTypeDef USART_InitStructure;

// 初始化串口
// ...

while (1) {
    char buffer[64];
    sprintf(buffer, "Global var address in STM32: %p\n", &global_var);
    for (int i = 0; buffer[i] != '\0'; i++) {
        USART_SendData(USART2, buffer[i]);
        while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    }
    func();
}

}

  • 编译成功,将代码烧录进入芯片,按下reset键,单片机发送数据,串口调试助手结果。
  • 在这里插入图片描述
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值