嵌入式基础-代码结构规范

QQ群-名称:嵌入式交流 ;群号:933192343;微信号:WangMing_GZ

代码结构规范是程序员的职业素养,良好的代码结构规范不仅可以减少重复造轮子,还能提高个人开发效率。
代码结构规范有以下几个基本要求:
1.主函数只负责创建任务或者监听任务状态
2.每个功能要有独立的编译文件
3.封装好的常用函数要归类到指定的文件夹路径下
4.全局变量的调用要封装成函数

下面的结构图中,黄底文本为文件夹,其他都是文件

  • main.c
  • Application
    • AppShowTime
      • AppShowTime.c
      • AppShowTime.h
  • Common
    • Common.h
    • ComTimeFunc
      • ComTimeFunc.c
      • ComTimeFunc.h

1.主函数规范

主函数用于创建各种线程然后进入睡眠循环,不能在主函数内添加业务逻辑功能

main.c

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include "AppShowTime.h"

unsigned char g_thread_running_flag = 1;

unsigned char Wrap_Get_g_thread_running_flag(void)
{
    return g_thread_running_flag;
}

int main(void)
{
    /*
    创建业务逻辑功能线程,主函数内不做任何处理
    */
    Wrap_App_show_time_init();

    while(g_thread_running_flag)
    {
        sleep(1);
    }

    return 0;
}

2.业务功能函数规范

业务功能函数按照每个功能都有独立的c文件和h文件

AppShowTime.c

#include "AppShowTime.h"
#include "Common.h"
#include "ComTimeFunc.h"

void* App_show_time_thread(void* arg)
{
    char DateStr[DEF_COM_DATE_STRING_LEN];

    while(Wrap_Get_g_thread_running_flag())
    {
        Wrap_Com_time_get_date_string(DateStr);
        printf("time: %s\n",DateStr);
        sleep(1);
    }

    pthread_exit(0);
}

void Wrap_App_show_time_init(void)
{
    pthread_t id;

    pthread_create(&id, NULL,App_show_time_thread, NULL);
    pthread_detach(id);
}

AppShowTime.h

#ifndef _APP_SHOW_TIME_H_
#define _APP_SHOW_TIME_H_

#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

void Wrap_App_show_time_init(void);

#endif

3.封装常用函数规范

常用函数按照功能分类,也要有独立的c文件和h文件;将常用函数统一起来,避免重复造轮子

Common.h

#ifndef _COMMON_H_
#define _COMMON_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <unistd.h>

unsigned char Wrap_Get_g_thread_running_flag(void);

#ifdef __cplusplus
}
#endif

#endif

ComTimeFunc.c

#include "ComTimeFunc.h"

void Wrap_Com_time_get_date_string(char *DateStr)
{
    time_t currTime;
    struct tm *currDate;

    time(&currTime);
    currDate = localtime(&currTime);

    memset(DateStr, 0, DEF_COM_DATE_STRING_LEN);
    strftime(DateStr, DEF_COM_DATE_STRING_LEN, "%Y-%m-%d %H:%M:%S", currDate);
}

ComTimeFunc.h

#ifndef _COM_TIME_FUNC_H_
#define _COM_TIME_FUNC_H_

#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#define DEF_COM_DATE_STRING_LEN             40

void Wrap_Com_time_get_date_string(char *DateStr);

#endif

4.全局变量封装函数规范

全局变量是编程中常用的变量,对于会被其他文件使用的全局变量,可以用函数的方式进行封装,下面的C只用于展示,不需要再次编写到程序里

unsigned char g_thread_running_flag = 1;

unsigned char Wrap_Get_g_thread_running_flag(void)
{
    return g_thread_running_flag;
}

备注

<< readme
如果按照以上结构写了这份代码,可以使用下面命令进行编译和运行编译后的程序
readme

gcc main.c Common/ComTimeFunc/ComTimeFunc.c Application/AppShowTime/AppShowTime.c -ICommon -ICommon/ComTimeFunc -IApplication/AppShowTime -o code_structure -lpthread

./code_structure
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WangMing_GZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值