未定义的引用 yyparse C++
Undefined reference yyparse c++
我正在使用 bison 和 flex 开发一个简单的解释器。当我编译我的代码时,我收到“未定义引用 'yyparse'”的错误。
mylang.ll
%{
/******************** C-libraries and Token definitions *****************/
#include <string.h> /* for strdup */
#include "mylang.yy.tab.h" /* for token definitions and yylval */
extern int yyparse(void);
%}
%option nounput yylineno
/******************** MY TOKEN Definitions *****************/
%%
int yywrap(void){}
mylang.yy
%{
/******************** C Libraries, Symbol Table, Code Generator & other C code *****************/
#include <stdio.h> /* For I/O */
#include <stdlib.h> /* For malloc here and in symbol table */
#include <string.h> /* For strcmp in symbol table */
#include "StackMachine.h" /* Stack Machine*/
#include "CodeGenerator.h" /* Code Generator*/
#include "SymbolTable.h" /* Symbol Table*/
extern "C" int yylex(void);
extern "C" void yyerror(const char *);
extern "C" int yylineno;
#include "IM.h" /* Identifier Making */
#define YYDEBUG 1 /* For Debugging*/
%}
/******************** SEMANTIC RECORDS *****************/
%union semrec /* The Semantic Records*/
{
double floatnum; /* Double values */
char *string; /* Identifiers*/
}
/******************** TOKENS ***************************/
%%
/******************** GRAMMAR RULES for the Simple language *******/
%%
/******************** YYERROR ********************************************************** */
void yyerror (const char *s ) /* Called by yyparse on error */
{
errors++;
printf ("Line Number : %d .. %s\n", yylineno,s);
}
mylang.cpp
#include "Headers.h"
extern "C" {
int yyparse(void);
}
/* ********************************** MAIN *****************************/
int main( int argc, char *argv[] )
{
extern FILE *yyin;
++argv; --argc;
yyin = fopen( argv[0], "r" );
errors = 0;
yyparse ();
/* Execute Code */
return 0;
}
我的 makefile 运行如下所示。
g++ -Os -g -std=c++0x -c SM.cpp
g++ -Os -g -std=c++0x -c mylang.cpp
bison -d mylang.yy
flex mylang.ll
g++ -Os -g -std=c++0x -c mylang.yy.tab.c
gcc -c lex.yy.c
g++ -Os -std=c++0x -o mylang mylang.yy.tab.o lex.yy.o SM.o mylang.o -lm
当我 运行 这段代码时,出现以下编译时错误。
mylang.cpp:31: undefined reference to `yyparse'
collect2: error: ld returned 1 exit status
make: *** [mylang] Error 1
这里在哪里定义yyparse函数?我在代码中犯了什么错误?
您正在使用 C++ 编译器编译解析器
g++ -Os -g -std=c++0x -c mylang.yy.tab.c
但是您将从该文件导出的解析器函数声明为 extern "C",从而导致链接器使用 C 命名约定查找符号 yyparse。
链接器搜索符号 _yyparse,而 C++ 编译器已使用 C++ 名称修改导出符号(将类型信息添加到名称中,以便可以导出所有可能的函数重载)并将其转换为类似 _Z3yyparsev 的内容.
删除 mylang.cpp 中 yyparse 声明周围的 extern "C" 应该可以解决问题。
我正在使用 bison 和 flex 开发一个简单的解释器。当我编译我的代码时,我收到“未定义引用 'yyparse'”的错误。
mylang.ll
%{
/******************** C-libraries and Token definitions *****************/
#include <string.h> /* for strdup */
#include "mylang.yy.tab.h" /* for token definitions and yylval */
extern int yyparse(void);
%}
%option nounput yylineno
/******************** MY TOKEN Definitions *****************/
%%
int yywrap(void){}
mylang.yy
%{
/******************** C Libraries, Symbol Table, Code Generator & other C code *****************/
#include <stdio.h> /* For I/O */
#include <stdlib.h> /* For malloc here and in symbol table */
#include <string.h> /* For strcmp in symbol table */
#include "StackMachine.h" /* Stack Machine*/
#include "CodeGenerator.h" /* Code Generator*/
#include "SymbolTable.h" /* Symbol Table*/
extern "C" int yylex(void);
extern "C" void yyerror(const char *);
extern "C" int yylineno;
#include "IM.h" /* Identifier Making */
#define YYDEBUG 1 /* For Debugging*/
%}
/******************** SEMANTIC RECORDS *****************/
%union semrec /* The Semantic Records*/
{
double floatnum; /* Double values */
char *string; /* Identifiers*/
}
/******************** TOKENS ***************************/
%%
/******************** GRAMMAR RULES for the Simple language *******/
%%
/******************** YYERROR ********************************************************** */
void yyerror (const char *s ) /* Called by yyparse on error */
{
errors++;
printf ("Line Number : %d .. %s\n", yylineno,s);
}
mylang.cpp
#include "Headers.h"
extern "C" {
int yyparse(void);
}
/* ********************************** MAIN *****************************/
int main( int argc, char *argv[] )
{
extern FILE *yyin;
++argv; --argc;
yyin = fopen( argv[0], "r" );
errors = 0;
yyparse ();
/* Execute Code */
return 0;
}
我的 makefile 运行如下所示。
g++ -Os -g -std=c++0x -c SM.cpp
g++ -Os -g -std=c++0x -c mylang.cpp
bison -d mylang.yy
flex mylang.ll
g++ -Os -g -std=c++0x -c mylang.yy.tab.c
gcc -c lex.yy.c
g++ -Os -std=c++0x -o mylang mylang.yy.tab.o lex.yy.o SM.o mylang.o -lm
当我 运行 这段代码时,出现以下编译时错误。
mylang.cpp:31: undefined reference to `yyparse'
collect2: error: ld returned 1 exit status
make: *** [mylang] Error 1
这里在哪里定义yyparse函数?我在代码中犯了什么错误?
您正在使用 C++ 编译器编译解析器
g++ -Os -g -std=c++0x -c mylang.yy.tab.c
但是您将从该文件导出的解析器函数声明为 extern "C",从而导致链接器使用 C 命名约定查找符号 yyparse。
链接器搜索符号 _yyparse,而 C++ 编译器已使用 C++ 名称修改导出符号(将类型信息添加到名称中,以便可以导出所有可能的函数重载)并将其转换为类似 _Z3yyparsev 的内容.
删除 mylang.cpp 中 yyparse 声明周围的 extern "C" 应该可以解决问题。