quot;Multiple definitionquot;, quot;first defined herequot; errors(“多重定义、“这里首先定义错误)
问题描述
我有 3 个项目:Server、Client 和 Commons.制作标题和Commons 中的源对不会造成任何问题,我可以从 Server 和 Client 自由访问这些功能.
I have 3 projects: Server, Client and Commons. Making header & source pairs in Commons doesn't cause any problems and I can access the functions freely from both Server and Client.
但是,出于某种原因,在 Server 或 Client 项目中制作额外的源/头文件总是会导致 multiple definition of (...)
和 first defined here
错误.
However, for some reason making additional source/header files within Server or Client project always causes multiple definition of (...)
and first defined here
errors.
例子:
commands.h(在 Client 项目的根目录中)
commands.h (in root dir of the Client project)
#ifndef COMMANDS_H_
#define COMMANDS_H_
#include "commands.c"
void f123();
#endif /* COMMANDS_H_ */
commands.c(在 Client 项目的根目录中)
commands.c (in root dir of the Client project)
void f123(){
}
main.c(在 Client 项目的根目录中)
main.c (in root dir of the Client project)
#include "commands.h"
int main(int argc, char** argv){
}
错误:
make: *** [Client] Error 1 Client
first defined here Client
multiple definition of `f123' commands.c
清理、重建索引、重建项目都没有帮助.重启电脑也不行.
Cleaning, rebuilding index, rebuilding projects doesn't help. Neither does restarting the computer.
推荐答案
这里的问题是你在函数原型之前的commands.h
中包含了commands.c
.因此,C预处理器将commands.c
的内容插入到commands.h
函数原型之前.commands.c
包含函数定义.结果,函数定义在导致错误的函数声明之前结束.
The problem here is that you are including commands.c
in commands.h
before the function prototype. Therefore, the C pre-processor inserts the content of commands.c
into commands.h
before the function prototype. commands.c
contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
预处理阶段后commands.h
的内容如下:
The content of commands.h
after the pre-processor phase looks like this:
#ifndef COMMANDS_H_
#define COMMANDS_H_
// function definition
void f123(){
}
// function declaration
void f123();
#endif /* COMMANDS_H_ */
这是一个错误,因为您无法在 C 中定义函数后声明该函数.如果您交换 #include "commands.c"
和函数声明,则不会发生错误,因为,现在,函数原型出现在函数声明之前.
This is an error because you can't declare a function after its definition in C. If you swapped #include "commands.c"
and the function declaration the error shouldn't happen because, now, the function prototype comes before the function declaration.
但是,包含 .c
文件是一种不好的做法,应该避免.此问题的更好解决方案是在 commands.c
中包含 commands.h
并将命令的编译版本链接到主文件.例如:
However, including a .c
file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h
in commands.c
and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_
#define COMMANDS_H_
void f123(); // function declaration
#endif
commands.c
#include "commands.h"
void f123(){} // function definition
这篇关于“多重定义"、“这里首先定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“多重定义"、“这里首先定义"错误


- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01