Matlab与Visual C++混合编程学习笔记 3 J' g K" ?& n, G' H6 b / a- ]3 @! b8 r" }要实现VC++与matlab的无缝混编,必须充分掌握matlab的C/C++数学库函数和图形库函数,而要运用这两类库函数则必须先熟悉matlab的接口类与接口函数的结构及使用规则。. D; |$ ?4 ~4 P n `
( J( [+ B6 |7 O0 D" J2 umcc & J- W1 d$ Q! p Q 4 a( p! z$ U, V1 A. Lmatlab与VC++混编的两个重要的且强大的命令mbuild和mcc,前者我们相对用得较少,故主要一般先从后者mcc的使用入手,其实在上一节中我们已经领略到了它的威力。, x% H6 o* n: v, M, m9 l! w( u
实际上,关于mcc(matlab compiler command)乃至混合编程,最好的使用教程就是matlab的help和官方网站提供的user’s guide等文档。这里不妨截取其中的一部分来说明吧,个人觉得原文应该比中文讲解得更细致,当然这里只提供了一部分主干线索,引入门吧,具体深入修行靠个人了。 & u' g( X {9 Q; q x7 q3 b' P A& o+ T4 R- a' N \3 L I
mcc , g3 U( O4 P# b+ k6 EInvoke MATLAB Compiler 1 f( E Z" n9 t# R6 a( t* FSyntax mcc [-options] mfile1 [mfile2 ... mfileN] l; ]; K# M) M
[C/C++file1 ... C/C++fileN]5 r! g: _. N- D* e0 U, R1 \
; ~) l3 c, H1 c( c4 }1. Description4 q# N* U' T: E: t$ C9 f* d
mcc is the MATLAB command that invokes the MATLAB Compiler. You can issue the mcc command either from the MATLAB command prompt (MATLAB mode) or the DOS or UNIX command line (stand-alone mode).5 Z# q6 c3 m4 F" [
7 Z \9 s# V E+ v" y% r0 v" u
2. Command Line Syntax 0 X; T( b* n* [* }- xYou may specify one or more MATLAB Compiler option flags to mcc. Most option flags have a one-letter name. You can list options separately on the command line, for example: $ B3 _/ z3 p' Nmcc -m -g myfun , P* h6 O+ ?- {6 Y3 r% [1 R9 y& V/ v& V& g2 E4 K/ z
You can group options that do not take arguments by preceding the list of option flags with a single dash (-), for example: . u: U6 y) H/ m' g
mcc -mg myfun 3 a% @9 I( m) \4 R3 A# ]5 w2 C& y. [+ f& X. J' u- w" V% d9 C
Options that take arguments cannot be combined unless you place the option with its arguments last in the list. For example, these formats are valid: 1 N4 J4 n; o% a! J9 n! E& P
mcc -m -A full myfun % Options listed separately0 \& k# c: ^; h) A; o0 K7 j8 \# H" W
mcc -mA full myfun % Options combined, A option last 0 Q- b- D1 l, b9 N5 W- k) F$ G3 ^This format is not valid: - W, V9 F' @# F6 w; N; ]3 ?mcc -Am full myfun % Options combined, A option not last7 R8 p/ G- R3 j6 n. j0 y; ?
2 @: z( z- X0 A
1 v) |9 _+ ]! C; S3 R3. Using Macros to Simplify Compilation " g4 e9 s( D2 Z5 [- ]. i The MATLAB Compiler, through its exhaustive set of options, gives you access to the tools you need to do your job. If you want a simplified approach to compilation, you can use one simple option, i.e., macro, that allows you to quickly accomplish basic compilation tasks. If you want to take advantage of the power of the Compiler, you can do whatever you desire to do by choosing various Compiler options. ^4 s- u+ ~1 |1 c' M0 g% b5 }( s
Table 7-2, Macro Options, shows the relationship between the macro approach to accomplish a standard compilation and the multioption alternative.$ S2 Y+ s$ l0 q5 D! h% }7 C + {- B* [3 Z: C" \! s& D) c 1 o7 j" m/ y% t* x! F
Understanding a Macro Option. The -m option tells the Compiler to produce a stand-alone C application. The -m macro is equivalent to the series of options -t -W main -L C -T link:exe -h libmmfile.mlib 9 N& l" L& m) [ 8 s) ?/ w* q4 P2 j) X' rTable 7-3, -m Macro, shows the options that compose the -m macro and the information that they provide to the Compiler.7 R# B8 Z- n# [* B. T/ e8 o# U
0 b2 \. O) W& Z
3 R* T/ G& E2 H. G9 I+ M; J) l& }, |- o4 K: N9 I1 m# C
4. Macro Options . x! \/ W# y E1 Z rThe macro options provide a simplified way to accomplish basic compilation tasks. ) }( h" N# K2 x% w-g (Debug). This option is a macro that is equivalent to ) M P: r2 m {, X+ l-G -A debugline:on -O none0 X3 `! N( E) ]3 d
or ' E; K8 Y) d# ^, m B: r
-B macro_option_g / p! r( H1 U' |8 q7 d6 e: q1 y, u2 d9 k8 a
-m (Stand-Alone C).( E/ f$ x3 U3 |9 A S
mcc -m mymfile6 r5 F% R- P* J) _' S
The -m option is equivalent to the series of options 1 s. D3 f) _ h1 r) v-W main -L C -t -T link:exe -h libmmfile.mlib+ D. Y% v$ c7 L# d/ Z
or : F! ?) Q5 X0 u-B macro_option_m6 @; t: @, |% w% f" _+ X8 n
0 Z5 n5 `* }7 T5 t-p (Stand-Alone C++).( A' }# ~. F3 M. Z' z* K2 S
mcc -p mymfile8 D1 v2 z S# {* B0 G+ ]; } {- T
The -p option is equivalent to the series of options ( H- [" T2 e1 }2 ^ o% P. ~/ b, I- ~! v-W main -L Cpp -t -T link:exe -h libmmfile.mlib 6 H D/ |: A- k. T* \0 Mor ) ?. c8 I$ }( }
-B macro_option_p& z7 R" m% S4 `5 b1 w6 Y" U
: Y9 H* P: g% K1 D. @$ l/ D-S (Simulink S-Function).6 |% B, x; ?3 \
mcc -S mymfile " i* `" g* S6 M) }The -S option is equivalent to the series of options ( F0 x) {6 h5 n0 i/ q: w-W simulink -L C -t -T link:mex libmatlbmx.mlib ) X* Q a* K$ m+ Uor , M1 D" ], r6 w; F* O
-B macro_option_s " u6 N- z* l1 G3 g! U# D- n" b % s* X0 Y% F$ x' m0 W n# F6 \' Q-x (MEX-Function). & Y& }- U5 B" \. G& {3 V* ?# zmcc -x mymfile " m' u' h) n! V1 |The -x option is equivalent to the series of options , B5 U* _5 ^% J4 E* B) r-W mex -L C -t -T link:mexlibrary libmatlbmx.mlib7 G) x6 c5 j+ f, B9 F* S; D
or $ u7 g; r- c+ v
-B macro_option_x : S7 y' x& Z' J: s5 ^9 K( X6 W 1 B% @' @) W4 z' o/ `6 Y! T " Y+ t7 {" L- l, c, Q1 q 9 \$ r6 l) @* _% b: W/ t( c/ ]5. Bundle Files3 b7 ?! e8 q0 L
This table shows the available bundle files. 1 E( k6 o( p7 q4 [0 \
7 B9 _& T4 b! D
( _3 a% V/ E: N' D8 s
: ~8 N" S# e- S/ f6.Examples; ]5 ^* j% [9 F5 G) L
Make a C translation and a MEX-file for myfun.m: 0 x1 z7 A. d$ B" I5 _
mcc -x myfun: O* U8 r; b4 t, o6 k2 t6 O
Make a C translation and a stand-alone executable for myfun.m: ( Z# X! M7 b+ G( c2 g+ @* wmcc -m myfun % P% B' A9 C6 ], |( [Make a C++ translation and a stand-alone executable for myfun.m: * A# l; I7 l Umcc -p myfun * U6 X; F) B. V( z' I, H" H- b3 wMake a C translation and a Simulink S-function for myfun.m (using dynamically sized inputs and outputs): 8 z; c: ], v: A" T. y3 mmcc -S myfun : s, H& Y, Q5 Z, E7 m" o8 u2 qMake a C translation and a Simulink S-function for myfun.m (explicitly calling for one input and two outputs): . `( ~6 a! l& b( @7 w1 ~7 Tmcc -S -u 1 -y 2 myfun1 n5 s1 G7 {2 N4 m' w