I got the error: undefined reference to WinMain when converting a simple matlab function to a c executable
조회 수: 29 (최근 30일)
이전 댓글 표시
Using coder to generate c executable result in an error. Here is part of the Matlab verbose
C:/crossdev/src/etc.. undefined reference to WinMain collect2.exe: error: 1d returned 1 exit status gmake: * [c:/temp/prodtest.exe] error 1 the make command returned an error of 2 An_error_occured_during_the_call_to_make is not recognized as an internal or external command, operable program or batch file
Error(s) encountered while building "prodtest' ### Failed to generate all binary outputs.
Strangely, Matlab is looking at c:/crossdev/src... but the crossdev folder does not exist on my machine. In short, Matlab is able to generate the c++ code but not the executable
댓글 수: 4
rezvan rafiee alavi
2020년 3월 27일
Ho did you create and use main.c file? because I made one but still I get the same error message
답변 (6개)
Tom Courtney
2022년 10월 31일
Hi as Quinghua said you should generate the C/C++ code first and then generate the exe. BUT before you generate the exe I think you need to make sure your setting will compile the main.c/main.h files in the examples folder.
- Go to "more settings" under the generation step of the coder app
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1175503/image.png)
2. Search "main" under "all settings"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1175508/image.png)
3. Under "generate example main" set the value to "generate and compile an example main function" (see image above)
댓글 수: 1
upol
2018년 12월 27일
편집: Walter Roberson
2018년 12월 28일
I am trying to convert this simple code into excutable using matlab coder.
function y = hello_world
%#codegen
y = 'Hello World!';
converting to source code C works but when i change the build type to Executable
It gives me this error:
Build error: C compiler produced errors. See the Build Log for further details.
C:/PROGRA~3/MATLAB/SUPPOR~1/R2018b/3P778C~1.INS/MINGW_~1.INS/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/lib/../lib\libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
gmake: *** [C:/dummy/hello_world.exe] Error 1
The make command returned an error of 2
'An_error_occurred_during_the_call_to_make' is not recognized as an internal or external command,
operable program or batch file.
Error(s) encountered while building "hello_world":
### Failed to generate all binary outputs.
댓글 수: 1
Ben Knight
2018년 12월 28일
same problem here. "undefined reference to `WinMain". I can create the C files, but not an executable. I am on 2017b... not suppose to be a problem. I installed via the add-on manager in MATLAB without errors.
Here are some responses from the command window that I get:
mex -setup
ans = MEX configured to use 'MinGW64 Compiler (C)' for C language compilation.
getenv('MW_MINGW64_LOC')
ans = 'C:\ProgramData\MATLAB\SupportPackages\R2017b\3P.instrset\mingw_w64.instrset'
upol
2019년 1월 10일
Why this gives problem in C Coder. Error: Directly accessing field or property of nonscalar struct or object not supported for code generation.
s1=string({OPS_FLT(:).ACFT_ID})
s2=OPS_FLT(2).ACFT_ID
uuindex=find(strcmpi(s1,s2))
({OPS_FLT(:).ACFT_ID}) has already been defined as
OPS_FLT(1).ACFT_ID="apple"
OPS_FLT(2).ACFT_ID="orange"
I am trying to find orange from the array. It works in Matlab but not in C Coder
댓글 수: 0
Qinghua Peng
2021년 8월 14일
most probably, you shoud RTFM cited by @upol
according to the manual, you should generate code file first, then generated a main.c and main.h that satisfied your apps based on the generated example/main.c and example/main.h
finally, you should run the prj file again, add Additional source files and Additional include directories, Generate.
댓글 수: 0
Ayush
2024년 12월 2일
Although I do not have access to your file but I was also getting the same error when using 'mexFuntion' as entry point.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <engine.h>
#include <math.h>
#include <time.h>
#include <float.h>
#define VRAI 1
#define FAUX 0
typedef char BOOLEEN;
// mexFunction used as an entry point
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
Engine *ep;
if (!(ep = engOpen(NULL))) {
exit(-1);
}
// rest of the script
}
It should be have been written with 'main' or 'WinMain' for an engine application like this. See for example the MATLAB example file:
copyfile(fullfile(matlabroot,'extern','examples','eng_mat','engwindemo.c'),'.','f')
which uses 'WinMain' as the entry point.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Hamamatsu Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!