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

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\");
lib = CreateLibrary[src, "mylib"];
myFunc = LibraryFunctionLoad[lib, "myFunc", {}, Real];

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

LibraryFunction::Here's my message: -- Message text not found -- >>


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?

  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Jul 9, 2015 at 11:37

2 Answers 2


You have not defined any message text in Mathematica.

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


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

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

The relevant documentation page is here.

  • 2
    $\begingroup$ 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? $\endgroup$
    – Ruslan
    Commented Jun 7, 2015 at 16:20
  • $\begingroup$ @Ruslan, I'm not sure. I've never tried to send arguments with the message. $\endgroup$ Commented Jun 7, 2015 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;
MLINK link = libData->getMathLink(libData);
MLPutFunction(link, "EvaluatePacket", 1);
    MLPutFunction(link, "Message", 2);
        MLPutFunction(link, "MessageName", 2);
            MLPutSymbol(link, "MyFunction");
            MLPutString(link, "info");
        MLPutString(link, msg);
pkt = MLNextPacket(link);
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.

  • $\begingroup$ 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? $\endgroup$
    – Ruslan
    Commented Aug 13, 2015 at 14:29
  • $\begingroup$ @Ruslan It works in v9 too. You need to #include "MathLink.h". $\endgroup$
    – Szabolcs
    Commented Aug 13, 2015 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.