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
这篇关于“多重定义"、“这里首先定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“多重定义"、“这里首先定义"错误
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01