# How to properly generate messages from LibraryLink function?

I'm currently using WolframLibraryData::Message to generate messages from a library function, like this:

Needs["CCompilerDriver"]
src = "
#include \"WolframLibrary.h\"
DLLEXPORT mint WolframLibrary_getVersion() {return WolframLibraryVersion;}
DLLEXPORT int WolframLibrary_initialize(WolframLibraryData libData) {return 0;}
DLLEXPORT void WolframLibrary_uninitialize(WolframLibraryData libData) {}

DLLEXPORT myFunc(WolframLibraryData libData, mint argc, MArgument* args, MArgument result)
{
libData->Message(\"Here's my message\");
MArgument_setReal(result,1.1);
return LIBRARY_NO_ERROR;
}
";
lib = CreateLibrary[src, "mylib"];
myFunc = LibraryFunctionLoad[lib, "myFunc", {}, Real];


Now the problem can be seen if myFunc[] is called. I get this result:

1.1

The problem is this -- Message text not found -- part. Apparently I'm generating the message in a wrong way. So how should I do instead? How do I fill this "message text" to make it look like messages from normal Mathematica functions?

• Good question. This has been bothering me too. Theoretically you can call back to the kernel through MathLink to get more flexibility in how to issue messages than what Message() allows for. Setting up a framework to make this easy is going to take a bit of work. In practical situations the approach I'm taking is to leave this sort of thing (checking errors, issuing user-friendly messages, etc.) to Mathematica code--not C code--as much as possible. – Szabolcs Jul 9 '15 at 11:37

You have not defined any message text in Mathematica.

The text you supply in the C code is the message tag, e.g

libData->Message("myerror");


Then you need to define the actual message content in Mathematica:

LibraryFunction::myerror = "Here's my message"


The relevant documentation page is here.

• How does this extend to messages with arguments, like e.g. NIntegrate::inumr, which looks like The integrand 1 has evaluated to...? I.e. how do I pass these arguments from the function? – Ruslan Jun 7 '15 at 16:20
• @Ruslan, I'm not sure. I've never tried to send arguments with the message. – Simon Woods Jun 7 '15 at 16:27

The simple way is what Simon described in his answer.

A more flexible way is described under Callback Evaluations in the LibraryLink User's Guide.

Note that in the version 10.0-10.2 documentation there's an error: getWSTP should be getWSLINK. You can also use the old (v9) M-prefix function names instead of the WS-prefix ones. For completeness, I'll reproduce the piece of code from there:

char *msg; // this should contain your message text
int pkt;
if (pkt == RETURNPKT)


This will simply evaluate Message[MyFunction::info, msg]. On the Mathematica side you'll want to define something like

MyFunction::info = "Message from LibraryLink: "


You need to #include "MathLink.h" for this to work. This header is located somewhere under \$InstallationDirectory/SystemFiles/Links/MathLink.

Update: I ran into problems with this when aborting functions. I don't quite understand what is happening on the MathLink connection during an abort, so for now I simply use if (libData->AbortQ()) return; before the above.

• Does this only work with version 10? I get a compilation error RETURNPKT undeclared. Could you give a working executable code for Mathematica to make the usage clear? – Ruslan Aug 13 '15 at 14:29
• @Ruslan It works in v9 too. You need to #include "MathLink.h"`. – Szabolcs Aug 13 '15 at 16:06