jpmaus
jpmaus

Reputation: 1

How do I write the entry point in a c file such that the file's other functions can be accessed via command line and/or WSTP/MathLink/Mathematica?

I have a 52KB c program with several functions (e.g., Function1, Function2, Function3, etc.) that I call from a "Main" entry point function. But rewriting/recompiling the program each time I need to use these functions seems like it is probably a waste of my newbie time. Ideally, I'd like to call these functions in Mathematica, but having spent all day trying to figure out WSTP / MathLink, it seems that accomplishing such is beyond me; if not beyond most, given the ubiquity of the tiresome addtwo example.

There must be a simple way to rewrite this Main function such that Function1, Function2, etc., can be easily executed via command line interface, if not called by Mathematica (e.g., LibraryFunctionLoad, Install, MLGetInteger(), etc.). I have a Main entry point:

int main (int argc, char *argv[])
{
    FUNCTION1(n1,n2,n3,etc.);
    FUNCTION2(n4,n5,n6,etc.);
    FUNCTION3(n7,n8,n9,etc.);   
 }

and I know how to change the inputs (i.e., n1, n2, n3, etc.) in the source, recompile it, and get my outputs. But how do I change and/or wrap the code above such that the inputs can be entered by way of a command line interface, or even better, by way of Mathematica? In relation to MathLink, it seems as if one must compile each function individually, that is, as opposed to accessing them all through the main entry point function, is this correct? And everything I've managed to find on the WWW about c and the command line interface has been kiddie stuff, like getting the terminal to tell you how many times you've hit "return" on your keyboard and stuff like that... I know what I'm asking here is ridiculously basic, but I wouldn't risk loosing points if I wasn't in desperate need of an answer.

Upvotes: 0

Views: 122

Answers (1)

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

Parse argc/argv which are command line arguments, you should then be able to convert it as you need. For example if you want to convert the first argument of the command line:

$ mycommand 10

to an int, you can use:

int main(int argc,char **argv) {
  int n1;
  sscanf(argv[1],"%d",&n1); // convert string to int
  FUNCTION1(n1,etc.);
}

Upvotes: 1

Related Questions