gpt4 book ai didi

C包含错误多重定义错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:44 24 4
gpt4 key购买 nike

我遇到了一个经典错误,但仍然不明白为什么会发生...

下面是简单的解释显然我有两个 C 文件 main.c 和 support.c

在 support.c 中我有一个函数 void bla(int input);

在 main.c 中我有几个函数使用来自 support.c 的 bla,我包括了

#include<support.c> 

在 main.c 的顶部

但是我无法编译项目,因为错误 multiple definition of bla, first defined here (eclipse points to the definition of bla in support.c)

我知道最好我必须创建头文件 support.h 并在那里给出原型(prototype) extern void bla(int input),但为此我必须包含 .c 文件。

提前致谢。

最佳答案

预处理器将复制 support.c 的内容,并将其粘贴到 main.c 以替换 #include<support.c> 行。所以函数 bla 有两个定义,一个在 support.c ,另一个在 main.c

解决方案是,不要包含源文件。将要导出的函数的声明放在头文件 support.h 中,并将头文件包含在 main.c 中:

#include "support.h"

关于C包含错误多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19762989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com