need help!!! calling Matlab from c++

Discussion in 'C++' started by rotarywing@gmail.com, Jun 17, 2006.

  1. Guest

    Hi, guys

    When I using Matlab engine in a c++ program, I encounter a
    problem.
    I hope someone could help me. It's urgent.

    One simple cpp code can present my problem. My code as follows


    //---------------------------------------------------//
    // cpp_ma.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <stdio.h>
    #include "iostream.h"
    #include "engine.h"

    int main(int argc, char* argv[])
    {
    Engine *m_ep;

    cout<<"&&&&&&&&&&&&& 001 &&&&&&&&&&&&&&&&&&"<<endl;

    if (!(m_ep=engOpen("\0")))
    {
    cout<<"########### 002 ###########"<<endl;
    fprintf(stderr,"\n Can't start MATLAB engine\n");
    exit(-1);
    }
    cout<<"########### 003 ###########"<<endl;
    engClose(m_ep);

    return 0;
    }

    //---------------------------------------------------//

    I use MS Visual C++ 6.0 to build this code. Under the debug mode,
    compling and
    building are OK, but when runing the program, error came out and it
    said " Unhandled
    exception in cpp_ma.exe: 0xc0000005:access violation." Under the
    release mode,
    compling is OK, but when building, below mesage poped out

    //----------------------------------------------------//

    --------------------Configuration: cpp_ma - Win32
    Release--------------------
    Linking...
    LINK : warning LNK4089: all references to "0.obj" discarded by /OPT:REF
    LINK : warning LNK4089: all references to "1.obj" discarded by /OPT:REF
    LINK : warning LNK4089: all references to "14.obj" discarded by
    /OPT:REF
    LINK : warning LNK4089: all references to "20.obj" discarded by
    /OPT:REF

    cpp_ma.exe - 0 error(s), 4 warning(s)

    //----------------------------------------------------//

    When runing the exe, there is a error, it said memory 0x00905a4d
    can't read .

    I really don't know what happened to my code, I have added "
    libeng.lib libmx.lib libut.lib " to
    the Object/library modules( Project->settings->link->general) and the
    relevant paths
    also added to the Tools->Options->Directories.

    Please help me if you have any idea about this. I would appreciate it
    sincerely
    Thanks in advance.

    Best regard.

    rotor
    , Jun 17, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi, guys
    >
    > When I using Matlab engine in a c++ program, I encounter a
    > problem.
    > I hope someone could help me. It's urgent.
    >
    > One simple cpp code can present my problem. My code as follows
    >
    >
    > //---------------------------------------------------//
    > // cpp_ma.cpp : Defines the entry point for the console application.
    > //
    > #include "stdafx.h"
    > #include <stdio.h>
    > #include "iostream.h"
    > #include "engine.h"



    You haven't provided enough information...

    Is Matlab C, rather than C++?
    If it is, then find all of the the places in your source
    where you are including the Matlab headers (inside 'engine.h'
    perhaps) and wrap them with an 'ifdef' block like this:

    /* if we are compiling C++ code */
    #ifdef __cplusplus
    /* the Matlab API is written in C, not C++.
    * so we have to tell the compiler that these
    * API's use the C naming/calling conventions.
    */
    extern "C" {
    #include "matlab_header_1.h"
    #include "matlab_header_2.h"
    #include "matlab_header_3.h"
    etc, etc, etc
    } /* end of 'extern "C"' block */
    #endif /* __cplusplus */

    This tells the compiler/linker which naming & calling conventions
    are used by the API functions. C and C++ (on many platforms)
    pass function arg on different manners, and encode the
    function names within the object files in different manners.


    >
    > int main(int argc, char* argv[])
    > {
    > Engine *m_ep;
    >
    > cout<<"&&&&&&&&&&&&& 001 &&&&&&&&&&&&&&&&&&"<<endl;
    >
    > if (!(m_ep=engOpen("\0")))
    > {
    > cout<<"########### 002 ###########"<<endl;
    > fprintf(stderr,"\n Can't start MATLAB engine\n");
    > exit(-1);
    > }
    > cout<<"########### 003 ###########"<<endl;
    > engClose(m_ep);
    >
    > return 0;
    > }
    >
    > //---------------------------------------------------//
    >
    > I use MS Visual C++ 6.0 to build this code. Under the debug mode,
    > compling and
    > building are OK, but when runing the program, error came out and it
    > said " Unhandled
    > exception in cpp_ma.exe: 0xc0000005:access violation." Under the
    > release mode,
    > compling is OK, but when building, below mesage poped out
    >
    > //----------------------------------------------------//
    >
    > --------------------Configuration: cpp_ma - Win32
    > Release--------------------
    > Linking...
    > LINK : warning LNK4089: all references to "0.obj" discarded by /OPT:REF
    > LINK : warning LNK4089: all references to "1.obj" discarded by /OPT:REF
    > LINK : warning LNK4089: all references to "14.obj" discarded by
    > /OPT:REF
    > LINK : warning LNK4089: all references to "20.obj" discarded by
    > /OPT:REF
    >
    > cpp_ma.exe - 0 error(s), 4 warning(s)
    >
    > //----------------------------------------------------//
    >
    > When runing the exe, there is a error, it said memory 0x00905a4d
    > can't read .
    >
    > I really don't know what happened to my code, I have added "
    > libeng.lib libmx.lib libut.lib " to
    > the Object/library modules( Project->settings->link->general) and the
    > relevant paths
    > also added to the Tools->Options->Directories.
    >
    > Please help me if you have any idea about this. I would appreciate it
    > sincerely
    > Thanks in advance.
    >
    > Best regard.
    >
    > rotor
    >
    Larry I Smith, Jun 17, 2006
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > Hi, guys
    >
    > When I using Matlab engine in a c++ program, I encounter a
    > problem.
    > I hope someone could help me. It's urgent.
    >
    > One simple cpp code can present my problem. My code as follows
    >
    > [redacted]


    May I suggest comp.soft-sys.matlab?
    red floyd, Jun 17, 2006
    #3
  4. Guest

    Re: need help!!! calling Matlab from c++

    red floyd wrote:
    > wrote:
    > > Hi, guys
    > >
    > > When I using Matlab engine in a c++ program, I encounter a
    > > problem.
    > > I hope someone could help me. It's urgent.
    > >
    > > One simple cpp code can present my problem. My code as follows
    > >
    > > [redacted]

    >
    > May I suggest comp.soft-sys.matlab?


    hello,

    I have pasted a clone in comp.soft-sys.matlab.
    Thanks anyway
    , Jun 18, 2006
    #4
  5. Guest

    Re: need help!!! calling Matlab from c++

    Larry I Smith wrote:

    >
    > You haven't provided enough information...
    >
    > Is Matlab C, rather than C++?
    > If it is, then find all of the the places in your source
    > where you are including the Matlab headers (inside 'engine.h'
    > perhaps) and wrap them with an 'ifdef' block like this:
    >
    > /* if we are compiling C++ code */
    > #ifdef __cplusplus
    > /* the Matlab API is written in C, not C++.
    > * so we have to tell the compiler that these
    > * API's use the C naming/calling conventions.
    > */
    > extern "C" {
    > #include "matlab_header_1.h"
    > #include "matlab_header_2.h"
    > #include "matlab_header_3.h"
    > etc, etc, etc
    > } /* end of 'extern "C"' block */
    > #endif /* __cplusplus */
    >
    > This tells the compiler/linker which naming & calling conventions
    > are used by the API functions. C and C++ (on many platforms)
    > pass function arg on different manners, and encode the
    > function names within the object files in different manners.
    >
    >



    Hello,
    Thanks for your response.
    I'm compling c++ code. I have found where to place the 'ifdef'
    block you
    mentioned. In the engine.h file, there is an original one like this

    /*
    #ifdef __cplusplus
    extern "C" {
    #endif
    */

    But I'm confused about the matlab_header_*.h files. What are their
    names, and
    where to find them, thanks

    rotor
    , Jun 18, 2006
    #5
  6. Re: need help!!! calling Matlab from c++

    wrote:
    > Larry I Smith wrote:
    >
    >> You haven't provided enough information...
    >>
    >> Is Matlab C, rather than C++?
    >> If it is, then find all of the the places in your source
    >> where you are including the Matlab headers (inside 'engine.h'
    >> perhaps) and wrap them with an 'ifdef' block like this:
    >>
    >> /* if we are compiling C++ code */
    >> #ifdef __cplusplus
    >> /* the Matlab API is written in C, not C++.
    >> * so we have to tell the compiler that these
    >> * API's use the C naming/calling conventions.
    >> */
    >> extern "C" {
    >> #include "matlab_header_1.h"
    >> #include "matlab_header_2.h"
    >> #include "matlab_header_3.h"
    >> etc, etc, etc
    >> } /* end of 'extern "C"' block */
    >> #endif /* __cplusplus */
    >>
    >> This tells the compiler/linker which naming & calling conventions
    >> are used by the API functions. C and C++ (on many platforms)
    >> pass function arg on different manners, and encode the
    >> function names within the object files in different manners.
    >>
    >>

    >
    >
    > Hello,
    > Thanks for your response.
    > I'm compling c++ code. I have found where to place the 'ifdef'
    > block you
    > mentioned. In the engine.h file, there is an original one like this
    >
    > /*
    > #ifdef __cplusplus
    > extern "C" {
    > #endif
    > */
    >
    > But I'm confused about the matlab_header_*.h files. What are their
    > names, and
    > where to find them, thanks
    >
    > rotor
    >


    I don't know the name of the Matlib headers. I used made-up
    names in my example. Replace those with the names of the real
    Matlib headers.

    Also get a good C++ book that explains all of this.

    Larry
    Larry I Smith, Jun 18, 2006
    #6
  7. JaiPillai

    Joined:
    Jul 26, 2010
    Messages:
    1
    If you are still needing help, check out the tutorial I wrote recently about using MATLAB Engine in Visual Studio. Its available in the Download section of my webpage.
    JaiPillai, Jul 26, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mohammed Smadi

    calling matlab

    Mohammed Smadi, Oct 12, 2005, in forum: Python
    Replies:
    3
    Views:
    572
    David Isaac
    Oct 25, 2005
  2. Robert Kern

    Re: calling matlab

    Robert Kern, Oct 12, 2005, in forum: Python
    Replies:
    0
    Views:
    391
    Robert Kern
    Oct 12, 2005
  3. siki
    Replies:
    0
    Views:
    940
  4. Replies:
    3
    Views:
    1,051
    nonother
    Jul 9, 2011
  5. Luna Moon
    Replies:
    16
    Views:
    1,321
    Giovanni Gherdovich
    Aug 8, 2008
Loading...

Share This Page