多个定义?

Multiple definitions?

我在编译 flex 和 bison 代码时遇到问题。更具体地说是我的 parser.yy 文件。在这个文件中,我包含了 MathCalc.h 和 BaseProg.h,它们是我制作的 类。问题是当我实例化 类 时,它在编译时给我一个 "multiple definition" 错误。任何帮助,将不胜感激!谢谢!!

Parser.yy(片段):

%code requires {
#include <iostream>
#include <cmath>
#include "MathCalc.h"
#include "BaseProg.h"
/* Parser error reporting routine */
void yyerror(const char *msg);

/* Scannar routine defined by Flex */
int yylex();

using namespace std;
BaseProg bprog;
MathCalc calc;

enum Type { INT, FLT};
}
/* yylval union type */
%union {
        double dval;
        int ival;
        char* name;
        Type type;
}

错误:

bison -d parser.yy
g++    -c -o scanner.o scanner.cc
g++    -c -o parser.tab.o parser.tab.cc
g++ scanner.o parser.tab.o BaseProg.o MathCalc.o -lfl -o ../Proj2
parser.tab.o:(.bss+0x0): multiple definition of `bprog'
scanner.o:(.bss+0x28): first defined here
parser.tab.o:(.bss+0x1): multiple definition of `calc'
scanner.o:(.bss+0x29): first defined here
collect2: ld returned 1 exit status

%code requires 块中的任何代码都将被放置在解析器源文件和解析器头文件中。 #include scanner source中的parser头文件是正常的(毕竟bison是这样生成头文件的),所以把全局变量定义放在%code requires块中是不明智的。

的确,将全局变量定义放在头文件中总是不明智的,正是因为头文件很可能包含在多个源文件中,结果是任何全局定义(与声明相对) ) 将被插入到多个翻译单元中,这违反了 ODR。

对于头文件,您应该将这些对象(BaseProg bprog;MathCalc calc;)标记为extern,然后确保您确实在某个源文件中定义了它们。或者,更好的是,您应该首先避免使用全局变量。