# Error message LNK2019, unresolved external symbol _output referenced in function __tr0

I've successfully compiled the simple addtwo function predefined in Mathematica as per: MathLink dependencies: "LNK2019: unresolved external symbol _MLMain" I am now attempting to write an external function which calls a class I wrote earlier and returns a list of values using an output file called output.tm. The output file is as follows:

void output P((double, double, double, double, double, double, double, double, int, int));

:Begin:
:Function:       output
:Pattern:        output[x0_Real64, t0_Real64, tend_Real64, deltat_Real64, D_Real64, p_Real64, q_Real64, finallevel_Real64, nDIM_Integer32, iterations_Integer32]
:Arguments:      { x0, t0, tend, deltat, D, p, q, finallevel, nDIM, iterations }
:ArgumentTypes:  { Real64, Real64, Real64, Real64, Real64, Real64, Real64, Real64, Integer32, Integer32 }
:ReturnType:     Manual
:End:


The relevant part of my c++ code is given here:

#include "mathlink.h"
using namespace std;
extern void output(MLINK mlp, double x0, double t0, double tend, double deltat, double D, double p, double q, double finallevel, int nDIM, int iterations);

class Solver()
{
etc. etc.
}
void output(MLINK mlp, double x0, double t0, double tend, double deltat, double D, double p, double q, double finallevel, int nDIM, int iterations)
{
Solver s(x0, D, t0, tend, deltat, finallevel, p, q, nDIM);

s.PropagationDelayDistributionSolver(iterations);

MLPutReal64List(mlp, s.returntau(), iterations);
}


Where s.returntau returns an array of doubles from the class (again, the details aren't important). When I compile this code, I only get one error:

Error 2 $\quad \quad$ error LNK2019: unresolved external symbol _output referenced in function __tr0 C:\Users\SOM874\Documents\Visual Studio 2010\Projects\PDS5\PDS5\output.obj $\quad \quad$ PDS5

I don't see anything wrong when I look at the output.c file but I am a novice, I wouldn't know what to look for. Is there anything wrong which jumps out at you? Thanks in advance.

• There are several problems here: 1. the :Pattern: part of the template must be a Mathematica pattern that will match any call of the function from within Mathematica. There's no such thing as Real64 in Mathematica. What you specify in :ArgumentTypes: a completely different thing, those are not patterns. The possible values for that are documented here and appear to be correct. – Szabolcs Feb 10 '15 at 20:31
• The second problem is what sakra has just posted so I won't repeat that. – Szabolcs Feb 10 '15 at 20:31
• And one more thing: in Mathematica, avoid names starting with capitals to make sure there's no collision with builtins. Specifically: do not use D. It has a builtin meaning. Use something else with lowercase, like dd. – Szabolcs Feb 10 '15 at 20:36

I guess that this is a linkage issue. The generated output.tm.c file is compiled a C code whereas "your code" is compiled as C++ code. Thus the function output will have C++ linkage while the function __tr0 expects the function to have C linkage. In your C++ code, declare the function with C linkage using extern "C", i.e.:
#include "mathlink.h"

• No. The extern "C" makes the function output callable from both C and C++ code. In its implementation you are free to use C++ language features. – sakra Feb 11 '15 at 13:39