11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7.2 Using Compiled Code in Maple • 317/* main entry point - MWRAP_add */ALGEB myAdd( MKernelVector kv, ALGEB fn_args ){INTEGER32 a1; /* INTEGER32 => int (defined in mpltable.h) */INTEGER32 a2;INTEGER32 r;if( MapleNumArgs(kv,fn_args) != 2 )MapleRaiseError(kv,"Incorrect number of arguments");/* convert from Maple integer to C int */a1 = MapleToInteger32(kv,((ALGEB*)fn_args)[1]);/* convert from Maple integer to C int */a2 = MapleToInteger32(kv,((ALGEB*)fn_args)[2]);r = a1 + a2;}return( ToMapleInteger(kv,(long) r) );This program first verifies if the Maple function call passed exactlytwo arguments. It then converts the two arguments to hardware integersand adds them. The result is converted to a Maple integer and returned.This program can be compiled into a DLL using your favorite C compiler.Ensure that you link with the Maple API shared library. The DLLcan be placed into the Maple bin.$SYSTEM directory, or somewhere elsein the PATH. When using DLLs outside of bin.$SYSTEM directory, youmay need to specify the full path to the DLL in the LIB argument todefine_external. UNIX developers may need to set their load-librarypath.Table 7.4 on page 340 lists the Maple API Libraries for C and Fortran.After compiling the DLL, the function can be used in Maple. No typedesciptors are needed in the define_external call since Maple does noconversion on arguments passed to the custom wrapper.> myAdd := define_external(’myAdd’,’MAPLE’,’LIB’=> "myAdd.dll"):> myAdd(2,3);5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!