“多重定义"、“这里首先定义"错误 [英] "Multiple definition", "first defined here" errors

查看:36
本文介绍了“多重定义"、“这里首先定义"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个项目:ServerClientCommons.制作标题和Commons 中的源对不会造成任何问题,我可以从 ServerClient 自由访问这些功能.

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.

但是,出于某种原因,在 ServerClient 项目中制作额外的源/头文件总是会导致 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(){

}

ma​​in.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

这篇关于“多重定义"、“这里首先定义"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