top | item 47021789

(no title)

saidinesh5 | 16 days ago

Let's say you need to use a function like:

    int add(int a, int b){
        // Long logic and then this
        return a+b;
    }
Let's say this is your main.c.

    #include "add.h"

    int main(void) {
      return add(5,6);
    }

The preprocessor just copies the contents of add.h into your main.c whenever you're trying to compile main.c. (let's ignore the concept of precompiled headers for now).

What you can instead do is just put the add function declaration in add.h that just tells the compiler that add function takes two integers and returns an integer.

   int add(int a, int b);
You can then put the add function definition in add.c , compile that to an add.o and link it to your main.o at link time to get your final binary - without having to recompile add.o every time you change your main.c.

Precompiled headers: https://maskray.me/blog/2023-07-16-precompiled-headers

discuss

order

No comments yet.