There cannot possibly be a way of getting the LibraryFunction name.
fun1 = LibraryFunctionLoad[...]
fun2 = fun1
What is the correct name now? fun1
or fun2
?
I suggest handling high-level things like issuing messages with the correct head in Mathematica code, not in C. But even in Mathematica code, the same problem persists. Suppose we have a function that calls a subroutine that is used by multiple user-facing functions and can fail. How does this subroutine know which head to attach the message to? It does not which function it was called by.
There are multiple solutions to this problem, but none that I know is perfect. One is to Throw
a Failure
-like object, catch it in the topmost function, attach the correct head to the message there, and issue it. A more common one is to pass down the correct head to the subroutine as an extra argument. You can use this with LibraryLink too.
What you could also do in LibraryLink if you really want to use the correct message is this:
When a failure occurs in your function, save the text of the corresponding message to a global variable and return with an error code. The LibraryFunction will them return LibraryFunctionError
with the same code. On the Mathematica side, detect this, and retrieve the message text (or list of message texts) through another library function. Then finally issue the Message
with the correct text within Mathematica.
This would work for error messages that are issued only when your function fails, and needs to exit immediately. It would not work if your function wants to continue after it issued the message.