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

时间:2021-10-30 00:06:24

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

我可以用C或c++写一个程序,有两个主要的函数吗?

15 个解决方案

#1


27  

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.

    让main()解释一些命令行参数,以决定调用的实际主要内容。缺点是两个程序都有可执行文件。

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

    从共享代码中创建一个库,并针对该库编译每个主文件。您将得到两个可执行文件。

#2


13  

Yes; however, it's platform specific instead 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,如果您询问您真正想要实现什么(而不是尝试解决这个问题的解决方案),那么您很可能会得到对您更有帮助的答案。

#3


13  

You can have two functions called main. The name is 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 main function.

可以有两个函数叫做main。这个名字在任何方面都不特别,也不保留。函数的特殊之处在于,它恰好有这个名字。函数是全局的。如果你在其他命名空间中写一个主函数,你会有第二个主函数。

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

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

The first main function is not special - notice how you have to return explicitly.

第一个主要功能并不特别——注意如何显式返回。

#4


7  

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.

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

#5


4  

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.

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

#6


3  

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

如果其中一个是静态的,并且驻留在另一个源文件中,我没有看到任何问题。

#7


2  

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.

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

#8


2  

In some very special architecture, you can. This is the case of the Cell Processor where 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).

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

#9


1  

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(),我认为您别无选择;

#10


0  

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()。

#11


0  

You can write it, and it'll compile, but it won't link (unless your linker is non-comformant)

您可以编写它,它将编译,但是它不会链接(除非您的链接器不符合)

#12


0  

The idiom is to dispatch on the value of argv[0]. With hardlinks (POSIX) you don't even lose diskspace.

这个成语用来形容argv[0]的值。使用硬链接(POSIX),您甚至不会丢失磁盘空间。

#13


-1  

This might be the answer for your query

这可能是您的查询的答案

void main()
{
    func1();
}
#define main func1
void main()
{
    printf("inside 2nd main");
}

#14


-1  

Not in C, C++

不是在C,c++

but now C#.net introduce trick you can use 2 mains in one application. i implemented in my application too. I have scenario and requirement and implemented it successfully.

但是现在c#。net引入了一个技巧,你可以在一个应用程序中使用2个mains。我在我的应用程序中也实现了。我有场景和需求,并成功实现了它。

reference and proof is here https://www.youtube.com/watch?v=KJcBj3hLIpM

参考和证明如下:https://www.youtube.com/watch?v=KJcBj3hLIpM

#15


-1  

Yes , Multiple main() are allowed but not in global namespace.

是的,允许使用多个main(),但不允许在全局命名空间中使用。

"Every C++ program must have exactly one global function named main()" - Bjarne stroustrup.

“每个c++程序必须有一个名为main()的全局函数。”——内定。

Eg 1 :

例如1:

namespace foo
{
  int main() //Main 1 
  {
    return 1;
  }
}

int main() // Main 2   
{

}

// Main 1 : namespace is foo

// Main 2 : namespace is global

// Allowed : Yes , its allowed as both the main() are present in different namespaces.

Eg 2 :

例如2:

int main() //Main 1 
{
    return 1;
}

void main() // Main 2   
{

}

// Main 1 : namespace is global

// Main 2 : namespace is global

// Allowed : No , as multiple main() in global namespace . Compile-time error is thrown : redefinition of main() .. 

#1


27  

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.

    让main()解释一些命令行参数,以决定调用的实际主要内容。缺点是两个程序都有可执行文件。

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

    从共享代码中创建一个库,并针对该库编译每个主文件。您将得到两个可执行文件。

#2


13  

Yes; however, it's platform specific instead 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,如果您询问您真正想要实现什么(而不是尝试解决这个问题的解决方案),那么您很可能会得到对您更有帮助的答案。

#3


13  

You can have two functions called main. The name is 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 main function.

可以有两个函数叫做main。这个名字在任何方面都不特别,也不保留。函数的特殊之处在于,它恰好有这个名字。函数是全局的。如果你在其他命名空间中写一个主函数,你会有第二个主函数。

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

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

The first main function is not special - notice how you have to return explicitly.

第一个主要功能并不特别——注意如何显式返回。

#4


7  

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.

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

#5


4  

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.

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

#6


3  

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

如果其中一个是静态的,并且驻留在另一个源文件中,我没有看到任何问题。

#7


2  

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.

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

#8


2  

In some very special architecture, you can. This is the case of the Cell Processor where 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).

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

#9


1  

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(),我认为您别无选择;

#10


0  

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()。

#11


0  

You can write it, and it'll compile, but it won't link (unless your linker is non-comformant)

您可以编写它,它将编译,但是它不会链接(除非您的链接器不符合)

#12


0  

The idiom is to dispatch on the value of argv[0]. With hardlinks (POSIX) you don't even lose diskspace.

这个成语用来形容argv[0]的值。使用硬链接(POSIX),您甚至不会丢失磁盘空间。

#13


-1  

This might be the answer for your query

这可能是您的查询的答案

void main()
{
    func1();
}
#define main func1
void main()
{
    printf("inside 2nd main");
}

#14


-1  

Not in C, C++

不是在C,c++

but now C#.net introduce trick you can use 2 mains in one application. i implemented in my application too. I have scenario and requirement and implemented it successfully.

但是现在c#。net引入了一个技巧,你可以在一个应用程序中使用2个mains。我在我的应用程序中也实现了。我有场景和需求,并成功实现了它。

reference and proof is here https://www.youtube.com/watch?v=KJcBj3hLIpM

参考和证明如下:https://www.youtube.com/watch?v=KJcBj3hLIpM

#15


-1  

Yes , Multiple main() are allowed but not in global namespace.

是的,允许使用多个main(),但不允许在全局命名空间中使用。

"Every C++ program must have exactly one global function named main()" - Bjarne stroustrup.

“每个c++程序必须有一个名为main()的全局函数。”——内定。

Eg 1 :

例如1:

namespace foo
{
  int main() //Main 1 
  {
    return 1;
  }
}

int main() // Main 2   
{

}

// Main 1 : namespace is foo

// Main 2 : namespace is global

// Allowed : Yes , its allowed as both the main() are present in different namespaces.

Eg 2 :

例如2:

int main() //Main 1 
{
    return 1;
}

void main() // Main 2   
{

}

// Main 1 : namespace is global

// Main 2 : namespace is global

// Allowed : No , as multiple main() in global namespace . Compile-time error is thrown : redefinition of main() ..