C/C++ 中的两个“主要”函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1990932/
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-08-27 21:46:38  来源:igfitidea点击:

Two 'main' functions in C/C++

c++c

提问by kuppusamy

Can I write a program in C or in C++ with two main functions?

我可以用 C 或 C++ 编写具有两个主要功能的程序吗?

回答by Uri

No. All programs have a single main(), that's how the compiler and linker generate an executable that start somewhere sensible.

不。所有程序都有一个 main(),这就是编译器和链接器生成从某个合理位置开始的可执行文件的方式。

You basically have two options:

你基本上有两个选择:

  1. Have the main() interpret some command line arguments to decide what actual main to call. The drawback is that you are going to have an executable with both programs.

  2. Create a library out of the shared code and compile each main file against that library. You'll end up with two executables.

  1. 让 main() 解释一些命令行参数来决定要调用的实际 main。缺点是您将拥有两个程序的可执行文件。

  2. 从共享代码中创建一个库,并针对该库编译每个主文件。你最终会得到两个可执行文件。

回答by Johannes Schaub - litb

You can have two functions called main. The nameis not special in any way and it's not reserved. What's special is the function, and it happens to have that name. The function is global. So if you write a main function in some other namespace, you will have a second mainfunction.

您可以将两个函数称为main. 这个名字没有任何特别之处,也没有保留。特殊的是function,它恰好有这个名字。该功能是全局的。因此,如果您在其他命名空间中编写 main 函数,您将拥有第二个main函数。

namespace kuppusamy {
  int main() { return 0; } 
}

int main() { kuppusamy::main(); }

The first mainfunction is not special - notice how you have to returnexplicitly.

第一个main函数并不特别 - 注意你必须如何return明确。

回答by Johannes Schaub - litb

Yes; however, it's platform specificinstead of standard C, and if you ask about what you really want to achieve (instead of this attempted solution to that problem), then you'll likely receive answers which are more helpful for you.

是的;但是,它是特定平台的,而不是标准 C,如果您询问您真正想要实现的目标(而不是尝试解决该问题的方法),那么您可能会收到对您更有帮助的答案。

回答by Mike

No, a program can have just 1 entry point(which is main()). In fact, more generally, you can only have one function of a given name in C.

不,一个程序只能有 1 个入口点(即main())。事实上,更一般地说,在 C 中你只能有一个给定名称的函数。

回答by Prasoon Saurav

No, main()defines the entry point to your program and you must only one main()function(entry point) in your program.

main()定义程序的入口点,并且您的程序中必须只有一个main()函数(入口点)。

Frankly speaking your question doesn't make much sense to me.

坦率地说,你的问题对我来说没有多大意义。

回答by ntd

If one is staticand resides in a different source file I don't see any problem.

如果一个是static并且驻留在不同的源文件中,我看不出任何问题。

回答by Phong

In some very special architecture, you can. This is the case of the Cell Processorwhere you have a main program for the main processor (64-bit PowerPC Processors Element called PPE) and one or many main program for the 8 different co-processor (32-bit Synergistic Processing Element called SPE).

在一些非常特殊的架构中,你可以。这是Cell Processor的情况,其中您有一个用于主处理器的主程序(称为 PPE 的 64 位 PowerPC 处理器元素)和用于 8 个不同协处理器(称为 SPE 的 32 位协同处理元素)的一个或多个主程序)。

回答by tgiphil

What do you mean by "main function"? If you mean the first function to execute when the program starts, then you can have only one. (You can only have one first!)

“主要功能”是什么意思?如果您的意思是程序启动时要执行的第一个函数,那么您只能有一个。(你只能先拥有一个!)

If you want to have your application do different things on start up, you can write a main function which reads the command line (for example) and then decides which other function to call.

如果你想让你的应用程序在启动时做不同的事情,你可以编写一个 main 函数来读取命令行(例如),然后决定调用哪个其他函数。

回答by Software Guy

No, you cannot have more than one main() function in C language. In standard C language, the main() function is a special function that is defined as the entry point of the program. There cannot be more than one copy of ANY function you create in C language, or in any other language for that matter - unless you specify different signatures. But in case of main(), i think you got no choice ;)

不,C 语言中不能有多个 main() 函数。在标准 C 语言中,main() 函数是一个特殊的函数,它被定义为程序的入口点。您用 C 语言或任何其他语言创建的任何函数的副本不能超过一个副本 - 除非您指定不同的签名。但在 main() 的情况下,我认为你别无选择;)

回答by William

No,The main() is the entry point to your program,since u can't have two entry points you cant have two main().

不,main() 是程序的入口点,因为你不能有两个入口点,所以你不能有两个 main()。