Skip to content

Template

In all my projects i use c templates to generate new files.
These templates ensure that all files have the same structure. They also enforce basic rules and help to stick to the project coding standard.

Template for c source file

/**
 * @file Template.c
 * @brief Template implementation
 *
 * @copyright Copyright (c) 2021 Tillmann Kaier
 *
 */

/* Includes ------------------------------------------------------------------*/
// Module Interface
#include "Template.h"
// Application
// Common Lib
// BSP
// Std Lib

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private constants ---------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Interface functions -------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

/* END OF FILE ---------------------------------------------------------------*/

The includes should be sorted from local scope to global scope. For the rational behind this see the LLVM coding standard.

Template for c header file

/**
 * @file Template.h
 * @brief Template interface
 * @details
 * detailed interface description
 *
 * @copyright Copyright (c) 2021 Tillmann Kaier
 *
 */
#pragma once // prevent recursive inclusion

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* Includes ------------------------------------------------------------------*/

/* Public defines ------------------------------------------------------------*/

/* Public constants ----------------------------------------------------------*/

/* Public types --------------------------------------------------------------*/

/* Public macro --------------------------------------------------------------*/

/* Public functions ----------------------------------------------------------*/

/* Public Variables ----------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

/* END OF FILE ---------------------------------------------------------------*/

I prefer #pragma once over a #define header guard because the pragma does need less code and it is less error prone.