Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix / namespace suggestion #12

Open
rboati opened this issue Nov 9, 2024 · 1 comment
Open

Prefix / namespace suggestion #12

rboati opened this issue Nov 9, 2024 · 1 comment

Comments

@rboati
Copy link

rboati commented Nov 9, 2024

I would suggest to change the actual prefix system with something more flexible.
I really like the one header idea but current implementation doesn't allow to choose a comfortable prefix and avoid collisions at the same time.
In contrast, for example, consider the following main.c:

#include <stdio.h>

#define MYLIB_H_IMPLEMENTATION
#define MYLIB_H_PREFIX a_
// For no prefix just #define MYLIB_H_PREFIX
// For default prefix no action is required
#include "mylib.h"


#define ANOTHERLIB_H_IMPLEMENTATION
#define ANOTHERLIB_H_NAME(x) x ## _from_anotherlib
#include "anotherlib.h"


int main(int argc, char *argv[]) {
	a_hello();
	hello_from_anotherlib();
	return 0;
}

Both headers export a hello function.
For the first #include there is a simpler version in which the user choose the prefix it would like to use for the module symbols.
For the second #include there is a slightly more advanced version in which the user defines the naming macro to use for the module symbols.
The headers could be implemented as follows:
mylib.h

#ifndef MYLIB_H_GUARD
#define MYLIB_H_GUARD

#ifndef MYLIB_H_PREFIX
#define MYLIB_H_PREFIX mylib_
#endif // MYLIB_H_PREFIX

#ifndef MYLIB_H_NAME
#define MYLIB_H_CONCAT_HELPER(x, y) x ## y
#define MYLIB_H_CONCAT(x, y) MYLIB_H_CONCAT_HELPER(x, y)
#define MYLIB_H_NAME(x) MYLIB_H_CONCAT(MYLIB_H_PREFIX, x)
#endif // MYLIB_H_NAME

#define mylib_hello MYLIB_H_NAME(hello)

void mylib_hello();

#ifdef MYLIB_H_IMPLEMENTATION
#include <stdio.h>

void mylib_hello() {
	printf("Hello, World from mylib!\n");
}

#endif // MYLIB_H_IMPLEMENTATION

// cleanup macros
#undef mylib_hello

#undef MYLIB_H_CONCAT_HELPER
#undef MYLIB_H_CONCAT
#undef MYLIB_H_NAME
#undef MYLIB_H_IMPLEMENTATION

#endif // MYLIB_H_GUARD

You get the idea, anyway:
anotherlib.h

#ifndef ANOTHERLIB_H_GUARD
#define ANOTHERLIB_H_GUARD

#ifndef ANOTHERLIB_H_NAME
#define ANOTHERLIB_H_NAME(x) anotherlib_ ## x
#endif // ANOTHERLIB_H_NAME

#define anotherlib_hello ANOTHERLIB_H_NAME(hello)

void anotherlib_hello();

#ifdef ANOTHERLIB_H_IMPLEMENTATION
#include <stdio.h>

void anotherlib_hello() {
	printf("Hello, World from anotherlib!\n");
}

#endif // ANOTHERLIB_H_IMPLEMENTATION

// cleanup macros
#undef anotherlib_hello

#undef ANOTHERLIB_H_NAME
#undef ANOTHERLIB_H_IMPLEMENTATION

#endif // ANOTHERLIB_H_GUARD

I already tried this implementation and it works as intended.

@rboati
Copy link
Author

rboati commented Nov 11, 2024

Unfortunately it doesn't work for macros as they cannot have a dynamic name and the user needs to define aliases manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant