C语言 一个函数的多重定义,这里先定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15983256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:01:35  来源:igfitidea点击:

Multiple definitions of a function, first defined here

cdefinition

提问by user1743798

I'm working on a scanner/parser for a university project, and I've run into this error:

我正在为大学项目开发​​扫描仪/解析器,但遇到了以下错误:

stack.c 91: multiple definition of '[all functions in stack.c]'
stack.c 91: first defined here

stack.c 91:'[stack.c 中的所有函数]'的多重定义
stack.c 91:首先在此处定义

For some reason that I can't seem to figure out the compiler seems to be telling me that I've defined the same function multiple times in the same place.

出于某种原因,我似乎无法弄清楚编译器似乎告诉我我在同一个地方多次定义了同一个函数。

I was given some skeleton code and a makefile to start off with so there must be some sort of incorrect communication between the files I wrote entirely myself (stack.c, stack.h) and the rest of the code.

我得到了一些框架代码和一个 makefile 作为开始,所以我完全自己编写的文件(stack.c,stack.h)和其余代码之间一定存在某种不正确的通信。

I've been moving around #includes and messing with the makefile for over an hour now and just running into more and more issues. How should this be set up?

我一直在#includes 周围移动并弄乱了makefile 一个多小时,只是遇到了越来越多的问题。这个应该怎么设置?

stack.cand stack.hare small files I wrote to be used in symtab.cand symtab.h.

stack.cstack.h是我写的小文件,用于symtab.csymtab.h.

回答by jxh

In symtab.h, you are #includeing the source file stack.c. Don't do that, and that multiple definition problem should go away.

在 中symtab.h,您正在#include访问源文件stack.c。不要那样做,多重定义问题应该会消失。

回答by user1743798

I finally got things to (seemingly) work. As user315052 I removed all #includes from .h files and put them in .c files instead. I was then receiving errors from symtab.h about types defined in stack.h so I #included stack.h in symtab.h and it seems to now work (or at least compile).

我终于让事情(似乎)起作用了。作为 user315052,我从 .h 文件中删除了所有 #includes 并将它们放在 .c 文件中。然后我从 symtab.h 收到关于 stack.h 中定义的类型的错误,所以我在 symtab.h 中 #included stack.h 并且它现在似乎可以工作(或至少可以编译)。