-1

I'm using a shared library utility that looks up symbols from a shared library (on non-windows platforms using GetProcAddress). This works for normal functions. However, I need a function that is a template instantiation in a namespace. I have confirmed using nm -gDC lib.so that the library contains the symbol and spelled it exactly the same in my lookup attempt but it can't be found.
nm -gDC lib.so

...
0000000000009575 T rosidl_service_type_support_t const* rosidl_typesupport_cpp::get_service_type_support_handle<example_interfaces::srv::AddTwoInts>()
...

I've tried to look up:

GetProcAddress((HINSTANCE)(lib), "rosidl_typesupport_cpp::get_service_type_support_handle<example_interfaces::srv::AddTwoInts>");

but it returns a nullptr.
Is there some special handling for template instantiations that I couldn't find?
I've found a different method that returns me what I need but I'd still be interested in any resources on why that didn't work!

Stefan Fabian
  • 498
  • 4
  • 21
  • 2
    Remove `-C` option. You want mangled name, that's the name `GetProcAddress` expects. – Igor Tandetnik May 28 '21 at 23:02
  • Ah, thank you that makes sense. The name mangling depends on the compiler, though, right? So this wouldn't be compiler portable let alone platform-independent. – Stefan Fabian May 29 '21 at 11:08
  • You can't expect a platform-independent binary; that `lib.so` certainly won't work on Windows, or Mac, or Android phone. And yes, name mangling schemes are often compiler-specific; if you want somewhat portable names exported from your library, stick to plain C functions. – Igor Tandetnik May 29 '21 at 12:24

1 Answers1

0

I have confirmed using nm -gDC lib.so that the library contains the symbol and spelled it exactly the same in my lookup attempt but it can't be found.

As Igor Tandetnik correctly commented, the name the library actually exports is the C++ mangled name, and not rosidl_typesupport_cpp::get_service_type_support_handle<example_interfaces::srv::AddTwoInts> (which is the demangled name).

To see the actual symbol name, use nm -D lib.so (in particular, omit the -C flag).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362