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

[config] Defining custom allocator macros in src/config.h results in: warning: "RL_*ALLOC" redefined #4778

Closed
sleeptightAnsiC opened this issue Feb 18, 2025 · 3 comments

Comments

@sleeptightAnsiC
Copy link
Contributor

sleeptightAnsiC commented Feb 18, 2025

While working on other issue ( #4777 / #4776 ) I noticed that src/raylib.h defines "RL_*ALLOC" macros before including src/config.h (raylib.h does not include config.h at all). If someone tries to define said macros inside config.h (which seems like very reasonable place) it will result in compiler warnings about that said macros were re-defined. This may also indicate that a mismatch happens somewhere during preprocessing stage.

Repro:

with the patch below applied to config.h

diff --git a/src/config.h b/src/config.h
index f7b01530..fd108d9a 100644
--- a/src/config.h
+++ b/src/config.h
@@ -28,6 +28,12 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 
+#include <stdlib.h> // puts
+#include <stdio.h>  // malloc realloc free
+#define RL_MALLOC(...) (puts(__func__), malloc(__VA_ARGS__))
+#define RL_REALLOC(...) (puts(__func__), realloc(__VA_ARGS__))
+#define RL_FREE(...) (puts(__func__), free(__VA_ARGS__))
+
 //------------------------------------------------------------------------------------
 // Module selection - Some modules could be avoided
 // Mandatory modules: rcore, rlgl, utils

try to compile with Cmake or GNU Make

$ make
gcc -c rcore.c -Wall -D_GNU_SOURCE -DPLATFORM_DESKTOP_GLFW -DGRAPHICS_API_OPENGL_33 -Wno-missing-braces -Werror=pointer-arith -fno-strict-aliasing -std=c99 -fPIC -O1 -Werror=implicit-function-declaration -D_GLFW_X11  -I.  -Iexternal/glfw/include
In file included from rcore.c:109:
config.h:33:9: warning: "RL_MALLOC" redefined
   33 | #define RL_MALLOC(...) (puts(__func__), malloc(__VA_ARGS__))
      |         ^~~~~~~~~
In file included from rcore.c:105:
raylib.h:138:13: note: this is the location of the previous definition
  138 |     #define RL_MALLOC(sz)       malloc(sz)
      |             ^~~~~~~~~
config.h:34:9: warning: "RL_REALLOC" redefined
   34 | #define RL_REALLOC(...) (puts(__func__), realloc(__VA_ARGS__))
      |         ^~~~~~~~~~
raylib.h:144:13: note: this is the location of the previous definition
  144 |     #define RL_REALLOC(ptr,sz)  realloc(ptr,sz)
      |             ^~~~~~~~~~
config.h:35:9: warning: "RL_FREE" redefined
   35 | #define RL_FREE(...) (puts(__func__), free(__VA_ARGS__))
      |         ^~~~~~~
raylib.h:147:13: note: this is the location of the previous definition
  147 |     #define RL_FREE(ptr)        free(ptr)
      |             ^~~~~~~

(this is x86_64 Linux with raylib pulled from git)

$ uname -a
Linux MAL200424 6.13.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 08 Feb 2025 18:54:55 +0000 x86_64 GNU/Linux
@sleeptightAnsiC
Copy link
Contributor Author

sleeptightAnsiC commented Feb 18, 2025

So I tried to fix this by including config.h on the very top of raylib.h

diff --git a/src/raylib.h b/src/raylib.h
index 7919db77..1e8aefbc 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -87,6 +87,11 @@
 
 #include <stdarg.h>     // Required for: va_list - Only used by TraceLogCallback
 
+// Check if config flags have been externally provided on compilation line
+#if !defined(EXTERNAL_CONFIG_FLAGS)
+    #include "config.h"     // Defines module configuration flags
+#endif
+
 #define RAYLIB_VERSION_MAJOR 5
 #define RAYLIB_VERSION_MINOR 6
 #define RAYLIB_VERSION_PATCH 0

warnings disappear but one of the examples breaks

$ mkdir build && cd build && cmake .. && make

[ 75%] Building C object examples/CMakeFiles/shapes_rectangle_advanced.dir/shapes/shapes_rectangle_advanced.c.o
/home/korn/raylib/examples/shapes/shapes_rectangle_advanced.c: In function ‘DrawRectangleRoundedGradientH’:
/home/korn/raylib/examples/shapes/shapes_rectangle_advanced.c:165:42: error: ‘texShapes’ undeclared (first use in this function)
  165 |                 rlTexCoord2f(shapeRect.x/texShapes.width, shapeRect.y/texShapes.height);
      |                                          ^~~~~~~~~
/home/korn/raylib/examples/shapes/shapes_rectangle_advanced.c:165:42: note: each undeclared identifier is reported only once for each function it appears in

@raysan5
Copy link
Owner

raysan5 commented Feb 19, 2025

@sleeptightAnsiC I prefer to avoid including config.h in raylib.h and including C libraries in config.h.

@raysan5 raysan5 closed this as completed Feb 19, 2025
@sleeptightAnsiC
Copy link
Contributor Author

sleeptightAnsiC commented Feb 19, 2025

@raysan5 What is the preferred way of defining those macros? In another file included with -include or -imacros ?

All raylib's sources that include raylib.h also seem to include config.h right afterwards.

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

2 participants