Re: Setting up external functions to be called from Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg82399] Re: Setting up external functions to be called from Mathematica
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Fri, 19 Oct 2007 05:01:54 -0400 (EDT)
- References: <ff781v$nod$1@smc.vnet.net>
Hi,
> 1. How do I create a MathLink template? Can I just use text editor
> and save it with .tm extension?
you write down a ASCII text and ask how to write a ASCII text ??
How do you write your C code ? With a ASCII editor ?? and you think
the same ASCII editor can't be used to write the template ?
>Where should the file be saved?
On your hard disk ? on a floppy disk ? ? on a net work drive ???
So, and finaly there is a pre-processor called mprep,
this pre-processor read the template and the c-file and generate
several new functions, one of it is MLMain()
On a UNIX box you can use the mcc script that call the mprep
pre-processor. On Windows you have to call mprep by your self
to make valid C-code form the template and C-definitions you wrote.
Say you have a template myfun.tm and the C-fragment myfun.c than
mprep myfun.tm myfun.c -o myfun.tmp.c
gcc -o myfun myfun.tmp.c -lML
should generate (assuming you have include and library path set correct)
a executable myfun that you can install in Mathematica. If you have
saved the *.tm file in a different directory than the code fragment
myfun.c, you have to give the correct path for both files.
If you don't know where to save the *.tm file --
don't make a *.tm file !
When I'm right, you know how to write the c-code fragment ? and you know
where to save it ? So just write the template definition at the top
of the file, i.e.,
-BeginOfYourFile:f.tm---------------------------
:Begin:
:Function: f
:Pattern: f[x_Integer, y_Integer]
:Arguments: {x, y}
:ArgumentTypes: {Integer, Integer}
:ReturnType: Integer
:End:
#include "mathlink.h"
int main(int argc, char *argv[]) {
return MLMain(argc, argv);
}
int f(int x, int y) {
return x+y;
}
-EndOfYourFile:f.tm---------------------------
and call mprep with
mprep f.tm -o f.c
gcc -o myfun f.c -lML
Regards
Jens
pinksheep415 wrote:
> Hi,
>
> I'm trying to figure out how to call an external function from
> Mathematica. Examples I've found so far doesn't give enough
> informations for a newbie like myself.
>
> The following is content of an example MathLink template file, f.tm:
>
> :Begin:
> :Function: f
> :Pattern: f[x_Integer, y_Integer]
> :Arguments: {x, y}
> :ArgumentTypes: {Integer, Integer}
> :ReturnType: Integer
> :End:
>
> 1. How do I create a MathLink template? Can I just use text editor
> and save it with .tm extension? Where should the file be saved?
>
> Following is the content of an example c code, I named it f.c:
> #include "mathlink.h"
>
> int main(int argc, char *argv[]) {
> return MLMain(argc, argv);
> }
>
> int f(int x, int y) {
> return x+y;
> }
>
> 2. Although I copied "mathlink.h" file into INCLUDE folder for C
> compiler (I'm temporarily using Dev-C++), the compiler doesn't
> recognize MLMain(argc, argv); Can anyone tell why? Does this have to
> do w/ the kind of compiler I'm using? Does this c file need to be in
> the same folder as the above .tm file?
>
> 3. After I get these files saved in the right folders, how do I use
> them?
> Any help would be greatly appreciated!!!
>
> -selim50 at gmail.com
>
>