Modularization#

modular programming

a programming paradigm that emphasizes organizing the functions of a codebase into independent modules – each providing an aspect of a computer program without providing other aspects.

Defining functions#

int square(int n);  // Function prototype (typically in .h files)

int square(int n) {  // Function definition (typically in .c files)
  return n * n;
}

Managing multiple modules in C#

πŸ“ include/   <= How is a module used?
  πŸ“„ lib1.h
  πŸ“„ lib2.h
πŸ“ src/       <= Implementations
  πŸ“„ lib1.c
  πŸ“„ lib2.c
  πŸ“„ main.c

VS Code considerations#

You have to modify:

  1. compile_flags.txt, so that language server can see the header files:

    -std=gnu2y
    -g
    -Iinclude
    
  2. tasks.json, e.g.,:

    "command": "clang",
    "args": [
        "'@compile_flags.txt'",
        "./src/main.c",
        "./src/lib1.c",
        "./src/lib2.c",
        "-o",
        "main"
    ],
    

    Alternatively we can use a makefile and use make as the command.