目录
3 新建 C++工程
4 调用测试
1 matlab脚本 StrPrint.m
function [sum, mult, strs] = StrPrint(m, n)
strs = 'Happiness is not by chance, but by choice.';
sum = m + n;
mult = m*n;
2 生成c/c++代码
下一步直接过点next
新建 C++工程
添加现有项把相关的都先加进来,让程序先运行
在StrPrint.cpp中添加相应的头文件和主函数,运行结果如下
调用测试
/*
* StrPrint.cpp
*
* Code generation for function 'StrPrint'
*
*/
/* Include files */
#include "rt_nonfinite.h"
#include "StrPrint.h"
#include <iostream>
/* Function Definitions */
//测试用二级指针返回值
void StrPrintPointer( const char** str)
{
int i0;
static const char cv0[42] = { 'H', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's', ' ',
'i', 's', ' ', 'n', 'o', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'a', 'n', 'c',
'e', ',', ' ', 'b', 'u', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'o', 'i', 'c',
'e', '.' };
*str = cv0;
}
void StrPrint(int m, int n, int *sum, int *mult, char strs[42])
{
int i0;
static const char cv0[42] = { 'H', 'a', 'p', 'p', 'i', 'n', 'e', 's', 's', ' ',
'i', 's', ' ', 'n', 'o', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'a', 'n', 'c',
'e', ',', ' ', 'b', 'u', 't', ' ', 'b', 'y', ' ', 'c', 'h', 'o', 'i', 'c',
'e', '.' };
long long i1;
for (i0 = 0; i0 < 42; i0++) {
strs[i0] = cv0[i0];
}
if ((m < 0) && (n < MIN_int32_T - m)) {
*sum = MIN_int32_T;
}
else if ((m > 0) && (n > MAX_int32_T - m)) {
*sum = MAX_int32_T;
}
else {
*sum = m + n;
}
i1 = (long long)m * n;
if (i1 > 2147483647LL) {
i1 = 2147483647LL;
}
else {
if (i1 < -2147483648LL) {
i1 = -2147483648LL;
}
}
*mult = (int)i1;
}
/* End of code generation (StrPrint.cpp) */
void main()
{
int m = 10;
int n = 100;
int sum = 0;
int mult = 0;
char strs[50] = { 0 };
char* pstr = new char[42];//最后应该处理为‘\0’,现在没管有错误
const char* pstrs = NULL;
//StrPrint(int m, int n, int *sum, int *mult, char strs[42])
StrPrint(m, n, &sum, &mult, strs);
StrPrint(1, 20, &sum, &mult, pstr);
std::cout << "sum is " << sum << std::endl
<< "mult is " << mult << std::endl
<< "strs is " << std::endl << strs << std::endl;
std::cout << pstr << std::endl;
std::cout << "-------------------------------" << std::endl;
StrPrintPointer(&pstrs);
std::cout << pstrs<< std::endl;
}