f



use of Matlab functions for 'c++'

Hi!!! I'm working with SystemC that is something like a c++ library,
and I wanna know how can I use the Matlab functions for 'c++'. I
think there is a principal function called 'mexFunction' which is the
gateway routine to call all that functions but I've already a 'main'
function so I don't know if I can use both or how can I use it
without the main, because I need the main function.
Thanks.
0
molakey2 (35)
6/6/2007 9:31:57 AM
comp.soft-sys.matlab 211264 articles. 25 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
620 Views

Similar Articles

[PageSpeed] 50

Laura wrote:
> Hi!!! I'm working with SystemC that is something like a c++ library,
> and I wanna know how can I use the Matlab functions for 'c++'. I
> think there is a principal function called 'mexFunction' which is the
> gateway routine to call all that functions but I've already a 'main'
> function so I don't know if I can use both or how can I use it
> without the main, because I need the main function.
> Thanks.


first i thought you want to call matlab function from you c++ program, 
but then the description sounds like you want to call c++ from matlab.

so, what is it?

if the first, you'll have to go through the matlab engine, in the second 
you are talking about mex files. both are documented in the 'external 
interfaces' section of the matlab help.


michael
0
6/6/2007 12:31:44 PM
Hi!! I want to call matlab function from a c++
program, but if I wrote that in the .h file:
#ifdef __cplusplus
extern "C"{
#endif

extern mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n,mxComplexity
ComplexFlag);
extern void mxSetM(mxArray *pm, mwSize m);
extern void mxSetN(mxArray *pm, mwSize n);
extern void mxDestroyArray(mxArray *pm);

I get the error:
$ make
g++ -Wall -g -O3 -I. -I../include
-I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
.../obj/moduleM
atrix.o -c ../src/moduleMatrix.cpp
.../src/moduleMatrix.cpp:108: error: parse error at end of input
make: *** [moduleMatrix.o] Error 1

But if I wrote:
#ifdef __cplusplus
extern "C"{
#endif

extern mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n,mxComplexity
ComplexFlag);
extern void mxSetM(mxArray *pm, mwSize m);
extern void mxSetN(mxArray *pm, mwSize n);
extern void mxDestroyArray(mxArray *pm);
}

Whit the final '}' I get the error:
$ make
g++ -Wall -g -O3 -I. -I../include
-I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
.../obj/array.o
 -c ../src/array.cpp
g++ -Wall -g -O3 -I. -I../include
-I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
.../obj/sc_main
..o -c ../src/sc_main.cpp
g++ -Wall -g -O3 -I. -I../include
-I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -L.
-L/usr/cad/sy
stemc/systemc_2_1.oct_12_2004.beta/lib-cygwin -o ../bin/demo.x \
   ../obj/moduleMatrix.o ../obj/array.o ../obj/sc_main.o -lsystemc
-lstdc++ -lm
  2>&1 | c++filt; rm -f Makefile.deps
