C语言 main() 的多重定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21508654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:43:14  来源:igfitidea点击:

Multiple definition of main()

cmainmultiple-definition-error

提问by jeevanreddymandali

Hi guys trying to use two main() and getting this error multiple definition of main(). I renamed my main functions then why is this error and also first defined here for my print(). header file:

大家好,尝试使用两个 main() 并得到 main() 的多个定义的错误。我重命名了我的主要函数,那么为什么会出现这个错误,并且首先在此处为我的 print() 定义。头文件:

#ifndef TOP_H_
#define TOP_H_

#include <stdio.h>
#include <string.h>
#define onemain main
#define twomain main
inline void print();


#endif /* TOP_H_ */

c file one:

c文件一:

#include "top.h"
void print();
int onemain()
{
    print();
    return 0;
}
void print()
{
printf("hello one");
}

c file two:

c文件二:

#include "top.h"
void print();
int twomain()
{
    print();
    return 0;
}
void print()
{
printf("hello two");
}

Error snapshot

错误快照

回答by kuroi neko

Basically any C (or even C++) program is a bunch of functions calling each other.
To begin a program execution, you have to pick one of these functions and call it first.
By convention, this initial function is called main.

基本上任何 C(甚至 C++)程序都是一堆相互调用的函数。
要开始执行程序,您必须选择其中一个函数并首先调用它。
按照惯例,这个初始函数被称为main

When you include several source files in a project, the IDE compiles them all, then calls the linker which looks for one single function called mainand generates an executable file that will call it.

当您在一个项目中包含多个源文件时,IDE 会将它们全部编译,然后调用链接器,该链接器查找一个被调用的函数main并生成一个可执行文件来调用它。

If, for any reason, you defined two "main" functions inside all these files, the linker will warn you that it cannot choose on its own which one you intended to be the starting point of your program.

如果出于任何原因,您在所有这些文件中定义了两个“主要”函数,链接器将警告您它无法自行选择您打算将哪个函数作为程序的起点。

回答by Potatoswatter

The macro substitution of onemainand twomainoccurs before the compiler proper sees the program, so that doesn't make a difference. The functions are both named main.

宏替换onemaintwomain发生编译器正确看到程序之前,这样就不会有所作为。这些函数都被命名为main

C++ allows different functions with the same name, but does not allow two definitions of the exact same function signature. There would be no way to form a function call expression that would reach either overload. Additionally, the functions are the same entity, and one thing cannot have two definitions.

C++ 允许具有相同名称的不同函数,但不允许完全相同的函数签名的两个定义。没有办法形成一个达到任一重载的函数调用表达式。另外,功能是同一个实体,一件事不能有两个定义。

In addition, in C++ maincannot be overloaded, because the program is supposed to start when the unique mainfunction is called, and any given system detects what format of mainthe particular program uses, out of a variety of allowed formats. (This auto-detection feature also applies to C.)

此外,在 C++main中不能重载,因为程序应该在main调用唯一函数时启动,并且任何给定的系统都会main从各种允许的格式中检测特定程序使用的格式。(此自动检测功能也适用于 C。)

But you are not asking about C++; in C, without function overloading, there are no redefinitions of the same name, even for different signatures. Each name of externlinkage in C uniquely identifies an entity, so you cannot have two.

但你不是在问 C++;在 C 中,如果没有函数重载,即使对于不同的签名,也不会重定义同名。externC 中的每个链接名称都唯一标识一个实体,因此不能有两个。

It's unclear what you want the resulting program to do. Most likely you need to build two separate programs.

目前尚不清楚您希望生成的程序做什么。您很可能需要构建两个单独的程序。

回答by glglgl

I don't get what you ask - your error messages are quite clear:

我不明白你问什么 - 你的错误信息很清楚:

  1. You have 2 definitions of print(), which will collide. Remove one.
  2. You as well have 2 definitions of main()- your #defines will replace your onemainand twomainfunctions, naming them effectively as main.
  1. 您有 2 个定义print(),它们会发生冲突。去掉一个。
  2. 您也有 2 个定义main()- 您的#defines 将替换您的onemaintwomain函数,将它们有效地命名为main.

回答by Oz123

You overrode the built in print, about main, try imagining a car with two steering wheels ... it wont work ...

你忽略了内置的print,关于main,试着想象一辆有两个方向盘的汽车......它行不通......

Your C program has two have a least one main, so the computer knows where the program starts. If you have 2 files with two mainfunction, then you have two different programs.

你的 C 程序有两个至少有一个main,所以计算机知道程序从哪里开始。如果您有两个具有两个main功能的文件,那么您就有两个不同的程序。

回答by samiam

It's not possible for a C program to have more than one main() in it. Also, main() should be declared as an intand return an integer value (usually 0).

一个 C 程序中不可能有多个 main()。此外,main() 应声明为 anint并返回一个整数值(通常为 0)。