I have modified a legacy C program so that it can be called as a function from Mathematica using the WSTP template mechanism. The original C program, however, contained multiple printf statements to report errors and other diagnostic information; these don't appear anywhere, obviously, because they are not talking to the WSTP link. When I replace these p by calls to WSPutString, the diagnostic text does indeed appear in my Notebook, BUT each call to WSPutString pauses execution of the C program and I have to call the function repeatedly from my Notebook, once for each line of diagnostic output and then once more for the final return value, which is an array of numbers. Wondering how to fix this pausing behavior. Here's what my code looks link:
// WSTP template
:Begin:
:Function: function
:Pattern: function[....]
:Arguments: {...}
:ArgumentTypes: {....}
:ReturnType: Manual
:End:
#include <stdio.h>
#include "wstp.h"
void function(.....)
{
// Calculate a bunch of stuff, putting the results in an array named "result"
.....
// While we're calculating this stuff, the original C program used printf calls to output diagnostic text strings
// at various points in the program
// printf("%s\n", diagostic_text);
// These messages do not appear in the Mathematica notebook, so instead I use a Wolfram C function:
....
WSPutString(stdlink,diagtxt1);
.....
WSPutString(stdlink,diagtxt2);
....
// Return the results of the calculation
WSPutReal64List(stdlink, result, size);
return;
}
int main(int argc, char *argv[]) {
return WSMain(argc, argv);
}
Now in my Mathematic notebook, I have to call the function many times. For example, if there are 43 "printf" statements before the C program returns the final result, I have to do this:
Install["./function"]
Table[function[....], 44]