Search code examples
c++sdlsdl-2dlopendlsym

Can dlsym commands be generated from a .h file?


ALREADY SOLVED! See the result here.

Not sure if I am asking at the correct place, but does anyone know a correct way to generate dlopen and dlsym commands based off a .h file?

I am trying to dynamically load SDL2 - which is a library written in C - but all methods of extracting the list of functions and their arguments with ctags seem fruitless (having to manually correct argument lists for 240 functions out of 3500 is not fun).

ctags -R -x --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f sdl /usr/include/SDL2/

The results from ctags look like this:

extern DECLSPEC int SDLCALL SDL_SetTextureColorMod (SDL_Texture * texture,
extern DECLSPEC int SDLCALL SDL_SetThreadPriority (SDL_ThreadPriority priority);
extern DECLSPEC void SDLCALL SDL_SetWindowBordered (SDL_Window * window,

This:

ctags -R -x --sort=yes --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ -f sdl /usr/include/SDL2/ | grep "SDL_AddTimer"

Yields this kind of result:

SDL_AddTimer     prototype    93 /usr/include/SDL2/SDL_timer.h extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,

Notice the missing arguments after interval. It's broken.

And I also saw this being a thing, and it's apparently generated automatically, not made by hand.

So... does anyone know of any way to automatically generate something similar? Especially for SDL2?

Or should I just use SDL functions, and manually dlsym them? Don't ask why am I unwilling to just link instead of using dlopen and dlsym.


Solution

  • I solved this problem by employing decltype.

    typedef decltype(SDL_RenderDrawRects)* PF_SDL_RenderDrawRects;
    

    This way, I will NOT need the argument list for the function. The wrappers are found here.