Yes, you can use dlsym to access global ones (as long as they are exported, not static). The example below is in C ++ and Mac, but obviously C will work fine.
lib.cpp:
extern "C" { int barleyCorn = 12; }
uselib.cpp
#include <dlfcn.h> #include <iostream> using namespace std; main() { void * f = dlopen ("lib.dylib", RTLD_NOW); void * obj = dlsym (f, "barleyCorn"); int * ptr = (int *) obj; cout << *ptr << endl; }
Output:
% ./a.out 12
Emeryberry
source share