megahomyak / refcnt

1 stars 0 forks source link

Suggestion: turn into a header-only library #1

Open TheEntropyShard opened 6 months ago

TheEntropyShard commented 6 months ago

Hello :D

Maybe turn this into a header-only stb-style library? When you include the header file it behaves like a header file, but if you #define {LIBRARYNAME}_IMPLEMENTATION before including, it will behave like a C file.

megahomyak commented 6 months ago

When you include the header file it behaves like a header file, but if you #define {LIBRARYNAME}_IMPLEMENTATION before including, it will behave like a C file

What do you mean? Sorry, I don't understand

TheEntropyShard commented 6 months ago

So, I mean that you remove the C file and copy-paste implementation right into the header file after declarations inside

#ifdef {LIBRARYNAME}_IMPLEMENTATION
// implementation functions
#endif // {LIBRARYNAME}_IMPLEMENTATION

And when you want to use the library you just do it like this:

#define {LIBRARYNAME}_IMPLEMENTATION
#include "{library}.h"

// use the library
megahomyak commented 6 months ago

Why should I require the user to #define something alongside the #include directive? What's the benefit, when I can use #ifndef-#define-#endif in the header file?

TheEntropyShard commented 6 months ago

It is much more easier to distribute and use the library cause it is just a single text file.

At the moment, if somebody wants to use this library, he will need to compile it first and then link with it, am I right?

megahomyak commented 6 months ago

Yes. If you wanted me to make a single-file library out of it, it's either that I don't understand something or you don't, because these two snippets you provided:

#ifdef {LIBRARYNAME}_IMPLEMENTATION
// implementation functions
#endif // {LIBRARYNAME}_IMPLEMENTATION

and

#define {LIBRARYNAME}_IMPLEMENTATION
#include "{library}.h"

// use the library

are really weird. I don't get it. Wouldn't it be easier to just use

#ifndef {LIBRARYNAME}
#define {LIBRARYNAME}

// implementation goes here

#endif

for the header file and

#include "{library}.h"

for its inclusion? Your way of doing it doesn't even prevent multiple inclusions

TheEntropyShard commented 6 months ago

Your way of doing it doesn't even prevent multiple inclusions

I know, I just didn't add the include guard.

Look at stb_image.h at lines 128-129, and 544-546. The "header" part of the library is contained inside the include guard, and the implementation part is outside of it but behind #ifdef STB_IMAGE_IMPLEMENTATION.

Here is a citation from nothings/stb:

How do I use these libraries?

The idea behind single-header file libraries is that they're easy to distribute and deploy because all the code is contained in a single file. By default, the .h files in here act as their own header files, i.e. they declare the functions contained in the file but don't actually result in any code getting compiled.

So in addition, you should select exactly one C/C++ source file that actually instantiates the code, preferably a file you're not editing frequently. This file should define a specific macro (this is documented per-library) to actually enable the function definitions. For example, to use stb_image, you should have exactly one C/C++ file that doesn't include stb_image.h regularly, but instead does

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

The right macro to define is pointed out right at the top of each of these libraries.

megahomyak commented 6 months ago

Oh, so it's for separation of the definitions and the implementations? Neat! Sorry I didn't understand you before. I will think about it

TheEntropyShard commented 6 months ago

Oh, so it's for separation of the definitions and the implementations?

Yeah, you are right.

Sorry I didn't understand you before.

Please forgive me my inability to properly explain things :D