MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Re: mathlink & Visual C++

  • To: mathgroup at
  • Subject: [mg15075] Re: [mg15030] Re: [mg15009] Re: mathlink & Visual C++
  • From: "Richard W. Singerman" <rws at>
  • Date: Thu, 10 Dec 1998 03:13:06 -0500
  • Sender: owner-wri-mathgroup at

In response to the posting, I have tried to make a simple example. What
do I need to modify to make it work?

Please comment. I'm finding this
introduction to MathLink and visual  c++ rather painful.

Note: I'm using Borland ver. 5 c++ for windows95.
extern "C" {

//... C function declarations here ....

MLENV MLInitialize(0);
int   MLActivate(Mlink link);
MLINK MLOpenArgv(MLENV env, char **argv0, char **argv1, long *errno);
void  MLClose(MLINK link);

??  MLPutString(stdlink, char *s);

#ifdef __cplusplus

#include "mathlink.h"
main(int argc, char *argv[]) {

        MLENV env;
        MLINK link;
        long *errno;

       env = MLInitialize(0);
       mylink = MLOpenArgv(env, argv,argv+argc, &errno);

        MLPutFunction( mylink, "ToExpression", 1);
        MLPutString( mylink, "b = Sin[Pi/2]");
       MLClose( mylink);


If I compile this in C++ (Borland ver. 5 for windows95) and run it,
I would like to see this pop up in my mathematica session and
 evaluate to "1". Can I then go to the mathematica window and use "b" in


>From: MJE <evans_nospam at>
>Subject: [mg15075] PRIVATE REPLY Re: [mg15019] error from MathLink,
     plotting results from a c++ simulation


MathLink cannot handle C++ directly.  I just made a post to the group on
that topic, answering someone else's problems.

You have to treat all your MathLink code as plain C and compile it that
way.  To use it from C++, use the usual header file trick,

#ifdef __cplusplus
extern "C" {
... C function declarations here .... #ifdef __cplusplus

which warns the C++ compiler to call the MathLink stuff as plain C. Make
sure to use a .c extension with the mprep tool, not a .cpp extension.

On Wed, 9 Dec 1998, MJE wrote:

> Apropos of that --
> I've had problems going way back to version 2 with MathLink not working
> under C++; and that was on Macintosh.  I ended up having to wrap all
> the MathLink code in "extern 'C'".  That was a long time ago.  Now I
> learn that things are still this way.
> MathLink is hard enough to master without the hassles of C vs. C++
> interfacing.  What this problem means is that all the books on C++
> algorithms are useless with MathLink.  (You have to create a plain-C
> buffer between MathLink and the algorithms if you want to use them.) 
> It also means that nice, clean, object-based string-handling functions
> cannot be used to deal with the string expression requirements of
> MathLink.
> It's ironic that WRI is selling MathCode C++ without having fixed this
> problem with MathLink itself.  Maybe they could get the people who
> built MathCode C++ to work on fixing MathLink as well.
> Mark
> remove "_nospam" to reply
> Kevin Leuthold wrote:
> > 
> > I had a private email conversation with John regarding the problem he
> > described, and want to share with the whole group my answer to him
> > since it may be of interest to anyone else compiling MathLink programs
> > with Visual C++.
> > 
> > John was giving mprep-generated code a .cpp extension, rather than a .c
> > extension.  In other words, instead of
> > 
> > mprep -o addtwotm.c
> > 
> > he was typing
> > 
> > mprep -o addtwotm.cpp
> > 
> > Unfortunately, mprep generates code that fails to compile under the
> > stronger  type checking that Visual C++ performs on .cpp files.  This
> > probably should be considered a bug in mprep.
> > 
> > Fortunately, there is a simple workaround, which is to always give
> > mprep-generated  code a .c extension rather than .cpp.  Of course, this
> > would mean that any C++ code  in a .tm file would have to be taken out
> > and put in a .cpp file.
> > 
> > Other than this bug that John has pointed out, there are no known
> > problems with building MathLink programs using Microsoft Visual C++
> > 6.0.
> > 
> > Kevin Leuthold
> > MathLink Group
> > Wolfram Research
> > 
> > John Gore <gore_john at> writes:
> > 
> > >I just moved to Vis C++ 6.0 from 4.0 and cant get my mathlink programs
> > >to compile.
> > 
> > > I have problems with the MLInitializeIcon routine produced by mprep. I
> > >get errors from  a lack of casting on structs HINSTANCE and HBRUSH
> > 
> > >This happens even in the addtwo example.
> > 
> > >Anyone using MS Vis C++ 6.0 ?
> > 
> > >Im using Mathematica 3.01
> > >Current version of Mathlink as Downloaded Windows 95
> > 
> > >Thanks - JG

  • Prev by Date: Two questions---nonevaluated Integrate; NIntegrate inside NDSolve
  • Next by Date: Re: Variables names
  • Previous by thread: Two questions---nonevaluated Integrate; NIntegrate inside NDSolve
  • Next by thread: $Path variable