Undefined references when building with files generated from wayland-protocols
I am trying to solve undefined references with RetroArch when built with CXX_BUILD=1 which uses -xc++.
/usr/bin/ld: obj-unix/release/gfx/drivers_context/wayland_ctx.o: in function `registry_handle_global(void*, wl_registry*, unsigned int, char const*, unsigned int)':
wayland_ctx.c:(.text+0x1e52): undefined reference to `zxdg_decoration_manager_v1_interface'
/usr/bin/ld: wayland_ctx.c:(.text+0x1f6b): undefined reference to `xdg_wm_base_interface'
/usr/bin/ld: wayland_ctx.c:(.text+0x1fa3): undefined reference to `zxdg_shell_v6_interface'
/usr/bin/ld: wayland_ctx.c:(.text+0x2053): undefined reference to `zwp_idle_inhibit_manager_v1_interface'
collect2: error: ld returned 1 exit status
make: *** [Makefile:196: retroarch] Error 1
These issues occur with files generated using wayland-scanner and wayland-protocols.
For example we use xdg_wm_base_interface here.
And include the generated xdg-shell.h in the same file.
Inside xdg-shell.h:
extern const struct wl_interface xdg_wm_base_interface;
And xdg-shell.c
WL_PRIVATE const struct wl_interface xdg_wm_base_interface = {
"xdg_wm_base", 2,
4, xdg_wm_base_requests,
1, xdg_wm_base_events,
};
I think this explains the problem I am seeing.
In C++, const objects at global scope are by default also static, i.e., they are not visible outside the source file. To fix that, add extern to each of the definitions.
Source: https://stackoverflow.com/questions/34185401/extern-variable-undefined/34185641#34185641
I have tried adding extern in xdg-shell.c where appropriate and I also tried including xdg-shell.h inside xdg-shell.c. Both approaches helped, but I as the files are auto-generated I am not sure how to solve this or where.
Any suggestions?