Skip to content

Commit b5406c8

Browse files
authored
Support clients defining weak symbols of opengx functions (#70)
With this change, the translation unit (TU) of functions.c gets brought into the list of units used in the linking when gc_gl.c's TU is used, as confirmed by nm: functions.o: ... 00000000 B _ogx_functions_c 00000000 T ogx_get_proc_address ... gc_gl.o: ... 00000000 D _ogx_force_proctable U _ogx_functions_c 00000000 T ogx_initialize ... As the comment in the code explains it, this allows us to support the scenario where we use opengx in a client library (such as SDL) without forcing a dependency on it in the client application. More precisely, this change is made to support the case where an application *does use* opengx via libSDL, to make so that the weak symbol for ogx_get_proc_address() defined in libSDL gets overridden by the real one (since the application is unlikely to use this function directly, but it's indirectly used when SDL_GL_GetProcAddress() is called).
1 parent 14a593b commit b5406c8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/functions.c

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef struct {
3838
void *address;
3939
} ProcMap;
4040

41+
int _ogx_functions_c = 0; /* referenced by gc_gl.c, see the comment in there */
42+
4143
#define PROC(name) { #name, name }
4244
static const ProcMap s_proc_map[] = {
4345
//PROC(glAccum),

src/gc_gl.c

+7
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ typedef struct
7979
char _ogx_log_level = 0;
8080
static GXTexObj s_zbuffer_texture;
8181
static uint8_t s_zbuffer_texels[2 * 32] ATTRIBUTE_ALIGN(32);
82+
/* Force the inclusion of functions.c's TU in the build when GL functions are
83+
* used. In this way, if a client library (such as SDL) defines weak symbols
84+
* for the opengx functions it uses, a client application which actually uses
85+
* opengx will link and use its real implementation; at the same time, a client
86+
* which does not use OpenGL is not forced to link with opengx. */
87+
extern int _ogx_functions_c;
88+
void *_ogx_force_proctable = &_ogx_functions_c;
8289

8390
static void draw_arrays_general(DrawMode gxmode, int first, int count, int ne,
8491
int color_provide, int texen);

0 commit comments

Comments
 (0)