Flex/Bison - 我该如何解决 "Multiple definition of `symtab'"
Flex/Bison - How can I resolvev "Multiple definition of `symtab'"
我重新创建了 advanced calculator from the book "flex & bison" 并对 C++ 做了一些小的改编。但是我无法解决的是我在编译时出现以下错误。
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: multiple definition of 'symtab'
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: first defined here
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: multiple definition of 'symtab'
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: first defined here
collect2.exe: error: ld returned 1 exit status
我在声明 symtab 的 .hpp-file 中有一个 header 守卫,但它说它有多个定义。我已经将代码上传到 github-repo。有人可以帮助我了解我做错了什么以及如何解决我的问题吗?
谢谢!
问题是您在 header 文件中定义了变量 symbol symtab[NHASH]
。每个包含此 header 的源文件都将定义该符号。由于您所有的源文件都包含此 header 文件,您将对同一个 object 有多个定义,因此违反了 ODR(一个定义规则),因此出现错误消息。
我看到 symtab
只在 calcfunction.cpp 中使用,你能把它移到那个文件吗?否则,您可以通过将其标记为外部来确保仅在 header 中声明它(查看此答案:How do I use extern to share variables between source files?)。
我重新创建了 advanced calculator from the book "flex & bison" 并对 C++ 做了一些小的改编。但是我无法解决的是我在编译时出现以下错误。
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: multiple definition of 'symtab'
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: first defined here
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: multiple definition of 'symtab'
C:\..\BisonFlexCalculator/Includes/calc.hpp:17: first defined here
collect2.exe: error: ld returned 1 exit status
我在声明 symtab 的 .hpp-file 中有一个 header 守卫,但它说它有多个定义。我已经将代码上传到 github-repo。有人可以帮助我了解我做错了什么以及如何解决我的问题吗?
谢谢!
问题是您在 header 文件中定义了变量 symbol symtab[NHASH]
。每个包含此 header 的源文件都将定义该符号。由于您所有的源文件都包含此 header 文件,您将对同一个 object 有多个定义,因此违反了 ODR(一个定义规则),因此出现错误消息。
我看到 symtab
只在 calcfunction.cpp 中使用,你能把它移到那个文件吗?否则,您可以通过将其标记为外部来确保仅在 header 中声明它(查看此答案:How do I use extern to share variables between source files?)。