.../obj/moduleMatrix.o(.text+0x81e): In function
`_ZN6Matrix10CreateDataEv':
/Phase1/module1/bin/../src/moduleMatrix.cpp:66: undefined reference
to `_mxGetNu
mberOfElements'
.../obj/moduleMatrix.o(.text+0x990):/Phase1/module1/bin/../src/moduleMa
trix.cpp:8
7: undefined reference to `_mxSetPr'
.../obj/array.o(.text+0x7f): In function `_ZN5Array11constructorEv':
/Phase1/module1/bin/../src/array.cpp:7: undefined reference to
`_mxCreateDoubleM
atrix_730'
.../obj/array.o(.text+0x93):/Phase1/module1/bin/../src/array.cpp:8:
undefined ref
erence to `_mxSetM_730'
.../obj/array.o(.text+0xa5):/Phase1/module1/bin/../src/array.cpp:9:
undefined ref
erence to `_mxSetN_730'
.../obj/array.o(.text+0xbe): In function `_ZN5Array10destructorEv':
/Phase1/module1/bin/../src/array.cpp:15: undefined reference to
`_mxDestroyArray
'
collect2: ld returned 1 exit status

Why? I don't understand.
Thank you
0
molakey2 (35)
6/18/2007 9:44:09 AM
On 18 Jun, 11:44, Laura <molak...@hotmail.com> wrote:
> Hi!! I want to call matlab function from a c++
> program, but if I wrote that in the .h file:
....
> $ make
> g++ -Wall -g -O3 -I. -I../include
....
> Why? I don't understand.
> Thank you

First, MEX is a program that needs to be configured and run from
within matlab, it seems like you try to run it like a standard 'make'?
That will not work.

Having said that, I have a vague recollection of having seen
somewhere
that matlab and gcc/g++ don't go very well together.

To test, find one of the C++ example files which is supplied
with matlab, and see if it compiles and runs correctly. Check the
external interfaces guide to find out where the examples are
located. Type 'doc mex' from the matlab prompt for help on how to
configure mex to use g++.

Rune

0
allnor (8509)
6/18/2007 1:53:32 PM
Laura wrote:
> Hi!! I want to call matlab function from a c++
> program, but if I wrote that in the .h file:
> #ifdef __cplusplus
> extern "C"{
> #endif
> 
> extern mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n,mxComplexity
> ComplexFlag);
> extern void mxSetM(mxArray *pm, mwSize m);
> extern void mxSetN(mxArray *pm, mwSize n);
> extern void mxDestroyArray(mxArray *pm);
> 
> I get the error:
> $ make
> g++ -Wall -g -O3 -I. -I../include
> -I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
> ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
> ../obj/moduleM
> atrix.o -c ../src/moduleMatrix.cpp
> ../src/moduleMatrix.cpp:108: error: parse error at end of input
> make: *** [moduleMatrix.o] Error 1

Of course there's a parser error, you are compiling with a C++ compiler that
defines _cplusplus, so there's an unterminated brace.

> 
> But if I wrote:
> #ifdef __cplusplus
> extern "C"{
> #endif
> 
> extern mxArray *mxCreateDoubleMatrix(mwSize m, mwSize n,mxComplexity
> ComplexFlag);
> extern void mxSetM(mxArray *pm, mwSize m);
> extern void mxSetN(mxArray *pm, mwSize n);
> extern void mxDestroyArray(mxArray *pm);
> }
> 
> Whit the final '}' I get the error:
> $ make
> g++ -Wall -g -O3 -I. -I../include
> -I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
> ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
> ../obj/array.o
>  -c ../src/array.cpp
> g++ -Wall -g -O3 -I. -I../include
> -I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
> ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -o
> ../obj/sc_main
> .o -c ../src/sc_main.cpp
> g++ -Wall -g -O3 -I. -I../include
> -I/usr/cad/systemc/systemc_2_1.oct_12_2004.be
> ta/include -I/usr/cad/systemc/systemc_2_1.oct_12_2004.beta/src -L.
> -L/usr/cad/sy
> stemc/systemc_2_1.oct_12_2004.beta/lib-cygwin -o ../bin/demo.x \
>    ../obj/moduleMatrix.o ../obj/array.o ../obj/sc_main.o -lsystemc
> -lstdc++ -lm
>   2>&1 | c++filt; rm -f Makefile.deps
> ../obj/moduleMatrix.o(.text+0x81e): In function
> `_ZN6Matrix10CreateDataEv':
> /Phase1/module1/bin/../src/moduleMatrix.cpp:66: undefined reference
> to `_mxGetNu
> mberOfElements'
> ../obj/moduleMatrix.o(.text+0x990):/Phase1/module1/bin/../src/moduleMa
> trix.cpp:8
> 7: undefined reference to `_mxSetPr'
> ../obj/array.o(.text+0x7f): In function `_ZN5Array11constructorEv':
> /Phase1/module1/bin/../src/array.cpp:7: undefined reference to
> `_mxCreateDoubleM
> atrix_730'
> ../obj/array.o(.text+0x93):/Phase1/module1/bin/../src/array.cpp:8:
> undefined ref
> erence to `_mxSetM_730'
> ../obj/array.o(.text+0xa5):/Phase1/module1/bin/../src/array.cpp:9:
> undefined ref
> erence to `_mxSetN_730'
> ../obj/array.o(.text+0xbe): In function `_ZN5Array10destructorEv':
> /Phase1/module1/bin/../src/array.cpp:15: undefined reference to
> `_mxDestroyArray
> '
> collect2: ld returned 1 exit status
> 
> Why? I don't understand.
> Thank you



This error is because you're not linking against the Matlab libraries. You need
to specify the directory and libraries for Matlab (i.e. -lmx -lmat -leng). You
need to specify the paths to those libraries with the -L flag for g++.

Chris
0
cchgroupmail (818)
6/18/2007 1:59:13 PM
Laura wrote:
>
>
> Hi!!! I'm working with SystemC that is something like a c++
> library,
> and I wanna know how can I use the Matlab functions for 'c++'. I
> think there is a principal function called 'mexFunction' which is
> the
> gateway routine to call all that functions but I've already a
> 'main'
> function so I don't know if I can use both or how can I use it
> without the main, because I need the main function.
> Thanks.
0
molakey2 (35)
6/19/2007 9:14:11 AM
Reply: