简体   繁体   English

C编程多定义错误

[英]C programming multiple definition error

I am using Eclipse for C programming. 我正在使用Eclipse进行C编程。

While building the program I am getting a multiple definition of 'main' error. 在构建程序时,我得到了multiple definition of 'main'错误的multiple definition of 'main'

I have multiple source files in the same project, so why I am getting a multiple definition error, how can I avoid it? 我在同一个项目中有多个源文件,那么为什么会出现multiple definition错误,如何避免呢?

Is it correct to have more than one source file in the same project? 同一项目中有多个源文件是否正确?

The error: 错误:

multiple definition of main'` error.

Tells that: 告诉:
You have more than one function named main() defined across your multiple files. 您在多个文件中定义了多个名为main()函数。
Remove the redundant main() function and keep only one. 删除多余的main()函数,仅保留一个。
Defining multiple definitions for a function breaks the One Definition Rule and hence the error. 为一个函数定义多个定义会破坏一个定义规则,从而导致错误。

Yes, it's correct to have multiple files in the same project. 是的,在同一项目中有多个文件是正确的。 Almost all non-trivial C projects have multiple files. 几乎所有不平凡的C项目都有多个文件。 If you are getting the "multiple definitions of main" at the link time, that means the main function was defined in more than one link objects. 如果在链接时获得“ main的多个定义”,则意味着main函数在多个链接对象中定义。 That can happen if 如果发生这种情况

  1. you actually have multiple mains defined, 您实际上定义了多个电源,
  2. you are linking to a library that has main defined 您正在链接到具有主要定义的库
  3. you have multiple files that #include a file with main. 您有多个包含#main文件的文件。

    #include of a .c file is normally not what you want to do. .c文件的#include通常不是您想要执行的操作。 If you want to use functions defined in other .c files, you generally want to create header files (.h) that have prototypes of the functions. 如果要使用在其他.c文件中定义的函数,则通常需要创建具有函数原型的头文件(.h)。

检查如何使用EclipseEclipse中定义和使用构建配置来切换main()的主要错误的多个定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM