dlsym/dlopen with runtime arguments

前端 未结 3 1696
挽巷
挽巷 2020-12-28 23:43

I am trying to do something like the following

  enum types {None, Bool, Short, Char, Integer, Double, Long, Ptr};
  int main(int argc, char ** args) {
              


        
3条回答
  •  半阙折子戏
    2020-12-29 00:29

    The Proper Solution

    Assuming you're writing the shared libraries; the best solution I've found to this problem is strictly defining and controlling what functions are dynamically linked by:

    1. Setting all symbols hidden
      • for example clang -dynamiclib Person.c -fvisibility=hidden -o libPerson.dylib when compiling with clang
    2. Then using __attribute__((visibility("default"))) and extern "C" to selectively unhide and include functions
    3. Profit! You know what the function's signature is. You wrote it!

    I found this in Apple's Dynamic Library Design Guidelines. These docs also include other solutions to the problem above was just my favorite.

    The Answer to your Question

    As stated in previous answers, C and C++ functions with extern "C" in their definition aren't mangled so the function's symbols simply don't include the full function signature. If you're compiling with C++ without extern "C" however functions are mangled so you could demangle them to get the full function's signature (with a tool like demangler.com or a c++ library). See here for more details on what mangling is.

    Generally speaking it's best to use the first option if you're trying to import functions with dlopen.

提交回复
热议问题