-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Config.h] Make all the defines in config.h be override-able by the build system #4554
base: master
Are you sure you want to change the base?
Conversation
Are you sure this is enough? Anything you pass on the command line will still get redefined in config.h itself. |
Yes because config.h now checks if the label is not already defined first before defining it, so things on the command line will be defined before the preprocessor runs and config.h will not replace them. This ensures that all things are still defined. I have tested this in msvc and get expected results, I can build raylib with a define in the build system only to disable screenshots. Please test it in other systems. What this won't do is disable things at runtime, since it's all still baked into the compile with preprocessor directives. |
I missed the change to config.h because GitHub collapsed it, sorry! I will test this with gcc/make on Linux later. |
I tested this with: |
@JeffM2501 I see the usefulness of this big change but trying to understand what problem it is currently addressing. As per the issue description:
Yeah, that's right but, afaik, there have been no real use-case issues related since I don't know... I got the feeling this redesign addresses a potential issue that nobody really had... at least yet. |
I am trying to make a game where I allow the user to map items to F12, I can not because the built in screen capture and recording is using that key. That is my real world case. |
You can still overwrite config.h and it will be the same as before. This just means you do not have to if you want another option. You are the author of raylib so it's not that big of a deal for you to just make a new config for each project, You always know what is in config.h. But if someone does not want to fork raylib, or does not know everything that is in raylib, that makes it much harder for them to keep raylib up to date. |
@JeffM2501 Thanks for the further explanation, just one last concern, if I define |
It is required that you set the value to 1. |
@JeffM2501 @raysan5 That's not true!!! I just tested myself #include <stdio.h>
int main(void)
{
#if EXAMPLE_OPTION
puts("option enabled");
#else
puts("option disabled");
#endif
}
It doesn't need to be =1 to enable, no compromise necessary |
@Peter0x44 thanks for the test! that's really great to know! Is that behaviour consistent between compilers or it is actually "Undefined Behaviour" and could be managed in other ways by other compilers? I'm trying to find this info but I couldn't... UPDATE: ChatGPT tells me:
|
https://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html
|
**Testing ChatGPT, quite impressive! 😄 ** The choice between checking if a flag is defined ( 1. Checking if a Flag is Defined (
|
Aspect | #if defined(CUSTOM_FLAG) |
#if CUSTOM_FLAG |
---|---|---|
Simplicity | Good for simple on/off toggles | Slightly more complex |
Flexibility | Limited to "defined" vs "not" | Can use numeric values |
Error Handling | No issues if undefined | Requires #ifndef fallback |
Multi-level Config | Not supported | Supported |
Code Clarity | Clearly indicates "defined check" | Explicit about value logic |
Best Practice
-
For simple toggles (e.g., enable/disable a feature):
Use#if defined(CUSTOM_FLAG)
or#ifdef CUSTOM_FLAG
. -
For configurable features (e.g., modes, levels):
Use#if CUSTOM_FLAG
and assign meaningful values during compilation (e.g.,-DFEATURE_LEVEL=2
). -
To ensure robustness:
Combine both patterns by defining defaults for macros in your code to avoid accidental undefined behavior:#ifndef CUSTOM_FLAG #define CUSTOM_FLAG 0 #endif #if CUSTOM_FLAG // Feature is enabled #endif
This way, your code is safer and can handle cases where the macro is not defined.
Per issue #4411 this PR makes all the config defines first check if they have been set by the build system, and doesn't just check for the define, but instead check the value.
This allows the config.h values to be individually set by the build system.