Most examples are made of 4 files:
- *.c : where the computation occurs.
- *.tm : MathLink template where the input and output are specified and the documentation of the function is placed.
- Makefile : where the compilation sequence is specified
- *.nb : Mathematica notebook that executes the whole program
As seen in Makefile, mprep generates a proper *.c code from the *.tm file, which is compiled along with the *.c file that contains the computational process.
The first example adds two integers
---------------------------------------------------------------------------------
addtwo.c
---------------------------------------------------------------------------------
#include "mathlink.h" extern int addtwo( int i, int j); int addtwo( int i, int j) { return i+j; } #if WINDOWS_MATHLINK #if __BORLANDC__ #pragma argsused #endif 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); } #else int main(int argc, char* argv[]) { return MLMain(argc, argv); } #endif
-----------------------------------------------------------------
addtwo.tm
-----------------------------------------------------------------
int addtwo P(( int, int)); :Begin: :Function: addtwo :Pattern: AddTwo[i_Integer, j_Integer] :Arguments: { i, j } :ArgumentTypes: { Integer, Integer } :ReturnType: Integer :End: :Evaluate: AddTwo::usage = "AddTwo[x, y] gives the sum of two machine integers x and y."
------------------------------------------------------------
Makefile
------------------------------------------------------------
MPREP = /usr/bin/mprep CXX = /usr/bin/c++ BINARIES = addtwo all : $(BINARIES) addtwo : addtwotm.c addtwo.c ${CXX} addtwotm.c addtwo.c -lML64i3 -lm -lpthread -lrt -lstdc++ -o $@ addtwotm.c : addtwo.tm ${MPREP} addtwo.tm -o addtwotm.c
------------------------------------------------
addtwo.nb
------------------------------------------------
SetDirectory["/home/rcabrera/Documents/source/mathematica/MathLinkExamples/mytest2/alternative"] link = Install["./addtwo"] ?AddTwo AddTwo[2, 3]
can any one help me to write the sample programm for mathlink C#...
ReplyDelete