I am trying to compile the MathLink examples included with Mathematica. I am careful to ensure that the libraries are present in the Visual Studio folder as indicated by the installation manual. I am also careful to ensure that ml32i3m.lib is present in Linker → External Dependencies. The program will not compile for me.

Here is the source code which is already present in Mathematica:

#include "mathlink.h"

extern int addtwo( int i, int j);

int addtwo( int i, int j)
    return i+j;


#if __BORLANDC__
#pragma argsused

int PASCAL WinMain( HINSTANCE hinstCurrent, HINSTANCE hinstPrevious, LPSTR lpszCmdLine, int nCmdShow)
    char  buff[512];
    char FAR * buff_start = buff;
    char FAR * argv[32];
    char FAR * FAR * argv_end = argv + 32;

    hinstPrevious = hinstPrevious; /* suppress warning */

    if( !MLInitializeIcon( hinstCurrent, nCmdShow)) return 1;
    MLScanString( argv, &argv_end, &lpszCmdLine, &buff_start);
    return MLMain( (int)(argv_end - argv), argv);


int main(int argc, char* argv[])
    return MLMain(argc, argv);


I get two errors which are as follows:

Error 2 error LNK2019: unresolved external symbol _MLInitializeIcon referenced in function _WinMain@16  C:\Users\SOM874\Documents\Visual Studio 2010\Projects\addtwo22\addtwo22\addtwo.obj  addtwo22

Error 1 error LNK2019: unresolved external symbol _MLMain referenced in function _WinMain@16    C:\Users\SOM874\Documents\Visual Studio 2010\Projects\addtwo22\addtwo22\addtwo.obj  addtwo22

How can I fix this?

  • $\begingroup$ I'm aware this very question has been asked before in this link: stackoverflow.com/questions/12550223/…. But it was not answered well, the file ml3213m.lib is exactly where it should be, in the lib folder in VC in the Visual Studio folder. If Visual studio cannot find it there it cannot find it anywhere. $\endgroup$
    – Onias
    Commented Feb 10, 2015 at 16:33
  • $\begingroup$ It's not the .lib file that's missing. You need to use mprep.exe to "compile" the addtwo.tm template into a .c file, and compile and link it in the project as well. This is described in the developer guide under "Building WSTP/MathLink programs." I can't help you with doing this specifically in Visual Studio as I don't use that. But when MLMain is missing, it indicates that the template wasn't used (as MLMain is output by mprep, not defined in ml321i3m.lib) $\endgroup$
    – Szabolcs
    Commented Feb 10, 2015 at 16:48
  • $\begingroup$ Additionally, when using v10 you have the choice to use WS prefixed functions and the developer kit in the WSTP directory or ML prefixed ones and the developer kit in the MathLink directory. I mean: do not mix the two. There's no difference between them other than the naming conventions. $\endgroup$
    – Szabolcs
    Commented Feb 10, 2015 at 16:51
  • $\begingroup$ mprep is present in the Visual Studio folder, so I'm going to look through the installer guide to see if there is a way to integrate it with Visual Studio without using the command line. $\endgroup$
    – Onias
    Commented Feb 10, 2015 at 17:35

1 Answer 1


For reference, the developer guide is here for Windows, Mac, Linux. See these pages for detailed instructions on how to compile MathLink programs.

The MLMain() function is defined in the output of the mprep template processor. The error message indicates that the template file wasn't processed or the C code generated by mprep wasn't compiled into the project.

Most MathLink programs, including the examples that come with Mathematica, consist of C source code and a .tm template file that defines how Mathematica function names and arguments should translate to C. This template file needs to be processed by the mprep program (included in the developer kit), which will output C source code. This code must be compiled and linked into the final executable just like all your other C files.

Note 1: In version 10 of Mathematica, MathLink has been renamed to WSTP and all ML prefixed function names were changed to WS prefixed one. There are now two developer kits included: "MathLink" for ML prefixed names and backwards compatibility and "WSTP" for WL prefixed names. To avoid problems, do not mix the two.

There's no functional difference between them. They only differ in naming conventions.

Note 2: Do make sure to link the correct version of the MathLink library.

In the name of ml32i3m.lib, 32 indicates that it's a 32-bit version. Link this if compiling a 32-bit executable. When compiling a 64-bit program, link the 64-bit version.

i3 stands for interface version 3. This was used up to Mathematica 9 and can still be used in version 10, but then you need to define the macro MLINTERFACE 3 when compiling. Otherwise use the i4 version with Mathematica 10.

Information on the differences between versions 3 and 4.

  • $\begingroup$ +1. For those compiling MathLink code on the RPi (B+, not sure if this is true for v2 yet) you must use MLINTERFACE 3. $\endgroup$ Commented Feb 10, 2015 at 17:41
  • $\begingroup$ @bobthechemist Good to know, I haven't tried compiling MathLink programs on the RPi yet. EDIT: But the i4 library is included as well, just checked. $\endgroup$
    – Szabolcs
    Commented Feb 10, 2015 at 17:41
  • $\begingroup$ For Visual Studio users, you have to add the c source file created by mprep using the .tm file into the source files and compile them all at once, otherwise the program will not run. $\endgroup$
    – Onias
    Commented Feb 10, 2015 at 18:07
  • $\begingroup$ The resulting executable is in the Debug folder of your project file. I had to use the command line to generate mprep but someone else might step in and say how they used it in Visual Studio directly, perhaps? $\endgroup$
    – Onias
    Commented Feb 10, 2015 at 18:19
  • $\begingroup$ @Onias Sorry, I can't help with Visual Studio. I always used makefiles, even when using Microsoft's compiler. $\endgroup$
    – Szabolcs
    Commented Feb 10, 2015 at 18:27

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.