need help!!! calling Matlab from c++

R

rotarywing

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
 
L

Larry I Smith

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
 
R

red floyd

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?
 
R

rotarywing

red said:
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
 
R

rotarywing

Larry said:
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
 
L

Larry I Smith

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
 
Joined
Jul 26, 2010
Messages
1
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top