I am trying to link my C++ program to Mathematica Kernel. I am trying to use the MLEvaluate() evaluate but I receive an error while compilation. My codes are as below:

#include <mathlink.h> //mathlink header
#include <stdio.h> //standard io header
#include <iostream>
using namespace std;

/* initialising variables */
MLENV env = (MLENV)0;
MLINK link = (MLINK)0;

int main(int argc, char *argv[]) {
    int errno;
    int packet;
    int input;

    /* initialises MathLink environemnt object */
    env = MLInitialize(0);

    /* links the program to the MathKernel */
    link = MLOpenString(env, "-linkmode launch -linkname 'C:\\Program Files\\Wolfram Research\\Mathematica\\9.0\\math.exe'", &errno);


    /* get packets until we find a ReturnPacket or error */
    while ((packet = MLNextPacket(link)) && packet != RETURNPKT) MLNewPacket(link);

    /* once the results is returned */
    if (MLError(link)) printf("\nError Has Occured!\n");    //if an error is returned
    else {                                                      //if an integer is returned
        int result;
        MLGetInteger(link, &result); //we know that the result is an integer in this case
        printf("\nResult: %d\n", result);

    printf("\nPress Enter to Exit...");
    getchar(); //holds the console till users presses enter
    getchar(); //holds the console till users presses enter
    return 0;


the error is get however is:

Error   1   error LNK2019: unresolved external symbol _MLEvaluate referenced in function _main  C:\Users\arvindrajan\Documents\Visual Studio 2013\Projects\MathematicaConsoleApplication\MathematicaConsoleApplication\main.obj MathematicaConsoleApplication

any advise?


  • $\begingroup$ The documentation says, "MLEvaluate() is only available for use in mprep MathLink template programs.". It sounds like you are calling Mathematica from a C program, not the reverse, so you are not using a template. This would mean you can't use MLEvaluate(). $\endgroup$ – Szabolcs Mar 6 '14 at 20:07

This is a linking error, not a compilation error.

It occurs because the linker can't find MLEvaluate(): the documentation states that

MLEvaluate() is only available for use in mprep MathLink template programs.

That's because this function is defined in the C source output by mprep when processing a template files. Template files apply only when you create an installable MathLink program that's used from within Mathematica, not when you have a standalone C program that uses Mathematica.

To sum up, you'll need to implement this functionality yourself, without MLEvaluate(). As a starting point, look at the source output by mprep for MLEvaluate:

int MLEvaluate( MLINK mlp, char* s)
    if( MLAbort) return 0;
    return MLPutFunction( mlp, "EvaluatePacket", 1L)
        && MLPutFunction( mlp, "ToExpression", 1L)
        && MLPutString( mlp, s)
        && MLEndPacket( mlp);
} /* MLEvaluate */
| improve this answer | |
  • $\begingroup$ Thank you very much. the MLAbort now gives the same problem: Error 2 error LNK2001: unresolved external symbol _MLAbort C:\Users\arvindrajan\Documents\Visual Studio 2013\Projects\MathematicaConsoleApplication\MathematicaConsoleApplication\main.obj MathematicaConsoleApplication without MLAbort, it works perfectly fine, just that there is no safety feature $\endgroup$ – arvindrajan92 Mar 7 '14 at 2:25
  • $\begingroup$ @arvindrajan92 It seems MLAbort is also something you can only use in installable MathLink programs. I don't have a lot of experience calling Mathematica from standalone C programs. I've only ever written installable MathLink programs, relying on templates, for serious projects. So I'd also have to look up the details. If you're writing a standalone program, it will be helpful to first get familiar with template-based ones and look at the code mprep generates. You can check when and why the variable MLAbort is set there. $\endgroup$ – Szabolcs Mar 7 '14 at 15:41
  • $\begingroup$ It is the urgent message handler that it setting it when it receives an MLAbortMessage. I think this functionality won't be necessary at all if your aim is to call Mathematica from a C program, but not call your C program from Mathematica. The purpose if this functionality is to allow your C functions, called through MathLink, to be aborted when you try to interrupt a calculation with Alt-. from within Mathematica. $\endgroup$ – Szabolcs Mar 7 '14 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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