Convert MATLAB code to C++ code

조회 수: 4 (최근 30일)
deep
deep 2012년 4월 14일
I have a simple function in M-function file which I want to convert to C++.
After following all the steps mentioned in the Documentation, I got the .cpp file ... However when I run this file in Visual studio, I got the following error: "error LNK1561: entry point must be defined" since the code doesn't have int main() at the beginning.
This is the generated c++ code:
/* * emcrand.cpp * * Embedded MATLAB Coder code generation for function 'emcrand' * * C source code generated on: Sat Apr 14 17:24:57 2012 * */
/* Include files */ #include "rt_nonfinite.h" #include "emcrand.h" #include "rand.h"
/* Type Definitions */
/* Named Constants */
/* Variable Declarations */
/* Variable Definitions */
/* Function Declarations */
/* Function Definitions */ real_T emcrand(void) { return m_rand(); }
/* End of Embedded MATLAB Coder code generation (emcrand.cpp) */
Would you please help finding what and where possibly could be the error ? Why the .cpp file doesn't have the int main() as it should be ?
Thanks.

채택된 답변

Rick Rosson
Rick Rosson 2012년 4월 14일
MATLAB Coder does not generate a main() function for you. You have to write this function yourself in C or C++ and include it in the parent project before compiling the generated code.
  댓글 수: 1
deep
deep 2012년 4월 15일
Thanks a lot.
I wrote the main function in C++ as you suggested. I called the generated code from this main function as follows:
/*
** main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "emcrand.h"
#include "emcrand_initialize.h"
#include "emcrand_terminate.h"
int main()
{
emcrand_initialize();
printf("emcrand=%g\n", emcrand());
emcrand_terminate();
return 0;
}
When I run the above code, unfortunately I have the following errors:
1. error LNK2001: unresolved external symbol "unsigned int v4_state" (?v4_state@@3IA) file: emcrand_initialize.obj
2. I receive also errors: LNK2019 and LNK1120
I think all the errors are related but I have no idea how to resolve them.
Please Help me !!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by