使用 gcc 编译时出现对“WinMain@16”错误的未定义引用
Undefined reference to `WinMain@16' error when compile with gcc
我要运行一个用 Flex 和 Bison 构建的简单程序。
这是我的代码。
Flex 文件 (calc.l)
%{
#include <stdio.h>
#include <stdlib.h>
#include "tt.tab.h"
%}
/* %option noyywrap */
%%
[0-9]+(\.[0-9]+)?([eE][0-9]+)? {yylval.f = atof(yytext); return NUM;}
[-+()*/] {return yytext[0];}
[\t\f\v\n] {;}
%%
int yywrap() {return -1}
野牛文件(tt.y)
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
void yyerror(char *msg);
%}
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
%}
%union {
float f;
}
%token <f> NUM
%type <f> E T F
%%
S : E {printf("f\n", );}
;
E : E '+' T {$$ = + }
| T {$$ = ;}
;
T : T '*' F {$$ = * }
| F {$$ = ;}
;
F : '(' E ')' {$$ = ;}
| '-' F {$$ = -;}
| NUM {$$ = ;}
%%
void yyerror(char *msg) {
fprintf(stderr, "%s\n", msg);
exit(1);
}
为了编译,我按照 cmd 中的命令进行编译。
Bison -d tt.y
弹性 calc.l
gcc lex.yy.c tt.tab.c -o calc
当我 运行 最后一个 gcc 命令时,出现了这样的错误。
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../libmingw32.a(main.o):(.text.startup+0xb0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
我的代码或任何代码有什么问题。感谢您的考虑:)
你没有定义main函数,flex和bison生成的代码没有定义main函数,你要自己定义,否则程序没有入口点。
您的定义有问题:
- in tt.y
{printf("f\n", );}
的格式与参数不兼容,必须是``{printf("%f\n", $1);}` ?
- in tt.y declare yyerror in the header with the declaration of yylex
- 在calc.l中
int yywrap() {return -1}
必须是int yywrap() {return -1;}
最小的 main 定义是:
int main()
{
yyparse();
return 0;
}
你可以把它和yyerror的定义放在tt.y
中
我要运行一个用 Flex 和 Bison 构建的简单程序。
这是我的代码。
Flex 文件 (calc.l)
%{
#include <stdio.h>
#include <stdlib.h>
#include "tt.tab.h"
%}
/* %option noyywrap */
%%
[0-9]+(\.[0-9]+)?([eE][0-9]+)? {yylval.f = atof(yytext); return NUM;}
[-+()*/] {return yytext[0];}
[\t\f\v\n] {;}
%%
int yywrap() {return -1}
野牛文件(tt.y)
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
void yyerror(char *msg);
%}
%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
%}
%union {
float f;
}
%token <f> NUM
%type <f> E T F
%%
S : E {printf("f\n", );}
;
E : E '+' T {$$ = + }
| T {$$ = ;}
;
T : T '*' F {$$ = * }
| F {$$ = ;}
;
F : '(' E ')' {$$ = ;}
| '-' F {$$ = -;}
| NUM {$$ = ;}
%%
void yyerror(char *msg) {
fprintf(stderr, "%s\n", msg);
exit(1);
}
为了编译,我按照 cmd 中的命令进行编译。
Bison -d tt.y
弹性 calc.l
gcc lex.yy.c tt.tab.c -o calc
当我 运行 最后一个 gcc 命令时,出现了这样的错误。
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../libmingw32.a(main.o):(.text.startup+0xb0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
我的代码或任何代码有什么问题。感谢您的考虑:)
你没有定义main函数,flex和bison生成的代码没有定义main函数,你要自己定义,否则程序没有入口点。
您的定义有问题:
- in tt.y
{printf("f\n", );}
的格式与参数不兼容,必须是``{printf("%f\n", $1);}` ? - in tt.y declare yyerror in the header with the declaration of yylex
- 在calc.l中
int yywrap() {return -1}
必须是int yywrap() {return -1;}
最小的 main 定义是:
int main()
{
yyparse();
return 0;
}
你可以把它和yyerror的定义放在tt.y
中