clinuxshared-librariesdlopen

How to import a shared object symbol that is an array using dlsym


I'm trying to manually import symbols from libncurses.so. This works fine for function pointers and normal variables like stdscr or COLORS. However, ncurses.h also defines a global array named acs_map:

extern NCURSES_EXPORT_VAR(chtype) acs_map[];

How can I import this using dlsym()? My problem here is that acs_map is an array, not a pointer so I'm not sure if it's allowed to do something like this:

chtype **ptr = dlsym(lib, "acs_map");
chtype *acs_map = *ptr;

But I think I have to use a pointer somehow because I can't assign a new value to an array variable, can I? So what's the recommended way to import acs_map using dlsym()?


Solution

  • Since arrays decay to a pointer to their first member, you can use a pointer type to refer to the array object.

    chtype *ptr = dlsym(lib, "acs_map");