是否可以在不使用 main() 函数的情况下编写程序?

2023-10-17C/C++开发问题
10

本文介绍了是否可以在不使用 main() 函数的情况下编写程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我在面试中不断被问到这个问题:

I keep getting this question asked in interviews:

不使用main()函数编写程序?

Write a program without using main() function?

我的一个朋友向我展示了一些使用宏的代码,但我无法理解.

One of my friends showed me some code using Macros, but i could not understand it.

所以问题是:

没有main()真的可以编写和编译程序吗?

Is it really possible to write and compile a program without main()?

推荐答案

在标准 C++ 中需要一个 main 函数,所以这个问题对标准 C++ 没有意义.

Within standard C++ a main function is required, so the question does not make sense for standard C++.

在标准 C++ 之外,您可以例如编写 Windows 特定程序并使用 Microsoft 的自定义启动函数之一(wMain、winMain、wWinmain).在 Windows 中,您也可以将程序编写为 DLL 并使用 rundll32 来运行它.

Outside of standard C++ you can for example write a Windows specific program and use one of Microsoft's custom startup functions (wMain, winMain, wWinmain). In Windows you can also write the program as a DLL and use rundll32 to run it.

除此之外,您还可以制作自己的小型运行时库.曾几何时,这是一项常见的运动.

Apart from that you can make your own little runtime library. At one time that was a common sport.

最后,您可以聪明地反驳说,根据标准的 ODR 规则 main 未被使用",因此任何程序都符合条件.呸!尽管除非面试官有不同寻常的幽默感(如果有的话,他们也不会问这个问题),否则他们不会认为这是一个很好的答案.

Finally, you can get clever and retort that according to the standard's ODR rule main isn't "used", so any program qualifies. Bah! Although unless the interviewers have unusual good sense of humor (and they wouldn't have asked the question if they had) they'll not think that that's a good answer.

这篇关于是否可以在不使用 main() 函数的情况下编写程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6